指定页面行数,返回对应行数的结果集。
需要注意的是,该过滤器并不能保证返回的结果行数小于等于指定的页面行数,因为过滤器是分别作用到各个region server的,它只能保证当前region返回的结果行数不超过指定页面行数。
构造函数:
PageFilter(long pageSize)
代码:
Table table = connection.getTable(TableName.valueOf(“user”));
PageFilter pf = new PageFilter(2L);
Scan scan = new Scan();
scan.setFilter(pf);
scan.setStartRow(Bytes.toBytes(“zhangsan_”));
ResultScanner rs = table.getScanner(scan);
结果:返回的结果实际上有四条,因为这数据来自不同RegionServer,
[row:zhangsan_1495527850759],[family:account],[qualifier:idcard],[value:9897645464646],[time:1495556648664]
[row:zhangsan_1495527850759],[family:account],[qualifier:passport],[value:5689879898],[time:1495636370056]
[row:zhangsan_1495527850824],[family:account],[qualifier:country],[value:china],[time:1495636452285]
[row:zhangsan_1495527850824],[family:account],[qualifier:name],[value:zhangsan],[time:1495556648729]
———————
public static JSONObject divicePage(String tableName, String startRow,
String endRow, String lastRowKey, int num) throws IOException {
Filter filter = new PageFilter(num);//每页展示条数
byte[] lastRow = null;
HTableInterface table = HbaseConnection.getHConnection().getTable(tableName);
Scan scan = new Scan();
scan.setFilter(filter);
if (lastRowKey != null) {
lastRow = lastRowKey.getBytes();
// 注意这里添加了POSTFIX操作,不然死循环了
//因为hbase的row是字典序列排列的,因此上一次的lastrow需要添加额外的0表示新的开始。另外startKey的那一行是包含在scan里面的
byte[] start = Bytes.add(lastRow, POSTFIX);
scan.setStartRow(start);
}else{
scan.setStartRow(startRow.getBytes());
}
byte[] end = endRow.getBytes();
scan.setStopRow(end);
ResultScanner rs = table.getScanner(scan);
Result r = null;
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
while ((r = rs.next()) != null) {
lastRow = r.getRow();
System.out.println(Bytes.toString(lastRow));
List<Cell> cells = r.listCells();
JSONObject record = new JSONObject();
for(int i=0;i<cells.size();i++){
String key = Bytes.toString(CellUtil.cloneQualifier(cells.get(i)));
String value = Bytes.toString(CellUtil.cloneValue(cells.get(i)));
record.put(key, value);
}
array.add(record);
}
rs.close();
json.put(“last_row”, Bytes.toString(lastRow));
json.put(“data”, array);
System.out.println(array.size());
return json;
}
———————