All Products
Search
Document Center

Tablestore:Batch read data

Last Updated:Apr 01, 2026

This topic describes how to use the Go SDK to batch read data from Tablestore. You can read data from multiple tables in a single request.

Usage notes

A single batch read operation can read up to 100 rows.

Prerequisites

Initialize a Tablestore client

Method

func (tableStoreClient *TableStoreClient) BatchGetRow(request *BatchGetRowRequest) (*BatchGetRowResponse, error)

BatchGetRowRequest parameters

  • MultiRowQueryCriteria (required)[]*MultiRowQueryCriteria: The criteria for the batch read operation.

    Parameter

    Type

    Description

    TableName (required)

    string

    The table name.

    PrimaryKey (required)

    []*PrimaryKey

    A list of primary keys.

    • The data type of a primary key column can be STRING, INTEGER, or BINARY.

    • The number and data types of the primary key columns must match the table's schema.

    MaxVersion (optional)

    int

    The maximum number of versions to return.

    • You must specify either this parameter or TimeRange.

    • If the number of matching versions exceeds this value, Tablestore returns the specified number of latest versions in descending order of timestamp.

    TimeRange (optional)

    *TimeRange

    The version range.

    • You must specify either this parameter or MaxVersion.

    • Each attribute column in a Tablestore table can have multiple versions. If you specify a version range, Tablestore returns only data within that range.

    ColumnsToGet (optional)

    []string

    The columns to retrieve. You can specify primary key columns or attribute columns.

    • If you do not specify this parameter, Tablestore returns the entire row.

    • If a row does not contain any of the specified columns, the response for that row contains its primary key but no attribute columns.

    Filter (optional)

    ColumnFilter

    The filter condition. For more information, see Filters.

    • If you specify both ColumnsToGet and Filter, the filter is applied to the row. For rows that match the filter, Tablestore returns only the columns specified in ColumnsToGet.

Sample code

The following code shows how to read two rows with the primary key values row1 and row2 from a table named test_table.

func BatchGetRowSample(client *tablestore.TableStoreClient) {
    // Construct the query criteria.
    multiRowQueryCriteria := new(tablestore.MultiRowQueryCriteria)
    // Set the table name.
    multiRowQueryCriteria.TableName = "test_table"
    // Add the primary key for the first row.
    getPk1 := new(tablestore.PrimaryKey)
    getPk1.AddPrimaryKeyColumn("id", "row1")
    multiRowQueryCriteria.AddRow(getPk1)
    // Add the primary key for the second row.
    getPk2 := new(tablestore.PrimaryKey)
    getPk2.AddPrimaryKeyColumn("id", "row2")
    multiRowQueryCriteria.AddRow(getPk2)
    // Set the maximum number of versions to return.
    multiRowQueryCriteria.MaxVersion = 1

    // Call the BatchGetRow method.
    batchGetRowRequest := new(tablestore.BatchGetRowRequest)
    batchGetRowRequest.MultiRowQueryCriteria = append(batchGetRowRequest.MultiRowQueryCriteria, multiRowQueryCriteria)
    batchGetRowResponse, err := client.BatchGetRow(batchGetRowRequest)

    // Process the response.
    if err != nil {
        fmt.Println("Batch get row failed with error: ", err)
    } else {
        fmt.Printf("* RequestId: %s \n", batchGetRowResponse.RequestId)
        for tableName, rows := range batchGetRowResponse.TableToRowsResult {
            fmt.Printf("* Table: %s \n", tableName)
            for _, row := range rows {
                if row.IsSucceed {
                    fmt.Printf("Succeeded Row: %v \n", row)
                } else {
                    fmt.Printf("Failed Row: %d | %v \n", row.Index, row.Error)
                }
            }
        }
    }
}

The following code snippets show how to configure parameters for batch read operations.

  • To read data from multiple tables in a single request, add a MultiRowQueryCriteria object for each table.

    // Construct the query criteria for the second table.
    multiRowQueryCriteria1 := new(tablestore.MultiRowQueryCriteria)
    multiRowQueryCriteria1.TableName = "orders_small"
    // Add the primary key for the first row.
    getPk3 := new(tablestore.PrimaryKey)
    getPk3.AddPrimaryKeyColumn("order_id", "90fb478c-1360-11f0-a34d-00163e30a2a9")
    multiRowQueryCriteria1.AddRow(getPk3)
    // Set the maximum number of versions to return.
    multiRowQueryCriteria1.MaxVersion = 1
    // Add the MultiRowQueryCriteria object to the request.
    batchGetRowRequest.MultiRowQueryCriteria = append(batchGetRowRequest.MultiRowQueryCriteria, multiRowQueryCriteria1)
  • To read data within a specific version range, set the TimeRange parameter. The operation returns only data within this range.

    // Set the version range to the last 24 hours.
    timeRange := new(tablestore.TimeRange)
    timeRange.Start = int64(time.Now().Unix() * 1000 - 86400 * 1000) 
    timeRange.End = int64(time.Now().Unix() * 1000) 
    multiRowQueryCriteria.TimeRange = timeRange
  • Specify the attribute columns to read.

    multiRowQueryCriteria.AddColumnToGet("col1")

Related documents

Read data within a range