package com.hjm.hbase.client;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.Exception;
/**这是一个如何获取某个hbase表格信息的操作
* 在Java的客户端连接hbase的时候只需要连接zk就可以
* 1.配置对象 设置zk的位置
* 2.获取操作hbase的连接对象
* 3.设置已存在的表的名字
* 4.通过表名进行连接获取表对象
* 5.创建并设置新的get对象的属性. 通过表.get(get对象)获取得到的结果
* 6.对结果进行属性克隆 转String进行print
*/
public class HbaseDemo1 {
public static void main(String[] args) throws Exception {
//1.获取配置对象
Configuration conf = HBaseConfiguration.create();
//2.配置对象设置好属性--zk的位置
conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
//3.通过配置对象创建连接对象
Connection conn = ConnectionFactory.createConnection(conf);
// TableName.create()创建表或者 以下 设置一个已经存在的表名
TableName tb_a = TableName.valueOf("tb_a");
//通过表名得到相应连接的表对象--a
Table a = conn.getTable(tb_a);
//获取这次想要查看的get对象,并设置get的属性-某个行键
Get get = new Get("rk001".getBytes());
//获取get的结果--克隆单元格里边包含的某些属性
Result result = a.get(get);
//result.advance()是否有下一个cell
//result.current()获取当前cell
while (result.advance()){
Cell currentCell = result.current();
byte[] row = CellUtil.cloneRow(currentCell);
byte[] family = CellUtil.cloneFamily(currentCell);
byte[] qualifier = CellUtil.cloneQualifier(currentCell);
byte[] value = CellUtil.cloneValue(currentCell);
System.out.println(new String(row)+"--"+new String(qualifier)+"--"+new String(value));
}
//关闭表格 关闭连接
a.close();
conn.close();
}
}
如有误请不吝指正!