hbase分页查询实现-

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

hbase分页查询实现Hbase本身是没有分页查询的,我在网上找了很多资料来实现一个分页功能,在这里做了一下记录,分享给大家,有什么不足之处,请尽管指出。废话不多说,看代码。importjava.io.IOException;importjava.util.LinkedHashMap;importjava.util.LinkedList;importjava.util.List;importjava.util.Map;importorg.apache.commons.lang.StringUtils;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.HTableInterface;importorg.apache.hadoop.hbase.client.HTablePool;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.filter.CompareFilter.CompareOp;importorg.apache.hadoop.hbase.filter.Filter;importorg.apache.hadoop.hbase.filter.FilterList;importorg.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;importorg.apache.hadoop.hbase.filter.SingleColumnValueFilter;importorg.apache.hadoop.hbase.util.Bytes;publicclassHBaseUtils{privatestaticConfigurationconfig=null;privatestaticHTablePooltp=null;static{//加载集群配置config=HBaseConfiguration.create();config.set(hbase.zookeeper.quorum,xx.xx.xx);config.set(hbase.zookeeper.property.clientPort,2181);//创建表池(可伟略提高查询性能,具体说明请百度或官方API)tp=newHTablePool(config,10);}/**获取hbase的表*/publicstaticHTableInterfacegetTable(StringtableName){if(StringUtils.isEmpty(tableName))returnnull;returntp.getTable(getBytes(tableName));}/*转换byte数组*/publicstaticbyte[]getBytes(Stringstr){if(str==null)str=;returnBytes.toBytes(str);}/***查询数据*@paramtableKey表标识*@paramqueryKey查询标识*@paramstartRow开始行*@paramparamsMap参数集合*@return结果集*/publicstaticTBDatagetDataMap(StringtableName,StringstartRow,StringstopRow,IntegercurrentPage,IntegerpageSize)throwsIOException{ListMapString,StringmapList=null;mapList=newLinkedListMapString,String();ResultScannerscanner=null;//为分页创建的封装类对象,下面有给出具体属性TBDatatbData=null;try{//获取最大返回结果数量if(pageSize==null||pageSize==0L)pageSize=100;if(currentPage==null||currentPage==0)currentPage=1;//计算起始页和结束页IntegerfirstPage=(currentPage-1)*pageSize;IntegerendPage=firstPage+pageSize;//从表池中取出HBASE表对象HTableInterfacetable=getTable(tableName);//获取筛选对象Scanscan=getScan(startRow,stopRow);//给筛选对象放入过滤器(true标识分页,具体方法在下面)scan.setFilter(packageFilters(true));//缓存1000条数据scan.setCaching(1000);scan.setCacheBlocks(false);scanner=table.getScanner(scan);inti=0;Listbyte[]rowList=newLinkedListbyte[]();//遍历扫描器对象,并将需要查询出来的数据rowkey取出for(Resultresult:scanner){Stringrow=toStr(result.getRow());if(i=firstPage&&iendPage){rowList.add(getBytes(row));}i++;}//获取取出的rowkey的GET对象ListGetgetList=getList(rowList);Result[]results=table.get(getList);//遍历结果for(Resultresult:results){Mapbyte[],byte[]fmap=packFamilyMap(result);MapString,Stringrmap=packRowMap(fmap);mapList.add(rmap);}//封装分页对象tbData=newTBData();tbData.setCurrentPage(currentPage);tbData.setPageSize(pageSize);tbData.setTotalCount(i);tbData.setTotalPage(getTotalPage(pageSize,i));tbData.setResultList(mapList);}catch(IOExceptione){e.printStackTrace();}finally{closeScanner(scanner);}returntbData;}privatestaticintgetTotalPage(intpageSize,inttotalCount){intn=totalCount/pageSize;if(totalCount%pageSize==0){returnn;}else{return((int)n)+1;}}//获取扫描器对象privatestaticScangetScan(StringstartRow,StringstopRow){Scanscan=newScan();scan.setStartRow(getBytes(startRow));scan.setStopRow(getBytes(stopRow));returnscan;}/***封装查询条件*/privatestaticFilterListpackageFilters(booleanisPage){FilterListfilterList=null;//MUST_PASS_ALL(条件AND)MUST_PASS_ONE(条件OR)filterList=newFilterList(FilterList.Operator.MUST_PASS_ALL);Filterfilter1=null;Filterfilter2=null;filter1=newFilter(getBytes(family1),getBytes(column1),CompareOp.EQUAL,getBytes(condition1));filter2=newFilter(getBytes(family2),getBytes(column1),CompareOp.LESS,getBytes(condition2));filterList.addFilter(filter1);filterList.addFilter(filter2);if(isPage){filterList.addFilter(newFirstKeyOnlyFilter());}returnfilterList;}privatestaticFilternewFilter(byte[]f,byte[]c,CompareOpop,byte[]v){returnnewSingleColumnValueFilter(f,c,op,v);}privatestaticvoidcloseScanner(ResultScannerscanner){if(scanner!=null)scanner.close();}/***封装每行数据*/privatestaticMapString,StringpackRowMap(Mapbyte[],byte[]dataMap){MapString,Stringmap=newLinkedHashMapString,String();for(byte[]key:dataMap.keySet()){byte[]value=dataMap.get(key);map.put(toStr(key),toStr(value));}returnmap;}/*根据ROWKEY集合获取GET对象集合*/privatestaticListGetgetList(Listbyte[]rowList){ListGetlist=newLinkedListGet();for(byte[]row:rowList){Getget=newGet(row);get.addColumn(getBytes(family1),getBytes(column1));get.addColumn(getBytes(family1),getBytes(column2));get.addColumn(getBytes(family2),getBytes(column1));list.add(get);}returnlist;}/***封装配置的所有字段列族*/privatestaticMapbyte[],byte[]packFamilyMap(Resultresult){Mapbyte[],byte[]dataMap=null;dataMap=newLinkedHashMapbyte[],byte[]();dataMap.putAll(result.getFamilyMap(getBytes(family1)));dataMap.putAll(result.getFamilyMap(getBytes(family2)));returndataMap;}privatestaticStringtoStr(byte[]bt){returnBytes.toString(bt);}publicstaticvoidmain(String[]

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功