java客户端获取Hbase单行数据或多行数据示例

来源:互联网 发布:mac上无法连接itunes 编辑:程序博客网 时间:2024/06/16 18:30
public static void main(String[] args) throws MasterNotRunningException, IOException{
// TODO Auto-generated method stub
String tableName = "zrl_emp";
//配置对象
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.master", "hdfs://Master.Hadoop:60000");
conf.set("hbase.rootdir", "hdfs://Master.Hadoop:9000/hbase");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "Master.Hadoop,Slave1.Hadoop");
//连接对象
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf(tableName));
//查询单行数据
Get g = new Get(Bytes.toBytes("rowKey1"));
Result result = table.get(g);
byte [] value = result.getValue(Bytes.toBytes("personal data"),
Bytes.toBytes("newColumnName2"));
byte[] value1 = result.getValue(Bytes.toBytes("personal data"),
Bytes.toBytes("city"));

//查询多行数据
List<Get> gets = new ArrayList<Get>();
Get get2 = new Get(Bytes.toBytes("rowKey2"));
Get get3 = new Get(Bytes.toBytes("rowKey3"));
Get get4 = new Get(Bytes.toBytes("rowKey4"));
gets.add(get2);
gets.add(get3);
gets.add(get4);
Result[] results = table.get(gets);


table.close();
System.out.println(result.toString());
System.out.println(result.size());
System.out.println(result.rawCells());
System.out.println(Bytes.toString(value));
System.out.println(Bytes.toString(value1));
System.out.println("--------------------");
for (int i = 0; i < results.length; i++) {
byte [] value2 = results[i].getValue(Bytes.toBytes("personal data"),
Bytes.toBytes("newColumnName2"));
System.out.println(Bytes.toString(value2));
}
}