JAVA使用HBase的Rowkey精确批量处理

8 篇文章 0 订阅

最近有需求说是根据多个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 = 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.cloneValue(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>  queryTableTestBatch(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.cloneValue(kv));
                list.add(value);
            }
        }
        return list;
    }

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值