博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hbase分页
阅读量:7039 次
发布时间:2019-06-28

本文共 3157 字,大约阅读时间需要 10 分钟。

hot3.png

    和传统数据库不同,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
 resultHandle(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
>();  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
=firstCount && i<=maxCount ){  rowKey = rs.getRow();  rowKeyList.add(rowKey);  }  i++;  }  //通过行健集合构建查询集合  List
 getList = getGetList(rowKeyList);  Result[] rss = table.get(getList);  Map
 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

你可能感兴趣的文章
inode探究及inode与软链接与硬链接关系
查看>>
web服务篇
查看>>
Maven&Nexus安装及使用
查看>>
LVS
查看>>
css3中translateY、translateX的使用
查看>>
创建mysql触发器
查看>>
shell 从文件按行读
查看>>
笔记3
查看>>
《Linux菜鸟入门》网关及DNS
查看>>
Python~~简介介绍
查看>>
使用cocoaPods,导入第三方库头文件没有自动联想
查看>>
锁定数据行 for update和for update nowait
查看>>
项目管理工具必须具备的5个功能
查看>>
PHP+socket+SMTP、POP3协议发送、接收邮件
查看>>
我的重构--重构案例2
查看>>
大快网站:如何选择正确的hadoop版本
查看>>
dhcp snopping及华三交换机配置
查看>>
java基础-IO
查看>>
python多线程之创建线程
查看>>
我的友情链接
查看>>