hbase客户端查询API

来源:互联网 发布:时尚笔记本电脑淘宝 编辑:程序博客网 时间:2024/04/29 23:37
    /*     * 查询数据     */    @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();    }
原创粉丝点击