java 连接HBase客户端

时间:2022-09-17 08:34:10

HBaseAdmin连接HBase客户端

1. 创建一个新的maven项目,导入org.apache.hadoop等jar包

2. 连接Hbase客户端 并查询数据。

package com.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

/**
* Created by zhaojy on 2017/4/21.
*/

public class HBaseDemo {
private static Configuration config=null;
private static HBaseAdmin admin=null;
//zookeeper集群地址
private final static String HB_ZK_QUORUM="10.8.177.27,10.8.177.28,10.8.177.29";
//zk端口号
private final static String HB_ZK_PORT="2181";
private static HTablePool tp = null;

static {
try {
config= HBaseConfiguration.create();
config.set("hbase.zookeeper.property.clientPort", HB_ZK_PORT);
config.set("hbase.zookeeper.quorum",HB_ZK_QUORUM);
admin = new HBaseAdmin(config);
tp = new HTablePool(config, 10);
}catch (Exception e){
e.printStackTrace();
}
}

//获取表信息
public static HTableInterface getTable(String tableName){
return tp.getTable(tableName);
}


public static void main(String args[]){
//analytics_demo 表名
HTableInterface table = getTable("analytics_demo");
// 获取row=domain.0的数据
Get get = new Get(Bytes.toBytes("domain.0"));
try {
Result result = table.get(get);

System.out.println(result.size());
System.out.println(result);
}catch (Exception e){
e.printStackTrace();
}

}
}