更新时间:2024-04-29 GMT+08:00

分页查询

功能介绍

先执行query接口返回简要数据信息,而后调用listRows接口翻页。

样例代码

public void testPagingQuery() {
  LOG.info("Entering testPagingQuery.");

  try (Table table = conn.getTable(tableName)) {
    // Using Table instance to create LemonTable.
    LemonTable lemonTable = new LemonTable(table);
    // Build LemonQuery.
    LemonQuery query = LemonQuery.builder()
      // Set ad-hoc query condition.
      .setQuery("education:bachelor OR education:master")
      // Set how many rows should be cached on client for the initial request.
      .setCaching(10)
      // Set return column family/columns.
      .addFamily(FAM_M)
      .addColumn(FAM_N, QUA_N)
      // Set return return result just contains rowkeys, no any qualifier
      // the CF of LemonConstants.EMPTY_COLUMN_RETURN can be a random existing CF
      //.addColumn(FAM_M, LemonConstants.EMPTY_COLUMN_RETURN)
      .build();
    ResultSet resultSet = lemonTable.query(query);
    // Read result rows.
    int count = resultSet.getCount();
    LOG.info("the entity count of query is " + count);

    // Read result page by page, every page show 10 lines data
    int maxPage = 100;
    final int lineNumPerPage = 5;
    for (int i = 0; i < maxPage; i++) {
      int start = lineNumPerPage * i;
      List<EntityEntry> entries = resultSet.listRows(start, lineNumPerPage);
      if (entries == null || entries.size() == 0)
      {
        break;
      }

      LOG.info("page " + (i + 1) + " count is " + entries.size() + ", result is following:");
      for (EntityEntry entry : entries) {
        Map<String, Map<String, String>> fams = entry.getColumns();
        for (Map.Entry<String, Map<String, String>> familyEntry : fams.entrySet()) {
          String family = familyEntry.getKey();
          Map<String, String> qualifiers = familyEntry.getValue();
          for (Map.Entry<String, String> qualifier : qualifiers.entrySet()) {
            String Qua = qualifier.getKey();
            String value = qualifier.getValue();
            LOG.info("rowkey is " + Bytes.toString(entry.getRow()) + ", qualifier is "
              + family + ":" + Qua + ", value is " + value);
          }
        }
      }
    }
  } catch (IOException e) {
    LOG.error("testPagingQuery failed ", e);
  }

  LOG.info("Exiting testPagingQuery.");
}