/** 封装查询条件 */ private FilterList packageFilters(SearchParam searchParam, boolean isPage) { FilterList filterList = null; // MUST_PASS_ALL(条件 AND) MUST_PASS_ONE(条件OR) filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); if (isPage) { filterList.addFilter(new FirstKeyOnlyFilter()); } if (StringUtils.isNotEmpty(searchParam.getHbaseFilter())) { // 将fel expression转为filter Filter userInputFilter = HbaseFilterUtil.createFilterByFelExpr( searchParam.getFamily(), searchParam.getHbaseFilter()); logger.info("[Filter for export " + searchParam.getTableName() + "] " + userInputFilter); filterList.addFilter(userInputFilter); } return filterList; }
public ModelAndView query(HttpServletRequest request, HttpServletResponse response) { ModelAndView mv = new ModelAndView(); int curPage = ServletRequestUtils.getIntParameter(request, "curPage", 0); int pageSize = ServletRequestUtils.getIntParameter(request, "pageSize", 100); if (pageSize > 10000) { throw new RuntimeException("max pageSize can not be more than 10000"); } PageBean pageBean = null; if (curPage != 0) { pageBean = new PageBean(curPage, pageSize); } String errorMsg = ""; final SearchParam searchParam = new SearchParam(); try { request.setCharacterEncoding("utf-8"); String tableName = request.getParameter("table"); String select = request.getParameter("select"); String where = request.getParameter("where"); String keys = request.getParameter("keys"); String startKey = request.getParameter("startKey"); String endKey = request.getParameter("endKey"); String family = request.getParameter("family"); String hbaseFilter = request.getParameter("hbaseFilter"); if (startKey == null) startKey = ""; if (endKey == null) endKey = ""; String tenantId = request.getParameter("tenantId"); if (startKey != null && startKey.contains("$")) { if (tenantId != null) { String[] tenantIdArray = tenantId.split(","); startKey = startKey.replaceFirst(REGEX, tenantIdArray[0]); } } if (endKey != null && endKey.contains("$")) { if (tenantId != null) { String[] tenantIdArray = tenantId.split(","); endKey = endKey.replaceFirst(REGEX, tenantIdArray[0]) + "_z"; } } searchParam.setTableName(tableName); searchParam.setFamily(family); searchParam.setStartKey(startKey); searchParam.setEndKey(endKey); searchParam.setKeys(keys); searchParam.setWhere(where); searchParam.setHbaseFilter(hbaseFilter); searchParam.setSelect(select); } catch (Exception e) { logger.error(e.getMessage(), e); errorMsg += e.getMessage(); } try { pageBean = getDataMapTemp(searchParam, curPage, pageSize); } catch (IOException e) { errorMsg += e.getMessage(); } @SuppressWarnings("unchecked") List<Map<String, String>> rows = pageBean.getResults(); List<String> titleList = new ArrayList<String>(); if (null != rows && rows.size() > 0) { for (Iterator<String> iter = rows.get(0).keySet().iterator(); iter.hasNext(); ) { String next = iter.next(); if ("_id".equals(next)) { titleList.add(0, next); } else { titleList.add(next); } } } DataSource dataSource = new DataSource(); dataSource.setRows(pageBean.getResults()); dataSource.setPage(pageBean.getCurrentPage()); dataSource.setTotal(pageBean.getTotalRecords()); dataSource.setRecords(pageBean.getLength()); dataSource.setStatus("success"); mv.addObject("status", errorMsg); mv.addObject("rows", rows); mv.addObject("keyList", titleList); mv.addObject("result", dataSource); return mv; }
private PageBean getDataMapTemp(SearchParam searchParam, Integer currentPage, Integer pageSize) throws IOException { String tableName = searchParam.getTableName(); String startRow = searchParam.getStartKey(); String stopRow = searchParam.getEndKey(); List<Map<String, String>> mapList = null; mapList = new LinkedList<Map<String, String>>(); ResultScanner scanner = null; // 为分页创建的封装类对象,下面有给出具体属性 TBData tbData = null; PageBean tbData = new PageBean(); Scan scan = null; HbaseWhereEngine whereEngine = null; HTableInterface table = HbaseTool.getTable(tableName); // 获取筛选对象 if ("ca_summary_optimize".equals(tableName) && compoundFieldMap.isEmpty()) { compoundFieldMap = CompoundFieldConfigService.getParentComField(tableName); } try { if (StringUtils.isNotEmpty(searchParam.getWhere())) { whereEngine = new HbaseWhereEngine(tableName, searchParam.getFamily(), searchParam.getWhere()); } String[] selectArray = null; if (StringUtils.isNotEmpty(searchParam.getSelect())) { selectArray = searchParam.getSelect().split(","); } byte[] cf = Bytes.toBytes(searchParam.getFamily()); // if keys is not null,then we can sure which records to be selected! if (StringUtils.isNotEmpty(searchParam.getKeys())) { List<Get> getKeysList = new ArrayList<Get>(); for (String key : searchParam.getKeys().split(",")) { Get get = new Get(Bytes.toBytes(key)); getKeysList.add(get); if (selectArray != null) { for (String field : selectArray) { String temp[] = processField(field, searchParam.getFamily()); if ("ca_summary_optimize".equals(tableName) && compoundFieldMap.containsKey(temp[1])) { get.addColumn(Bytes.toBytes(temp[0]), Bytes.toBytes(compoundFieldMap.get(temp[1]))); } else { get.addColumn(Bytes.toBytes(temp[0]), Bytes.toBytes(temp[1])); } } } if (selectArray != null && whereEngine != null) { Set<String> varSet = whereEngine.getVarSet(); for (String var : varSet) { get.addColumn(cf, Bytes.toBytes(var)); } } } Result[] resultsFromKeys = table.get(getKeysList); for (Result rr : resultsFromKeys) { if (!rr.isEmpty()) { if (whereEngine != null && !whereEngine.meetCondition(rr)) { continue; } Map<String, String> map = new TreeMap<String, String>(); map.put("_id", Bytes.toString(rr.getRow())); for (String field : selectArray) { String value = HbaseWhereEngine.getColumnValue(tableName, cf, rr, field); if (!field.equals("id")) { map.put(field, value); } } mapList.add(map); } } pageSize = mapList.size(); tbData.setCurrentPage(currentPage); tbData.setLength(pageSize); tbData.setTotalRecords(mapList.size()); } else { // if keys is null,we select some records between startKey and end key or top // pageSize ! // 获取最大返回结果数量 if (pageSize == null || pageSize == 0L) pageSize = 100; if (currentPage == null || currentPage == 0) currentPage = 1; // 计算起始页和结束页 Integer firstPage = (currentPage - 1) * pageSize; Integer endPage = firstPage + pageSize; scan = getScan(startRow, stopRow); // 给筛选对象放入过滤器(true标识分页,具体方法在下面) scan.setFilter(packageFilters(searchParam, true)); // 缓存1000条数据 scan.setCaching(1000); scan.setCacheBlocks(false); if (selectArray != null) { for (String field : selectArray) { String temp[] = processField(field, searchParam.getFamily()); if ("ca_summary_optimize".equals(tableName) && compoundFieldMap.containsKey(temp[1])) { scan.addColumn(Bytes.toBytes(temp[0]), Bytes.toBytes(compoundFieldMap.get(temp[1]))); } else { scan.addColumn(Bytes.toBytes(temp[0]), Bytes.toBytes(temp[1])); } } } if (selectArray != null && whereEngine != null) { Set<String> varSet = whereEngine.getVarSet(); for (String var : varSet) { scan.addColumn(cf, Bytes.toBytes(var)); } } scanner = table.getScanner(scan); int i = 0; List<byte[]> rowList = new LinkedList<byte[]>(); // 遍历扫描器对象, 并将需要查询出来的数据row key取出 for (Result result : scanner) { String row = toStr(result.getRow()); if (i >= firstPage && i < endPage) { // filter firstPage rowList.add(getBytes(row)); } if (i >= endPage) { break; } i++; } // 获取取出的row key的GET对象 List<Get> getList = getList(rowList, cf, selectArray, tableName); Result[] results = table.get(getList); for (Result result : results) { if (whereEngine != null && !whereEngine.meetCondition(result)) { continue; } Map<byte[], byte[]> fmap = packFamilyMap(tableName, result, cf, selectArray); Map<String, String> rmap = packRowMap(fmap); rmap.put("_id", toStr(result.getRow())); mapList.add(rmap); } // 封装分页对象 tbData.setCurrentPage(currentPage); tbData.setLength(pageSize); // (pageSize); tbData.setTotalRecords(i); } tbData.setResults(mapList); } catch (Exception e) { } finally { if (table != null) { table.close(); } closeScanner(scanner); } compoundFieldMap.clear(); return tbData; }