Updated on 2024-11-28 GMT+08:00

Sampling Query

Function Description

Set setSampling() in addition to the settings in common queries. During query, a shard is randomly selected from an index table to execute a query job.

Below is an example of the code implementation.

Sample Code

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("");
}