更新时间:2024-11-28 GMT+08:00

抽样查询

功能介绍

普通查询的基础上设置setSampling(),查询时从索引表中随机选择一个分片执行查询任务。

可以参考以下样例代码。

样例代码

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

  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)
      // sampling query will be select one random shard/region to query
      .setSampling()
      // 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);

    List<EntityEntry> entries = resultSet.listRows();
    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("testSamplingQuery failed ", e);
  }

  LOG.info("Exiting testSamplingQuery.");
  LOG.info("");
}
public void testSamplingQuery() {
  LOG.info("Entering testSamplingQuery.");

  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)
      // sampling query will be select one random shard/region to query
      .setSampling()
      // 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);

    List<EntityEntry> entries = resultSet.listRows();
    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("testSamplingQuery failed ", e);
  }

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