All Products
Search
Document Center

Tablestore:Read data whose primary key values are in the specified range

Last Updated:Jul 04, 2025

This topic describes how to use Tablestore SDK for Java to read data whose primary key values are in the specified range from Tablestore.

Prerequisites

A client is initialized. For more information, see Initialize a Tablestore client.

Method description

public GetRangeResponse getRange(GetRangeRequest getRangeRequest) throws TableStoreException, ClientException

GetRangeRequest parameters

  • rangeRowQueryCriteria (required) RangeRowQueryCriteria: The information about the range read operation, which includes the following parameters.

    Parameter

    Type

    Description

    tableName (required)

    String

    The name of the data table.

    inclusiveStartPrimaryKey (required)

    PrimaryKey

    The information about the start primary key, including the primary key column names and primary key column values.

    • The response includes the start primary key column.

    • The number and types of primary key columns that you specify must be consistent with the number and types of primary key columns of the table.

    • When data is read in the forward direction, the start primary key must be smaller than the end primary key.

    • When data is read in the backward direction, the start primary key must be greater than the end primary key.

    • PrimaryKeyValue.INF_MIN indicates the minimum value. PrimaryKeyValue.INF_MAX indicates the maximum value.

    exclusiveEndPrimaryKey (required)

    PrimaryKey

    The information about the end primary key, including the primary key column names and primary key column values.

    • The response does not include the end primary key.

    • The number and types of primary key columns that you specify must be consistent with the number and types of primary key columns of the table.

    • PrimaryKeyValue.INF_MIN indicates the minimum value. PrimaryKeyValue.INF_MAX indicates the maximum value.

    direction (optional)

    Direction

    The direction in which data is read.

    • FORWARD: reads data in the forward direction. This is the default value.

    • BACKWARD: reads data in the backward direction.

    maxVersions (optional)

    OptionalValue<Integer>

    The maximum number of versions.

    • You must specify either the maximum number of versions or the version range.

    • If the number of versions that meet the query conditions exceeds the specified maximum number of versions, the specified number of versions of data is returned in descending order of version number.

    timeRange (optional)

    OptionalValue<TimeRange>

    The range of versions to read.

    • You must specify either the maximum number of versions or the version range.

    • Each attribute column in a Tablestore data table can have multiple versions. After you specify the version range, only the data within the version range is returned.

    limit (optional)

    int

    The maximum number of rows to return in a single operation. The value must be greater than 0. If the number of rows that meet the query conditions exceeds the specified value, the specified maximum number of rows and the primary key value for the next query are returned.

    columnsToGet (optional)

    Set<String>

    The columns to read, which can be primary key columns or attribute columns.

    • If columnsToGet is not specified, the entire row is returned.

    • If columnsToGet is specified and the row does not contain any of the specified columns, the row is not returned.

    filter (optional)

    OptionalValue<Filter>

    The filter condition. For more information, see Configure a filter.

    • If both columnsToGet and filter are specified, the rows are filtered based on the conditions specified by columnsToGet, and then filtered based on the filter conditions.

Sample code

The following sample code reads data from the test_table table where the primary key value is greater than row1.

public static void getRangeExample(SyncClient client) {
    // Construct the query conditions.
    RangeRowQueryCriteria rangeRowQueryCriteria = new RangeRowQueryCriteria("test_table");
    // Specify the start primary key for the query.
    PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
    rangeRowQueryCriteria.setInclusiveStartPrimaryKey(primaryKeyBuilder.build());
    // Specify the end primary key. The result does not include the end primary key.
    primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
    rangeRowQueryCriteria.setExclusiveEndPrimaryKey(primaryKeyBuilder.build());
    // Specify the version that you want to query.
    rangeRowQueryCriteria.setMaxVersions(1);

    // Call the getRange method to query data.
    GetRangeRequest getRangeRequest = new GetRangeRequest(rangeRowQueryCriteria);
    GetRangeResponse getRangeResponse = client.getRange(getRangeRequest);

    // Return the result.
    System.out.println("RequestId: " + getRangeResponse.getRequestId());
    System.out.println("Read CU Cost: " + getRangeResponse.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit());
    System.out.println("Write CU Cost: " + getRangeResponse.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit());
    for(Row row : getRangeResponse.getRows())
        System.out.println(row);
}

The maximum amount of data that can be scanned in a single range read operation is 5,000 rows or 4 MB. If the limit is exceeded, the primary key value for the next read operation is returned. You can refer to the following code for iterative queries.

while(true) {
    GetRangeRequest getRangeRequest = new GetRangeRequest(rangeRowQueryCriteria);
    GetRangeResponse getRangeResponse = client.getRange(getRangeRequest);

    // Return the result.
    System.out.println("RequestId: " + getRangeResponse.getRequestId());
    System.out.println("Read CU Cost: " + getRangeResponse.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit());
    System.out.println("Write CU Cost: " + getRangeResponse.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit());
    for(Row row : getRangeResponse.getRows())
        System.out.println(row);
    System.out.println("Rows Count: " + getRangeResponse.getRows().size());

    // Specify the start primary key for the next read operation.
    if(getRangeResponse.getNextStartPrimaryKey() != null)
        rangeRowQueryCriteria.setInclusiveStartPrimaryKey(getRangeResponse.getNextStartPrimaryKey());
    else break;
}

You can also refer to the following sample code to configure the following settings when you query data.

  • Specify the direction in which data is read.

    // Specify that data is read in the backward direction.
    rangeRowQueryCriteria.setDirection(Direction.BACKWARD);
    // Specify the start primary key. When you read data in the backward direction, the start primary key must be greater than the end primary key.
    PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
    rangeRowQueryCriteria.setInclusiveStartPrimaryKey(primaryKeyBuilder.build());
    // Specify the end primary key. The result does not include the end primary key.
    primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
    rangeRowQueryCriteria.setExclusiveEndPrimaryKey(primaryKeyBuilder.build());
  • Specify the version range of data to read. Only data within the version range is returned.

    // Specify the version range of data to query to one day before the current time.
    rangeRowQueryCriteria.setTimeRange(new TimeRange(System.currentTimeMillis() - 86400*1000, System.currentTimeMillis()));
  • Specify the attribute columns to read.

    rangeRowQueryCriteria.addColumnsToGet("col2");
  • Specify the maximum number of rows to return in a single operation.

    rangeRowQueryCriteria.setLimit(10);

References