zoukankan      html  css  js  c++  java
  • 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最后添加

  • 相关阅读:
    20171104 DOI Excel 导出
    ABAP 字符串处理
    SE80 开发对象
    ABAP开发中message dump
    物料单位转换的两个函数
    ABAP 多行消息分别显示弹窗
    隐藏你写的程序代码
    学习笔记:Javascript 变量 包装对象
    访问平安银行网站时出现证书问题 NET::ERR_CERT_SYMANTEC_LEGACY
    关于跨域问题
  • 原文地址:https://www.cnblogs.com/darange/p/13692928.html
Copyright © 2011-2022 走看看