public List<Map<String,String>> getPageData(String tableName,String startRowKey,String endRowKey,int pageNo,int pageSize,boolean rowkeyOnly){
List<Map<String,String>> rtnList = new ArrayList<>();
try(Table table = conn.getTable(TableName.valueOf(tableName))){
Scan scan = new Scan();
Filter pgfilter = new PageFilter(pageSize);
//记录上一页最后一条rowkey
byte[] lastRow = null;
//[start,end)
if(!StringUtils.isBlank(startRowKey))
scan.withStartRow(Bytes.toBytes(startRowKey),true);
if(!StringUtils.isBlank(endRowKey))
scan.withStopRow(Bytes.toBytes(endRowKey),false);
if(rowkeyOnly){
List<Filter> filters = new ArrayList<>();
Filter keyFilter = new FirstKeyOnlyFilter();
filters.add(keyFilter);
filters.add(pgfilter);
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,filters);
scan.setFilter(filterList);
}else
scan.setFilter(pgfilter);
int pageIdx = 1;
while (pageIdx <= pageNo){
//非第一页,更上一页最后一个rowkey+0x00作为startRowkey
if(lastRow != null){
byte[] pageStartRowKey = Bytes.add(lastRow, new byte[]{ 0X00 });
scan.withStartRow(pageStartRowKey,true);
}
ResultScanner scanner = table.getScanner(scan);
int localRows = 0;
Result result;
if(pageIdx == pageNo){
while ((result = scanner.next() )!= null){
HashMap<String,String> map = new LinkedHashMap();
map.put("rowkey", Bytes.toString(result.getRow()));
if(rowkeyOnly == false){
Cell[] cells = result.rawCells();
// 遍历取出cell
if (cells.length>0) {
for (Cell cell : cells) {
String field = Bytes.toString(CellUtil.cloneQualifier(cell));
String fieldVal = Bytes.toString(CellUtil.cloneValue(cell));
map.put(field, fieldVal);
}
}
}
rtnList.add(map);
}
}else {
while ((result = scanner.next()) != null){
localRows ++;
lastRow = result.getRow();
}
}
pageIdx++;
scanner.close();
//这一页没有记录了,表示没有查到数据,跳出while
if(localRows == 0) break;
}
}catch (Exception e){
logger.error("scan " + tableName +",{STARTROW=>" +startRowKey + ", STOPROW=>" + endRowKey + "},error !
",e);
throw new MyCheckException("hbase scan failed!"+e.getMessage());
}
return rtnList;
}
注:如果使用多个Filter的话,PageFilter最后添加