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