参照博客:http://ronxin999.blog.163.com/blog/static/422179202013621111545534/
提供了两种解决思路,这里使用的是第一种
这里使用hbase的PageFilter实现简单的分页
分页过滤,通过设置pagesize参数可以返回每一页page的数量。
客户端需要记住上一次访问的row的key值。
package hbaseTest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* Hello world!
*/
public class PageFilterExample {
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "QT-H-0038");
String tableName = "testTable";
String cfName = "colfam1";
final byte[] POSTFIX = new byte[] { 0x00 };
HTable table = new HTable(config, tableName);
Filter filter = new PageFilter(15);
byte[] lastRow = null;
int totalRows = 0;
while (true) {
Scan scan = new Scan();
scan.setFilter(filter);
if(lastRow != null){
//注意这里添加了POSTFIX操作,不然死循环了
byte[] startRow = Bytes.add(lastRow,POSTFIX);
scan.setStartRow(startRow);
}
ResultScanner scanner = table.getScanner(scan);
int localRows = 0;
Result result;
while((result = scanner.next()) != null){
System.out.println(localRows++ + ":" + result);
totalRows ++;
lastRow = result.getRow();
}
scanner.close();
if(localRows == 0) break;
}
System.out.println("total rows:" + totalRows);
}
}
因为hbase的row是字典序列排列的,因此上一次的lastrow需要添加额外的0表示新的开始。另外startKey的那一行是包含在scan里面的。