Exemplo n.º 1
0
  /** 封装查询条件 */
  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;
  }
Exemplo n.º 2
0
  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;
  }