@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()
}
@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()
}