This topic describes how to read multiple rows of data from Tablestore by using the Java SDK. You can query data from multiple tables.
Usage notes
You can read a maximum of 100 rows of data in a single batch read operation.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
Method
public BatchGetRowResponse batchGetRow(BatchGetRowRequest batchGetRowRequest) throws TableStoreException, ClientExceptionSample code
The following sample code reads two rows of data with primary key values row1 and row2 from the test_table table.
public static void batchGetRowExample(SyncClient client) {
// Construct query conditions.
MultiRowQueryCriteria multiRowQueryCriteria = new MultiRowQueryCriteria("test_table");
// Add primary key information for the first row.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
multiRowQueryCriteria.addRow(primaryKeyBuilder.build());
// Add primary key information for the second row.
primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row2"));
multiRowQueryCriteria.addRow(primaryKeyBuilder.build());
// Specify the version to query.
multiRowQueryCriteria.setMaxVersions(1);
// Call the batchGetRow method to perform batch data query.
BatchGetRowRequest batchGetRowRequest = new BatchGetRowRequest();
batchGetRowRequest.addMultiRowQueryCriteria(multiRowQueryCriteria);
BatchGetRowResponse batchGetRowResponse = client.batchGetRow(batchGetRowRequest);
// Return the result.
System.out.println("* RequestId: " + batchGetRowResponse.getRequestId());
System.out.println("* Is all succeeded: " + batchGetRowResponse.isAllSucceed());
// Print the successfully read rows.
System.out.println("* Succeeded Rows: ");
for(BatchGetRowResponse.RowResult rowResult:batchGetRowResponse.getSucceedRows())
System.out.println(rowResult.getRow());
// Print the rows that failed to be read.
if(!batchGetRowResponse.isAllSucceed()) {
System.out.println("* Failed Rows: ");
for(BatchGetRowResponse.RowResult rowResult:batchGetRowResponse.getFailedRows())
System.out.println(rowResult.getTableName() + " | " + batchGetRowRequest.getPrimaryKey(rowResult.getTableName(), rowResult.getIndex()) + " | " + rowResult.getError());
}
}You can refer to the following sample code to configure parameters when you read data in a batch.
Read data from multiple tables. You can read data from multiple tables at a time in a batch read operation. You need to specify a MultiRowQueryCriteria object for each table.
// Construct query conditions for the second table. MultiRowQueryCriteria multiRowQueryCriteria1 = new MultiRowQueryCriteria("orders_small"); // Add primary key information for the first row. primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimaryKeyColumn("order_id", PrimaryKeyValue.fromString("90fb478c-1360-11f0-a34d-00163e30a2a9")); multiRowQueryCriteria1.addRow(primaryKeyBuilder.build()); // Specify the version to query. multiRowQueryCriteria1.setMaxVersions(1); // Add MultiRowQueryCriteria to the request. batchGetRowRequest.addMultiRowQueryCriteria(multiRowQueryCriteria1);Specify the version range to read. Only data within the version range is returned.
// Specify the version range to read data from one day ago to the current time. multiRowQueryCriteria.setTimeRange(new TimeRange(System.currentTimeMillis() - 86400*1000, System.currentTimeMillis()));Specify the attribute columns to read.
multiRowQueryCriteria.addColumnsToGet("col1");