/*
* 查询数据
*/
@Test
public void testGet() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","os-1:2181,os-2:2181,os-3:2181");
//插入数据必须拿到一个客户端对象
HTable hTable = new HTable(conf, "cart");
Get get = new Get(Bytes.toBytes("user_03"));
//指定要查询的列
get.addColumn("product".getBytes(), Bytes.toBytes("product_num"));
Result result = hTable.get(get);
byte[] value = result.getValue("product".getBytes(), "product_num".getBytes());
System.out.println("值为:"+Bytes.toString(value));
hTable.close();
}
/*
* scan查询
*/
@Test
public void testScan() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","os-1:2181,os-2:2181,os-3:2181");
HTable hTable = new HTable(conf, "cart");
Scan scan = new Scan(Bytes.toBytes("user_01"),"user_03".getBytes());
ResultScanner scanner = hTable.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while(iterator.hasNext()) {
Result result= iterator.next();
byte[] value_num = result.getValue("product".getBytes(), "product_num".getBytes());
byte[] value_id = result.getValue("product".getBytes(), "product_id".getBytes());
byte[] value_num_01 = result.getValue("product".getBytes(), "product_num_01".getBytes());
System.out.println(Bytes.toString(value_num_01));
System.out.println(Bytes.toString(value_num));
System.out.println(Bytes.toString(value_id));
}
hTable.close();
}
hbase客户端查询API
最新推荐文章于 2023-06-04 10:59:19 发布