HBase客户端Java API使用总结

HBase版本:HBase 0.95.0-hadoop1

客户端程序依赖的库,全部可从HBase的lib目录下找到
commons-codec-1.7.jar
commons-configuration-1.6.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
guava-12.0.1.jar
hadoop-core-1.1.2.jar
hbase-client-0.95.0-hadoop1.jar
hbase-common-0.95.0-hadoop1.jar
hbase-protocol-0.95.0-hadoop1.jar
htrace-1.50.jar
log4j-1.2.17.jar
protobuf-java-2.4.1.jar
slf4j-api-1.4.3.jar
slf4j-log4j12-1.4.3.jar
zookeeper-3.4.5.jar

HBase客户端编程框架

public class XXX {

 public static void main(String args[]) throws Exception {
  /* 唯一必须指定的配置项是ZooKeeper的位置 */
  Configuration conf = new Configuration();
  conf.set("hbase.zookeeper.quorum",
    "192.168.1.87,192.168.1.92,192.168.1.76");
  
  /* HTable是客户端访问HBase的入口类,对指定Table的CRUD操作通过该类请求 */
  HTable table = new HTable(conf, "test");
  
  try {
   
   // 增加或修改行
   Put put = new Put(Bytes.toBytes("row-x"));
   // 添加要增加或修改的列,需依次指定列族、列、值,还可指定版本号
   put.add(Bytes.toBytes("xx"), Bytes.toBytes("col-1"),
     Bytes.toBytes("value-1"));
   put.add(Bytes.toBytes("xx"), Bytes.toBytes("col-2"),
     123, Bytes.toBytes("value-2 of version 123"));
   // 提交PUT请求
   table.put(put);
   
   // 删除行
   Delete delete = new Delete(Bytes.toBytes("row-x"));
   // 若不想删除整行,可指定要删除的列族、列、列的某个版本
   /*
   删除指定列族:
   delete.deleteFamily(Bytes.toBytes("xx"));
   删除指定列:
   delete.deleteColumn(Bytes.toBytes("xx"), Bytes.toBytes("col-1"));
   删除指定列的某个版本:
   delete.deleteColumn(Bytes.toBytes("xx"), Bytes.toBytes("col-1"), 123);
   */
   // 提交DELETE请求
   table.delete(delete);
   
   // 查找行
   /*
   查找单行使用Get,查找多行使用Scan,Get是Scan的特殊情况,查找的结果都是Result实例
   使用Get查找单行:
   Get get = new Get(Bytes.toBytes("row-x"));
   Result result = table.get(get);
   使用Scan查找多行:
   Scan scan = new Scan();
   还可使用Scan的相应方法限定查找的行范围、列范围,若不限定则查找全部
   Iterator<Result> it = table.getScanner(scan).iterator();
   while (it.hasNext()) {
    Result result = it.next();
    ...
   }
   */
   // Result封装了一行的被查询内容:KV对的列表
   String row = Bytes.toString(result.getRow());
   System.out.println("Row Key: " + row);
   List<KeyValue> list = result.list();
   for (KeyValue pair : list) {
    pair.getRow(); // Row Key
    pair.getFamily(); // 列族
    pair.getQualifier(); // 列
    pair.getTimestamp(); //版本号
   }

  } finally {
   table.close(); /* 操作完Table后关闭相应的HTable实例 */
  }
 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值