hbase 1.1.3 java使用方式
- 1.pom引用
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.3</version>
</dependency>
2.客户端host配置
由于hbase 客户端通过zookeeper查询Hmaste、Hregionserver的地址信息,而通过zookeeper中返回的是HMaster、HRegionserver中host信息。所以需要在客户端配置对应的host。192.168.162.1 host1
192.168.162.2 host2
192.168.162.3 host33.java 客户端
/**
* hbase client 单例连接
* @author chun
*
*/
public class HbaseClient {
private static final Logger LOGGER = LoggerFactory.getLogger(HbaseClient.class);
private Configuration conf = null;
private Connection conn = null;
private static HbaseClient instance = null;
private HbaseClient(){
init();
}
public void init(){
try{
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", PropertiesUtil.getStringValue("hbase.zookeeper.quorum"));
conf.set("zookeeper.znode.parent", PropertiesUtil.getStringValue("zookeeper.znode.parent"));
conf.set("hbase.zookeeper.property.clientPort", PropertiesUtil.getStringValue("hbase.zookeeper.property.clientPort"));
conn = ConnectionFactory.createConnection(conf);
}catch(Exception e){
LOGGER.error("初始化hbase连接失败"+e.getMessage(),e);
}
}
public static HbaseClient getInstance(){
if(instance == null){
synchronized (HbaseClient.class) {
if(instance == null){
instance = new HbaseClient();
}
}
}
return instance;
}
/**
* 获取htable操作类
* @param tableName
* @return
* @throws IOException
*/
public Table getHtable(String tableName) throws IOException{
return conn.getTable(TableName.valueOf(tableName));
}
/**
*
* @param hTableInterface
*/
public void relaseHtable(Table table){
if(table == null){
return;
}
try {
table.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(),e);
}
}
/**
* 关闭hbase连接
*/
public void destory(){
try {
conn.close();
instance = null;
} catch (IOException e) {
LOGGER.error(e.getMessage(),e);
}
}
}
- 4.简单示例
/**
* 测试get使用
*/
@Test
public void testGet(){
HTableInterface hTableInterface = null;
try{
Get get = new Get(Bytes.toBytes("a1"));
Get get2 = new Get(Bytes.toBytes("a2"));
List<Get> gets = new ArrayList<Get>();
gets.add(get);
gets.add(get2);
hTableInterface = HbaseClient.getInstance().getHtable("test");
Result[] results = hTableInterface.get(gets);
for(Result result : results){
if(!result.isEmpty()){
System.out.println("the row is "+ new String(result.getRow()));
NavigableMap<byte[], byte[]> qualiers = result.getFamilyMap(Bytes.toBytes("cf"));
for(Entry<byte[], byte[]> entry: qualiers.entrySet()){
System.out.println("the qualier is "+Bytes.toString(entry.getKey())+" value is "+ Bytes.toString(entry.getValue()));
}
}else{
System.out.println("the row is empty");
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
HbaseClient.getInstance().relaseHtable(hTableInterface);
}
}
/**
* 测试scan使用
*/
@Test
public void testScan(){
Table table = null;
try{
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("a"));
table = HbaseClient.getInstance().getHtable("t1");
ResultScanner scanner = table.getScanner(scan);
while(true){
Result result = scanner.next();
if(result == null){
break;
}
if(!result.isEmpty()){
System.out.println("the row is "+ new String(result.getRow()));
NavigableMap<byte[], byte[]> qualiers = result.getFamilyMap(Bytes.toBytes("cf"));
for(Entry<byte[], byte[]> entry: qualiers.entrySet()){
System.out.println("the qualier is "+Bytes.toString(entry.getKey())+" value is "+ Bytes.toString(entry.getValue()));
}
}else{
System.out.println("the row is empty");
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
HbaseClient.getInstance().relaseHtable(table);
}
}