Read consecutive rows from a data table within a primary key range with Tablestore SDK for Java, with support for forward and reverse scans, version-range filtering, and column-value filtering.
Prerequisites
Install Tablestore SDK for Java and initialize a SyncClient (or AsyncClient) before calling getRange.
Feature description
public GetRangeResponse getRange(GetRangeRequest getRangeRequest) throws TableStoreException, ClientException
Scans consecutive rows from a data table within a primary key range. Specify the start and end primary keys in RangeRowQueryCriteria, then retrieve the returned rows from response.getRows().
The following example performs a forward scan on get_range_demo, reading all rows with a primary key greater than or equal to row1 and returning only the latest version of each column.
A single range scan returns at most 5,000 rows or 4 MB. When the limit is reached, the response sets nextStartPrimaryKey to the start primary key of the next page. Call getRange in a loop to read the remaining data.
String tableName = "get_range_demo";
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria(tableName);
// Start primary key (inclusive)
PrimaryKeyBuilder startPkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
startPkBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
criteria.setInclusiveStartPrimaryKey(startPkBuilder.build());
// End primary key (exclusive); INF_MAX denotes positive infinity
PrimaryKeyBuilder endPkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
endPkBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
criteria.setExclusiveEndPrimaryKey(endPkBuilder.build());
criteria.setMaxVersions(1);
GetRangeResponse response = client.getRange(new GetRangeRequest(criteria));
System.out.println("RequestId: " + response.getRequestId());
System.out.println("Read CU: " + response.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit());
for (Row row : response.getRows()) {
System.out.println(row);
}
Parameters
Pass the query conditions through RangeRowQueryCriteria. The parameters are described below.
|
Name |
Type |
Description |
|
tableName (required) |
String |
The name of the data table to read from. |
|
inclusiveStartPrimaryKey (required) |
PrimaryKey |
The start primary key, with the primary key column names and values.
|
|
exclusiveEndPrimaryKey (required) |
PrimaryKey |
The end primary key, with the primary key column names and values.
|
|
direction (optional) |
Direction |
The scan direction.
|
|
maxVersions (one of two) |
OptionalValue<Integer> |
The maximum number of versions. Required if If the data has more versions than |
|
timeRange (one of two) |
OptionalValue<TimeRange> |
The version range. Required if Attribute columns can store multiple versions. When you set a version range, Tablestore returns only versions within that range. |
|
limit (optional) |
int |
The maximum number of rows returned per call. The value must be greater than 0. If the matching data exceeds |
|
columnsToGet (optional) |
Set<String> |
The columns to read. Accepts primary key columns or attribute columns.
|
|
filter (optional) |
OptionalValue<Filter> |
The filter condition. For details, see Filter. If you set both |
Scenario examples
Paginated iteration
nextStartPrimaryKey in the response is the start primary key of the next page. Call getRange in a loop until nextStartPrimaryKey is null to scan all matching rows.
String tableName = "get_range_demo";
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria(tableName);
PrimaryKeyBuilder startPkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
startPkBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MIN);
criteria.setInclusiveStartPrimaryKey(startPkBuilder.build());
PrimaryKeyBuilder endPkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
endPkBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
criteria.setExclusiveEndPrimaryKey(endPkBuilder.build());
criteria.setMaxVersions(1);
int totalRows = 0;
while (true) {
GetRangeResponse response = client.getRange(new GetRangeRequest(criteria));
totalRows += response.getRows().size();
// The current response did not return all rows; use nextStartPrimaryKey to fetch the next page.
PrimaryKey nextStart = response.getNextStartPrimaryKey();
if (nextStart == null) {
break;
}
criteria.setInclusiveStartPrimaryKey(nextStart);
}
System.out.println("Total rows scanned: " + totalRows);
Reverse scan
Use setDirection(Direction.BACKWARD) to scan in reverse. The start primary key must then be greater than the end primary key.
String tableName = "get_range_demo";
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria(tableName);
criteria.setDirection(Direction.BACKWARD);
// For a reverse scan, the start primary key must be greater than the end primary key
PrimaryKeyBuilder startPkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
startPkBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
criteria.setInclusiveStartPrimaryKey(startPkBuilder.build());
PrimaryKeyBuilder endPkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
endPkBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
criteria.setExclusiveEndPrimaryKey(endPkBuilder.build());
criteria.setMaxVersions(1);
GetRangeResponse response = client.getRange(new GetRangeRequest(criteria));
System.out.println("Rows (backward): " + response.getRows().size());
Conditional filtering
Use setFilter to return only rows that match a column-value condition.
String tableName = "get_range_demo";
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria(tableName);
PrimaryKeyBuilder startPkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
startPkBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MIN);
criteria.setInclusiveStartPrimaryKey(startPkBuilder.build());
PrimaryKeyBuilder endPkBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
endPkBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
criteria.setExclusiveEndPrimaryKey(endPkBuilder.build());
criteria.setMaxVersions(1);
// Return only rows where col1 equals "val1"
SingleColumnValueFilter filter = new SingleColumnValueFilter(
"col1",
SingleColumnValueFilter.CompareOperator.EQUAL,
ColumnValue.fromString("val1"));
filter.setPassIfMissing(false);
criteria.setFilter(filter);
GetRangeResponse response = client.getRange(new GetRangeRequest(criteria));
System.out.println("Rows (filtered): " + response.getRows().size());