HbaseClient Scan類中setStartRow()和setStopRo()的使用
這兩個函數就是設置scan的起始位置、終止位置(exclusive)。
假設test表中存儲着如下RowKey:
12Aabb
3aabb
Aabb
aabb
運行如下代碼:
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("1"));
scan.setStopRow(Bytes.toBytes("A"));
scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
ResultScanner result = table.getScanner(scan);
result.forEach(r -> {
Map map = r.getFamilyMap(Bytes.toBytes("info"));
List<Cell> cells = r.listCells();
cells.forEach(c -> System.out.println(Bytes.toString(CellUtil.cloneRow(c))));
});
//結果:
//12Aabb
//3aabb
這只是一個簡單的例子,希望你不要把上面設置的函數參數”1”、”A”理解錯了。
由於RowKey是按ASCII碼錶升序排列。設置setStartRow、setStopRow的意義就是:返回所有滿足條件”1”< rowKey <”A” 的行
這裏用於比較RowKey大小的函數就是Java String中的compareTo(“”)
用上面的例子來說的話即:若rowKey.compareTo(“1”)>0,且rowKey.compareTo(“A”)<0就該行就應該返回
假設這樣:
scan.setStartRow(Bytes.toBytes("1"));
scan.setStopRow(Bytes.toBytes("A{"));
//結果:
//12Aabb
//3aabb
//Aabb
關於Hbase RowKey排序:http://blog.csdn.net/zhaominpro/article/details/79423328