Hbase分页

    和传统数据库不同,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 Map<String,String> resultHandle(Result rs){
		Map<String,String> result = new HashMap<String, String>();
		Cell[] cells = rs.rawCells();
		result = new HashMap<String, String>();
		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<String,String> > scanByPage(String tableName,String StartRow,S            tring endRow,Integer currentPae,Integer pageSize,Filter ...filters){
		 
		 HTableInterface table = null;
		 List<byte[]> rowKeyList = new LinkedList<byte[]>();
		 List< Map<String,String> > result = new LinkedList<Map<String,String>>();
		 try {
			 //格式化输入信息
			 if( currentPae == null || currentPae == 0 )
				 currentPae = 1;
			 if( pageSize == null || pageSize == 0 )
				 pageSize = 100;
			 //计算分页的数据范围
			 int firstCount = (currentPae - 1) * pageSize; 
			 int maxCount = firstCount + pageSize - 1; 
			 table = getTable(tableName);
			 Scan scan = getScan(StartRow, endRow);
			 PageFilter filter = new PageFilter(maxCount+1);
			 scan.setFilter(filter);
			 scan.setMaxVersions();
			 if( filters!=null ){
				 for( int i=0;i<filters.length;i++ ){
					 scan.setFilter(filters[i]);
				 }
			 }
			 //TODO 只取rowkey
			 ResultScanner scanner = table.getScanner(scan);
			 int i=0;
			 //获取分页数据的rowKey
			 for( Result rs:scanner ){
				 byte[] rowKey = null;
				 if( i>=firstCount && i<=maxCount ){
					 rowKey = rs.getRow();
					 rowKeyList.add(rowKey);
				 }
				 i++;
			 }
			 //通过行健集合构建查询集合
			 List<Get> getList = getGetList(rowKeyList);
			 Result[] rss = table.get(getList);
			 Map<String, String> rsMap = null;
			 for( Result rs:rss ){
				 rsMap = resultHandle(rs);
				 result.add(rsMap);
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeTable(table);
		}
		 return result;
		
	 }
	 
}


转载于:https://my.oschina.net/u/2270476/blog/348609

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值