All Products
Search
Document Center

Tablestore:Read multiple rows of data at the same time

Last Updated:Jul 08, 2025

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, ClientException

BatchGetRowRequest parameters

  • criteriasGroupByTable (required) Map<String, MultiRowQueryCriteria>: the information about the batch read operation. MultiRowQueryCriteria includes the following parameters.

    Note

    When you query data from multiple tables, one MultiRowQueryCriteria object corresponds to one table. All rows in a MultiRowQueryCriteria object use the same query criteria.

    Parameter

    Type

    Description

    tableName (required)

    String

    The name of the data table.

    rowKeys (required)

    List<PrimaryKey>

    The list of primary key columns.

    maxVersions (optional)

    OptionalValue<Integer>

    The maximum number of versions.

    • You must specify at least one of the maximum number of versions and the version range.

    • If the number of versions of data that meet the query conditions exceeds the specified maximum number of versions, data of the specified number of versions is returned from the newest to the oldest.

    timeRange (optional)

    OptionalValue<TimeRange>

    The range of versions to read.

    • You must specify at least one of the maximum number of versions and the version range.

    • Each attribute column in a Tablestore data table can have multiple versions. After you specify the version range, only data within the version range is returned.

    columnsToGet (optional)

    Set<String>

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

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

    • If columnsToGet is specified and the row does not contain any of the specified columns, null is returned.

    filter (optional)

    OptionalValue<Filter>

    The filter condition. For more information, see Configure a filter.

    • If columnsToGet and filter are both specified, rows that meet the conditions specified by columnsToGet are filtered first, and then filtered based on the filter conditions.

Sample 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");

References