java - How do I create a row key prefix scan using asynchbase -
i'm trying row-key prefix scan using java asynchbase library. e.g. want find first 10 records row key has prefix "abc":
byte[] prefix = bytes.tobytes("abc"); scanner scanner = hbaseclient.newscanner(tblname); scanner.setfamily(bytes.tobytes("cf_test")); scanner.setfilter(new rowfilter(comparefilter.compareop.equal, new binaryprefixcomparator(prefix))); result = scanner.nextrows(10).join(); // ...
i use rowfilter job, doesn't work. seems trigger full-table scan.
i don't find filter class rowprefixfilter
, neither #setrowkeyprefix()
method in scanner class.
how create scan match row key prefix?
update:
read code of hbase-client project , found 1 solution. in org.apache.hadoop.hbase.client.scan
class, #setrowprefixfilter()
method implemented below:
public scan setrowprefixfilter(byte[] rowprefix) { if (rowprefix == null) { setstartrow(hconstants.empty_start_row); setstoprow(hconstants.empty_end_row); } else { this.setstartrow(rowprefix); this.setstoprow(calculatetheclosestnextrowkeyforprefix(rowprefix)); } return this; }
it implemented via setting start row , stop row.
change code to:
byte[] prefix = bytes.tobytes("abc"); scanner scanner = hbaseclient.newscanner(tblname); scanner.setfamily(bytes.tobytes("cf_test")); scanner.setstartkey(prefix); scanner.setstopkey(calculatetheclosestnextrowkeyforprefix(prefix)); result = scanner.nextrows(10).join();
the implementation of #calculatetheclosestnextrowkeyforprefix()
same 1 in org.apache.hadoop.hbase.client.scan
:
private byte[] calculatetheclosestnextrowkeyforprefix(byte[] rowkeyprefix) { // treating 'unsigned very long' , // doing +1 manually. // search place trailing 0xffs start int offset = rowkeyprefix.length; while (offset > 0) { if (rowkeyprefix[offset - 1] != (byte) 0xff) { break; } offset--; } if (offset == 0) { // got 0xffff... (only ffs) stoprow value // last possible prefix before end of table. // set stop @ 'end of table' return hconstants.empty_end_row; } // copy right length of original byte[] newstoprow = arrays.copyofrange(rowkeyprefix, 0, offset); // , increment last 1 newstoprow[newstoprow.length - 1]++; return newstoprow; }
Comments
Post a Comment