设为首页 加入收藏

TOP

JAVA使用HBase根据Rowkey批量查询数据(一次查多条,返回多个记录)
2019-04-14 13:43:33 】 浏览:52
Tags:JAVA 使用 HBase 根据 Rowkey 批量 查询 数据 一次 多条 返回 多个 记录

最近有需求说是根据多个RowKey返回结果集:

public static Configuration conf = null;
    public static Connection connection = null;
    public static Admin admin = null;
    static {
        conf = HBaseConfiguration.create();
 
        conf.set("hbase.zookeeper.quorum", "10.120.193.800,10.120.193.810");
        conf.set("hbase.master", "10.120.193.730:60010");
        conf.set("hbase.zookeeper.property.clientPort", "2181"); 
        //conf.set("zookeeper.znode.parent", "/hbase-unsecure");
        //conf.set("hbase.client.keyvalue.maxsize", "1048576000"); //1G
        conf = HBaseConfiguration.create(conf);
        try {
            connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Hbase表操作

    public static List<String> qurryTableTest(List<String> rowkeyList) throws IOException {
        String tableName = "xxxxx";
        Table table = connection.getTable( TableName.valueOf(tableName));// 获取表
        for (String rowkey : rowkeyList){
            Get get = new Get(Bytes.toBytes(rowkey));
            Result result = table.get(get);
            for (Cell kv : result.rawCells()) {
                String value = Bytes.toString(CellUtil.cloneva lue(kv));
                list.add(value);
            }
        }
        return list;
    }

Hbase的get原码如下:

public Result[] get(List<Get> gets) throws IOException {
        if(gets.size() == 1) {
            return new Result[]{this.get((Get)gets.get(0))};
        } else {
            try {
                Object[] r1 = this.batch(gets);
                Result[] results = new Result[r1.length];
                int i = 0;
                Object[] arr$ = r1;
                int len$ = r1.length;
 
                for(int i$ = 0; i$ < len$; ++i$) {
                    Object o = arr$[i$];
                    results[i++] = (Result)o;
                }
 
                return results;
            } catch (InterruptedException var9) {
                throw (InterruptedIOException)(new InterruptedIOException()).initCause(var9);
            }
        }
    }

实现多RowKey方式如下:

public static List<String> qurryTableTestBatch(List<String> rowkeyList) throws IOException {
        List<Get> getList = new ArrayList();
        String tableName = "table_a";
        Table table = connection.getTable( TableName.valueOf(tableName));// 获取表
        for (String rowkey : rowkeyList){//把rowkey加到get里,再把get装到list中
            Get get = new Get(Bytes.toBytes(rowkey));
            getList.add(get);
        }
        Result[] results = table.get(getList);//重点在这,直接查getList<Get>
        for (Result result : results){//对返回的结果集进行操作
            for (Cell kv : result.rawCells()) {
                String value = Bytes.toString(CellUtil.cloneva lue(kv));
                list.add(value);
            }
        }
        return list;
    }

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇HBase实战 | 排查HBase堆外内存溢.. 下一篇Hbase查询问题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目