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, ClientExceptionSample 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");