Hbase访问方式之Java API
2016-03-10 16:05
471 查看
Hbase的访问方式
1、Native Java API:最常规和高效的访问方式;
2、HBase Shell:HBase的命令行工具,最简单的接口,适合HBase管理使用;
3、Thrift Gateway:利用Thrift序列化技术,支持C++,PHP,Python等多种语言,适合其他异构系统在线访问HBase表数据;
4、REST Gateway:支持REST 风格的Http API访问HBase, 解除了语言限制;
5、MapReduce:直接使用MapReduce作业处理Hbase数据;
6、使用Pig/hive处理Hbase数据。
常用Java API的用法:
1、加载配置
[java] view plaincopy
Configuration config = HBaseConfiguration.create();
//可以自定义配置,也可以从自定义配置文件中读取
/*config.set("hbase.zookeeper.property.clientPort", "4181");
config.set("hbase.zookeeper.quorum", "hadoop.datanode5.com,hadoop.datanode2.com,hadoop.datanode3.com");
config.set("hbase.master", "hadoop.datanode3.com\\:600000");*/
2、表的创建、表信息修改、表删除
[java] view plaincopy
HBaseAdmin admin = new HBaseAdmin(config);
//创建表
HTableDescriptor htd = new HTableDescriptor(tableName);
htd.addFamily(new HColumnDescriptor("cf1"));
htd.addFamily(new HColumnDescriptor("cf2"));
admin.createTable(htd);
//修改表信息
admin.disableTable(tableName);
// modifying existing ColumnFamily
admin.modifyColumn(tableName, new HColumnDescriptor("cf1"));
admin.enableTable(tableName);
//删除表
admin.disableTable(Bytes.toBytes(tableName));
admin.deleteTable(Bytes.toBytes(tableName));
3、添加记录
[java] view plaincopy
/** 在多次使用时,建议用HTablePool
HTable table = new HTable(config, tableName);
=>
HTablePool pool = new HTablePool(config, 1000);
HTableInterface table = pool.getTable(tableName);*/
HTable table = new HTable(config, tableName);
/**
* 在插入操作时,默认不适用任何缓存
* 可自定义使用缓存,以及缓存大小
* 每个任务最后需要手工调用 flushCommits();
*/
/*table.setAutoFlush(false);
table.setWriteBufferSize(1024);*/
Put put1 = new Put(Bytes.toBytes(rowKey));
if (ts == 0) {
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
} else {
//自定义版本时,从自定义的版本号,类型为long
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts,Bytes.toBytes(value));
}
table.put(put1);
//table.flushCommits();
4、查询,根据Rowkey查询
[java] view plaincopy
Get get1 = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get1);
System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
Result[] result = table.get(List<Get>);//查询指定Rowkey的多条记录
5、查询,指定条件和rowkey区间查询
[java] view plaincopy
Scan scan = new Scan();
//默认缓存大小为1,设置成一个合理的值,可以减少scan过程中next()的时间开销,代价是客户端的内存
scan.setCaching(500);
scan.setCacheBlocks(false);
//根据startRowKey、endRowKey查询
//Scan scan = new Scan(Bytes.toBytes("startRowKey"), Bytes.toBytes("endRowKey"));
//rowKey之外的过滤条件,在List中可以add;
/**List<Filter> filters = new ArrayList<Filter>();
Filter filter = new SingleColumnValueFilter("familyName".getBytes(),
"qualifierName".getBytes(),
CompareOp.EQUAL,
Bytes.toBytes("value"));
filters.add(filter);
scan.setFilter(new FilterList(filters));*/
ResultScanner scanner = table.getScanner(scan);
System.out.println("scan result list:");
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("data1"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("data2"))));
}
scanner.close();
参考:
1、http://www.taobaotest.com/blogs/1605
2、http://abloz.com/hbase/book.html#data_model_operations(官网示例)
/article/8132069.html
http://blog.csdn.net/woshiwanxin102213/article/details/17914083 http://blog.csdn.net/woshiwanxin102213/article/details/17611457
1、Native Java API:最常规和高效的访问方式;
2、HBase Shell:HBase的命令行工具,最简单的接口,适合HBase管理使用;
3、Thrift Gateway:利用Thrift序列化技术,支持C++,PHP,Python等多种语言,适合其他异构系统在线访问HBase表数据;
4、REST Gateway:支持REST 风格的Http API访问HBase, 解除了语言限制;
5、MapReduce:直接使用MapReduce作业处理Hbase数据;
6、使用Pig/hive处理Hbase数据。
常用Java API的用法:
1、加载配置
[java] view plaincopy
Configuration config = HBaseConfiguration.create();
//可以自定义配置,也可以从自定义配置文件中读取
/*config.set("hbase.zookeeper.property.clientPort", "4181");
config.set("hbase.zookeeper.quorum", "hadoop.datanode5.com,hadoop.datanode2.com,hadoop.datanode3.com");
config.set("hbase.master", "hadoop.datanode3.com\\:600000");*/
2、表的创建、表信息修改、表删除
[java] view plaincopy
HBaseAdmin admin = new HBaseAdmin(config);
//创建表
HTableDescriptor htd = new HTableDescriptor(tableName);
htd.addFamily(new HColumnDescriptor("cf1"));
htd.addFamily(new HColumnDescriptor("cf2"));
admin.createTable(htd);
//修改表信息
admin.disableTable(tableName);
// modifying existing ColumnFamily
admin.modifyColumn(tableName, new HColumnDescriptor("cf1"));
admin.enableTable(tableName);
//删除表
admin.disableTable(Bytes.toBytes(tableName));
admin.deleteTable(Bytes.toBytes(tableName));
3、添加记录
[java] view plaincopy
/** 在多次使用时,建议用HTablePool
HTable table = new HTable(config, tableName);
=>
HTablePool pool = new HTablePool(config, 1000);
HTableInterface table = pool.getTable(tableName);*/
HTable table = new HTable(config, tableName);
/**
* 在插入操作时,默认不适用任何缓存
* 可自定义使用缓存,以及缓存大小
* 每个任务最后需要手工调用 flushCommits();
*/
/*table.setAutoFlush(false);
table.setWriteBufferSize(1024);*/
Put put1 = new Put(Bytes.toBytes(rowKey));
if (ts == 0) {
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
} else {
//自定义版本时,从自定义的版本号,类型为long
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts,Bytes.toBytes(value));
}
table.put(put1);
//table.flushCommits();
4、查询,根据Rowkey查询
[java] view plaincopy
Get get1 = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get1);
System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
Result[] result = table.get(List<Get>);//查询指定Rowkey的多条记录
5、查询,指定条件和rowkey区间查询
[java] view plaincopy
Scan scan = new Scan();
//默认缓存大小为1,设置成一个合理的值,可以减少scan过程中next()的时间开销,代价是客户端的内存
scan.setCaching(500);
scan.setCacheBlocks(false);
//根据startRowKey、endRowKey查询
//Scan scan = new Scan(Bytes.toBytes("startRowKey"), Bytes.toBytes("endRowKey"));
//rowKey之外的过滤条件,在List中可以add;
/**List<Filter> filters = new ArrayList<Filter>();
Filter filter = new SingleColumnValueFilter("familyName".getBytes(),
"qualifierName".getBytes(),
CompareOp.EQUAL,
Bytes.toBytes("value"));
filters.add(filter);
scan.setFilter(new FilterList(filters));*/
ResultScanner scanner = table.getScanner(scan);
System.out.println("scan result list:");
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("data1"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("data2"))));
}
scanner.close();
参考:
1、http://www.taobaotest.com/blogs/1605
2、http://abloz.com/hbase/book.html#data_model_operations(官网示例)
/article/8132069.html
http://blog.csdn.net/woshiwanxin102213/article/details/17914083 http://blog.csdn.net/woshiwanxin102213/article/details/17611457
相关文章推荐
- spring 多数据源 事物
- Java内存与垃圾回收调优
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
- Java 并发之 ConcurrentSkipListMap 简述
- spring 动态注册数据源
- Spring Bean 初始化过程
- java 线程池详解
- 详解Java GC的工作原理
- java file 海量文件处理
- JDK 源码设计 时间换空间 &amp; 空间换时间
- Exception in thread &quot;main&quot; java.lang.LinkageError错误的解决
- myeclipse使用maven教程
- 《深入理解java虚拟机》学习笔记4——Java虚拟机垃圾收集器
- myeclipse 上安装 Maven3
- 各种 Java 的序列化库的性能比较测试结果
- eclipse JRebel 插件
- MyBatis-spring和spring JDBC批量插入Mysql的效率比较
- struts2 ognl java.lang.NoSuchMethodException Ljava.lang.String
- java clinit 死锁
- java.lang.NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/Strin