hbase limit java,使用java检索hbase中的任意三个随机限定符

My hbase table looks like this:

hbase(main):040:0> scan 'TEST'

ROW COLUMN+CELL

4 column=data:108, timestamp=1399972960190, value=-240.0

4 column=data:112, timestamp=1399972960138, value=-160.0

4 column=data:12, timestamp=1399972922979, value=2

4 column=data:120, timestamp=1399972960124, value=-152.0

4 column=data:144, timestamp=1399972960171, value=-240.0

4 column=data:148, timestamp=1399972960152, value=-240.0

4 column=data:16, timestamp=1399972909606, value=9

4 column=data:8, timestamp=1399972917978, value=6

where all 4s are row id and 108,112,12... are qualifiers. I want to fetch random three qualifiers from this table TEST.

I can fetch all qualifiers but not random three qualifiers. Is there any shell command or API in java through which I can achieve this?

解决方案

If it is about just getting the first three rows use scan shell command with LIMIT set to 3:

hbase(main):001:0> scan 'demo', {LIMIT => 3}

If you wish to do it using Java API, set a loop over ResultScanner which breaks after the third iteration. Simple and easy.

public static void main(String[] args) throws IOException {

Configuration conf = HBaseConfiguration.create();

HTable table = new HTable(conf, "demo");

Scan s = new Scan();

ResultScanner rs = table.getScanner(s);

int check = 0;

for(Result r : rs){

if(++check > 3)

break;

for (KeyValue kv : r.raw()){

System.out.println("Qualifier : " + Bytes.toString(kv.getQualifier()));

}

}

rs.close();

table.close();

}

}

And if you wish to get 3 random rows, use

HTH

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值