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