All Products
Search
Document Center

Tablestore:Read a single row of data

Last Updated:Jun 30, 2025

This topic describes how to read a single row of data from a Tablestore table by using Tablestore SDK for Java.

Usage notes

When you read data from a data table, you must provide the complete primary key, including the value of the auto-increment primary key column.

Prerequisites

A client is initialized. For more information, see Initialize a Tablestore client.

Method description

public GetRowResponse getRow(GetRowRequest getRowRequest) throws TableStoreException, ClientException

GetRowRequest parameters

  • rowQueryCriteria (required) SingleRowQueryCriteria: The information about the row of data that you want to read, including the following parameters.

    Parameter

    Type

    Description

    tableName (required)

    String

    The name of the data table.

    primaryKey (required)

    PrimaryKey

    The primary key information, including the primary key column names and values.

    • The data types of primary key columns include STRING, INTEGER, and BINARY.

    • The number and types of primary key columns that you specify must be consistent with those of the table.

    maxVersions (optional)

    OptionalValue<Integer>

    The maximum number of versions.

    • You must configure at least one of the maxVersions and timeRange parameters.

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

    timeRange (optional)

    OptionalValue<TimeRange>

    The range of data versions.

    • You must configure at least one of the maxVersions and timeRange parameters.

    • Each attribute column in a Tablestore table can have different data 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.

    • When you do not configure the columnsToGet parameter, the entire row of data is returned.

    • When you configure the columnsToGet parameter 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 you configure the columnsToGet and filter parameters, the rows are filtered based on the conditions specified by the columnsToGet parameter, and then filtered based on the conditions specified by the filter parameter.

Sample code

The following sample code reads a single row of data with the primary key value of row1.

public static void getRowExample(SyncClient client) {
    // Construct the primary key.
    PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
    PrimaryKey primaryKey = primaryKeyBuilder.build();

    // Construct the row data to read.
    SingleRowQueryCriteria singleRowQueryCriteria = new SingleRowQueryCriteria("test_table", primaryKey);
    singleRowQueryCriteria.setMaxVersions(1);

    // Call the getRow method to read the row data.
    GetRowRequest getRowRequest = new GetRowRequest(singleRowQueryCriteria);
    GetRowResponse getRowResponse = client.getRow(getRowRequest);

    // Return the result.
    System.out.println("RequestId: " + getRowResponse.getRequestId());
    System.out.println("Read CU Cost: " + getRowResponse.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit());
    System.out.println("Write CU Cost: " + getRowResponse.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit());
    System.out.println("Row Data: \n" + getRowResponse.getRow());
}
  • Specify the version range of data to read. Only data within the version range is returned.

    // Set the version range of data to read to one day before the current time.
    singleRowQueryCriteria.setTimeRange(new TimeRange(System.currentTimeMillis() - 86400*1000, System.currentTimeMillis()));
  • Specify the attribute columns to read.

    singleRowQueryCriteria.addColumnsToGet("col2");

References