HBase的常用Java API

1. 创建HBase表的对象

HBase表的对项名字叫HTable,创建它的方法有很多,常见的有如下:

org.apache.hadoop.hbase.client.HTable hTable = new HTable(org.apache.hadoop.hbase.HBaseConfiguration conf, String tableName);

或

org.apache.hadoop.hbase.client.HTable hTable = new HTable(org.apache.hadoop.hbase.HBaseConfiguration conf, Byte[] tableName);

1.1 其中的conf为配置对象,它的创建以及相关参数设置如下:

//创建对象
HBaseConfiguration conf = HBaseConfiguration.create();/

//设置zookeeper
conf.set("hbase.zookeeper.quorum","hadoop-task04,hadoop-task05,hadoop-task06");

注:1.它的相关册数设置是和配置文件hbase-site.xml里的设置是一致的。

  2.建表还可以通过表的资源池来拿到一张表,不过这个方法已经过时了(org.apache.hadoop.hbase.client.HTablePool).

  3.可以通过客户端与HBase的连接来拿到一张表的对象,代码如下(推荐):

HConnection connection = HConnectionManager.createConnection(config);
 HTableInterface table = connection.getTable("table1");
 try {
   // Use the table as needed, for a single operation and a single thread
 } finally {
   table.close();
   connection.close();
 }

这样就可以拿到一个表的对象了。

待更新。。。