和传统数据库不同,Hbase的分页非常的困难(就我的愚见,可以说hbase无法真正分页),在参考了网上一些代码后,写了一段hbase分页代码。其原理主要是,先查询出rowKey,对rowKey进行分页后,再通过分页后的rowKey进行数据查询。
public class HbaseHelp { private static Configuration conf = null; /** * hbase的连接 * 用于替代HtablePool * 用此类创建Htable会提高效率 */ private static HConnection conn = null; /** * 初始化配置 */ static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", PropertiesUtil.getPropertyString("hbase.zookeepe r.quorum")); conf.set("hbase.zookeeper.property.clientPort", PropertiesUtil.getPropertyString("h base.zookeeper.property.clientPort")); conf.set("hbase.zookeeper.master", "192.168.1.12:60000"); try { conn = HConnectionManager.createConnection(conf); } catch (IOException e) { System.out.println("创建连接失败"); e.printStackTrace(); } } private static HTableInterface getTable(String tableName) throws IOException{ return conn.getTable( tableName ); } private static Scan getScan(String StartRow,String endRow){ Scan scan = new Scan(); if( StartRow!=null ) scan.setStartRow( toBytes(StartRow) ); if( endRow!=null ) scan.setStopRow( toBytes(endRow) ); //设置缓存数量 scan.setCaching(1000); //开启缓存 scan.setCacheBlocks(true); return scan; } private static MapresultHandle(Result rs){ Map result = new HashMap (); Cell[] cells = rs.rawCells(); result = new HashMap (); boolean isFirst = true; for( Cell cell:cells ){ if( isFirst ){ result.put( "rowKey" , toString( CellUtil.cloneRow(cell) ) ); result.put("timeStamp", cell.getTimestamp()+""); isFirst = false; } result.put(toString( CellUtil.cloneQualifier(cell) ), toString( CellUtil.cloneValue(cell) )); } return result; } private static void closeTable(HTableInterface table){ if( table==null ) return; try { table.close(); } catch (Exception e) { System.out.println("关闭表"+table.getName()+"失败!!!!!!"); e.printStackTrace(); } } public static List< Map > scanByPage(String tableName,String StartRow,S tring endRow,Integer currentPae,Integer pageSize,Filter ...filters){ HTableInterface table = null; List rowKeyList = new LinkedList (); List< Map > result = new LinkedList