• HBase分页查询


    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最后添加

  • 相关阅读:
    一款基于jQuery制作的焦点图片局部场景图片切换的特效
    一款基于jQuery打造的滤镜百叶窗幻灯片特效
    css3实现多种颜色的底部阴影按钮特效
    一款简单的css3打造的一面五星红旗特效
    一款jQuery打造的具有多功能切换的幻灯片特效
    一款jquery打造的特炫酷的全屏上下滚动的jquery焦点图特效
    一款基于jquery打造的全屏切换的焦点图切换特效
    一款运用纯CSS编写的android图标
    datagrid删除确认对话框
    好站好文滙集
  • 原文地址:https://www.cnblogs.com/darange/p/13692928.html
Copyright © 2020-2023  润新知