tortoiserun

导航

Hbase学习之二 | Java Api篇

Hbase除了通过shell命令来访问外,还提供了Java Api的访问接口,通过Java api创建、删除表,及对记录的增删改查,接着上篇的例子介绍下常用的Api。

添加依赖

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase</artifactId>
    <version>0.94.10</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-core</artifactId>
    <version>0.20.2-cdh3u3</version>
</dependency>

注意:Hbase Jar的版本跟服务器上Hbase的版本需一致。否则Java客户端连接HBase时会报错:Not a host:port pair。

若本地Maven库中没有下载,还需添加配置

<repositories>
<repository>
 <id>cloudera</id>
 <url>https://repository.cloudera.com/content/groups/public</url>
</repository>
</repositories>

确保HBase环境已启动且能连接到,添加配置文件:将服务器上的hbase-site.xml文件拷贝到工程目录下。这样在加载时候会自动加载所需要的配置项,主要的配置项是hbase.zookeeper.quorum(zookeeperHosts)和hbase.zookeeper.property.clientPort(port)。

加载配置,代码示例如下

Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",zookeeperHosts);  
configuration.set("hbase.zookeeper.property.clientPort",port+"");

创建表

/**========创建表=========*/
HBaseAdmin admin = new HBaseAdmin(configuration); //HbaseAdmin负责跟表相关的操作
HTableDescriptor desc = new HTableDescriptor("people");
desc.addFamily(new HColumnDescriptor("blood"));
desc.addFamily(new HColumnDescriptor("feature"));
admin.createTable(desc );

在创建表时,考虑到性能,Family一般只有一个。

RowKey 和 ColumnName 是二进制值(Java 类型 byte[]),value 是一个字节数组(Java类型 byte[])。

增加记录

/**=========插入数据=========*/
HTablePool hTablePool = hbaseDataSource.getHtablePool();
HTableInterface hTableInterface = hbaseDataSource.getHtableInterface(hTablePool, "people");//HTabelInterface负责跟记录相关的操作如增删改查等
Put put = new Put(Bytes.toBytes("1"));
put.add(Bytes.toBytes("blood"), Bytes.toBytes("a"), Bytes.toBytes("a is aaa"));
put.add(Bytes.toBytes("blood"), Bytes.toBytes("b"), Bytes.toBytes("b is bbb"));
put.add(Bytes.toBytes("blood"), Bytes.toBytes("o"), Bytes.toBytes("o is ooo"));
put.add(Bytes.toBytes("blood"), Bytes.toBytes("ab"), Bytes.toBytes("ab is ababab"));
put.add(Bytes.toBytes("feature"), Bytes.toBytes("a"), Bytes.toBytes("aaa"));
put.add(Bytes.toBytes("feature"), Bytes.toBytes("ab"), Bytes.toBytes("ababab"));
table.put(put);

根据rw查询数据

/**=========根据rowkey查询数据=========*/
Get get = new Get(Bytes.toBytes("1"));
Result result = hTableInterface.get(get);
for(KeyValue kv :result.list()){ 
    System.out.println("family:" +Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" +Bytes.toString(kv.getQualifier())); System.out.println(
"value:" +Bytes.toString(kv.getValue())) }

qualifier:Row key和column key在HBase中也称为qualifier,是bytes类型,而时间维度的key则是long integer类型,比较典型的是使用 java.util.Date.getTime()或者System.currentTimeMillis()来做为时间维度的key。

遍历查询数据

/**=========遍历查询数据=========*/
Scan scan = new Scan(Bytes.toBytes(startId), Bytes.toBytes(endId));
ResultScanner rs =null;
try {
  rs = hTableInterface.getScanner(scan);
  for (Result r : rs){
      for (KeyValue kv :r.list()){
           System.out.println("family:" +Bytes.toString(kv.getFamily()));
           System.out.println("value:" +Bytes.toString(kv.getValue()));
           }
      }
} finally {
  rs.close();
}

更新数据

/**=========更新ab的值为a and b=========*/
Put put = new Put(Bytes.toBytes("1")); 
put.add(Bytes.toBytes("blood"), Bytes.toBytes("ab"), Bytes.toBytes("a and b"));
HTableInterface.put(put);

删除数据

/**=========删除指定column=========*/
Delete deleteColumn = new Delete(Bytes.toBytes("1"));
deleteColumn.deleteColumns(Bytes.toBytes("blood"),Bytes.toBytes("ab"));
HTableInterface.delete(deleteColumn);


/**=========删除所有column=========*/ Delete deleteAll = new Delete(Bytes.toBytes("1")); HTableInterface.delete(deleteAll);

删除表

/**=======删除表========*/
admin.disableTable("people");
admin.deleteTable("people");

小结

HBase现在用户越来越多,除了上面提到的,常用api对照jar包自己可以再学习研究下,兴趣是最好的老师。

posted on 2014-08-18 15:08  tortoiserun  阅读(145)  评论(0编辑  收藏  举报