Batch read data

更新时间: 2026-06-22 17:08:54

Batch read rows from one or more Tablestore tables in a single request by using the Go SDK.

Usage notes

A single batch read request can return up to 100 rows.

Prerequisites

Initialize a Tablestore client

Method

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

BatchGetRowRequest parameter reference

  • MultiRowQueryCriteria (required)[]*MultiRowQueryCriteria: Query criteria that specify the tables and rows to read.

    Parameter

    Type

    Description

    TableName (required)

    string

    The name of the table to read.

    PrimaryKey (required)

    []*PrimaryKey

    The primary keys of the rows to read.

    • Supported primary key column types: STRING, INTEGER, and BINARY.

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

    MaxVersion (optional)

    int

    The maximum number of versions to return per cell.

    • Specify either this parameter or TimeRange.

    • If more versions exist than the specified limit, Tablestore returns the newest versions first.

    TimeRange (optional)

    *TimeRange

    The time range of cell versions to return.

    • Specify either this parameter or MaxVersion.

    • Each attribute column can store multiple versions. When this parameter is set, only versions within the specified range are returned.

    ColumnsToGet (optional)

    []string

    The names of the columns to return. You can specify primary key columns, attribute columns, or both.

    • If omitted, all columns in the row are returned.

    • If a row contains none of the requested columns, the result for that row is null.

    Filter (optional)

    ColumnFilter

    The filter condition. For more information, see Filters.

    • If both ColumnsToGet and Filter are set, Tablestore first retrieves the columns specified by ColumnsToGet, then applies Filter to the result. Only rows that match the filter are returned, and each matching row contains only the columns specified in ColumnsToGet.

Sample code

The following example reads two rows whose primary keys are row1 and row2 from the test_table 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 snippets show how to set optional batch read parameters.

  • To read from multiple tables in one request, add a separate 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 limit reads to a specific version range, set TimeRange. Only data within the specified range is returned.

    // 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 specific attribute columns to read.

    multiRowQueryCriteria.AddColumnToGet("col1")

Related topics

Read data within a range

上一篇: Range read 下一篇: Batch update data
阿里云首页 表格存储 相关技术圈