All Products
Search
Document Center

Tablestore:Read data by using an iterator

Last Updated:Jul 02, 2025

This topic describes how to use Tablestore SDK for Java to read data from Tablestore by using an iterator.

Prerequisites

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

Method description

public Iterator<Row> createRangeIterator(RangeIteratorParameter rangeIteratorParameter) throws TableStoreException, ClientException

RangeIteratorParameter parameters

Parameter

Type

Description

tableName (required)

String

The name of the data table.

inclusiveStartPrimaryKey (required)

PrimaryKey

The start primary key information.

  • The returned data includes the start primary key.

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

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

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

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

exclusiveEndPrimaryKey (required)

PrimaryKey

The end primary key information.

  • The returned data does not include the end primary key.

  • The number and types of primary key columns that you specify must be consistent with those 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 forward direction. This is the default value.

  • BACKWARD: reads data in 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, data of the specified number of versions 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 table can have multiple versions. After you specify the version range, only data within this range is returned.

maxCount (optional)

int

The maximum number of rows to read by using the iterator. The value must be greater than 0.

bufferSize (optional)

int

The buffer size. The value must be greater than 0. If the number of rows that meet the query conditions exceeds the specified buffer size, each query returns the specified number of rows and the start primary key of the next row to read.

columnsToGet (optional)

Set<String>

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

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

  • If columnsToGet is specified and a 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 the columnsToGet and filter parameters are both specified, the rows that meet the query conditions are filtered based on the columnsToGet parameter, and then filtered based on the filter conditions.

Sample code

The following sample code demonstrates how to use an iterator to read data from the test_table table starting from the primary key value row1.

public static void createRangeIteratorExample(SyncClient client) {
    // Construct the iterative query condition.
    RangeIteratorParameter rangeIteratorParameter = new RangeIteratorParameter("test_table");
    // Specify the start primary key for the iterative query.
    PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
    rangeIteratorParameter.setInclusiveStartPrimaryKey(primaryKeyBuilder.build());
    // Specify the end primary key for the iterative query. The result does not include the end primary key.
    primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
    rangeIteratorParameter.setExclusiveEndPrimaryKey(primaryKeyBuilder.build());
    // Specify the version to query.
    rangeIteratorParameter.setMaxVersions(1);

    // Call createRangeIterator to obtain the iterator.
    Iterator<Row> iterator = client.createRangeIterator(rangeIteratorParameter);
    while(iterator.hasNext()) {
        Row row = iterator.next();
        System.out.println(row);
    }
}

You can refer to the following sample code to configure parameters when you read data by using an iterator.

  • Specify the direction in which data is read.

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

    // Set the version range to query data from one day ago to the current time.
    rangeIteratorParameter.setTimeRange(new TimeRange(System.currentTimeMillis() - 86400*1000, System.currentTimeMillis()));
  • Specify the maximum number of rows to read by using an iterator.

    rangeIteratorParameter.setMaxCount(20);
  • Specify the buffer size.

    rangeIteratorParameter.setBufferSize(5);
  • Specify the attribute columns to read.

    rangeIteratorParameter.addColumnsToGet("col2");

References