HBase的分页-PageFilter

指定页面行数,返回对应行数的结果集。
需要注意的是,该过滤器并不能保证返回的结果行数小于等于指定的页面行数,因为过滤器是分别作用到各个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;

}
———————

Categories: Hbase

发表评论 取消回复

placeholder.jpg

电子邮件地址不会被公开。

Name
Email
Website
What's on your mind?

博客
32132
07-14 365
07-12 298
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值