Batch read rows

Updated at:

Use the Tablestore SDK for Go to read multiple rows from one or more Wide Column model tables in one call.

Prerequisites

Install the Tablestore SDK for Go and initialize the client.

Description

Call BatchGetRow to batch read rows by specifying their complete primary keys. A call can read up to 100 rows and can read from multiple tables. Each row operation is executed independently. Check the result of each row.

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

The following sample reads the rows whose primary key values are row1 and row2 from the example_table table.

row1 := &tablestore.PrimaryKey{}
row1.AddPrimaryKeyColumn("id", "row1")
row2 := &tablestore.PrimaryKey{}
row2.AddPrimaryKeyColumn("id", "row2")

criteria := &tablestore.MultiRowQueryCriteria{
    TableName:  "example_table",
    MaxVersion: 1,
}
criteria.AddRow(row1)
criteria.AddRow(row2)

request := &tablestore.BatchGetRowRequest{
    MultiRowQueryCriteria: []*tablestore.MultiRowQueryCriteria{criteria},
}
response, err := client.BatchGetRow(request)
if err != nil {
    log.Fatal(err)
}

for _, row := range response.TableToRowsResult["example_table"] {
    fmt.Println(row.IsSucceed, row.PrimaryKey, row.Columns, row.Error)
}

Parameters

BatchGetRowRequest contains the following parameter.

Name

Type

Description

MultiRowQueryCriteria (required)

[]*MultiRowQueryCriteria

The batch read criteria for the tables. To read multiple tables, add one element for each table.

Batch read criteria

Each element in MultiRowQueryCriteria is of the MultiRowQueryCriteria type and contains the following parameters.

Name

Type

Description

TableName (required)

string

The table name.

PrimaryKey (required)

[]*PrimaryKey

The complete primary keys of the rows to read. Primary key values support the STRING, INTEGER, and BINARY types. The number, order, and types of columns in each primary key must match the primary key schema of the table.

MaxVersion (optional)

int

The maximum number of versions to return for each attribute column. At least one of MaxVersion and TimeRange is required.

If the number of matching data versions exceeds this value, the specified number of versions are returned from the latest to the earliest.

TimeRange (optional)

*TimeRange

The version time range, which specifies a start time, end time, or exact version in milliseconds. At least one of MaxVersion and TimeRange is required.

Each attribute column can have different data versions. If this parameter is specified, only data in the specified version time range is returned.

ColumnsToGet (optional)

[]string

The primary key columns or attribute columns to return. If this parameter is not specified, the entire row is returned.

If a row does not contain any specified column, the corresponding RowResult is successful but contains neither primary key columns nor attribute columns.

Filter (optional)

ColumnFilter

The attribute column filter. For more information, see Use filters.

If ColumnsToGet and Filter are both specified, Tablestore first selects columns based on ColumnsToGet and then applies Filter to the selected columns. If the filter references an attribute column that is not included in ColumnsToGet, the column is treated as missing. Whether the row is filtered out is determined by FilterIfMissing.

StartColumn (optional)

*string

The start attribute column of the name range. This column is included.

EndColumn (optional)

*string

The end attribute column of the name range. This column is excluded.

Response

BatchGetRowResponse contains the following business information.

Field

Type

Description

TableToRowsResult

map[string][]RowResult

The row results grouped by table name. Each RowResult contains the success status, error information, primary key, attribute columns, and row index in the request.

Examples

Read rows from multiple tables

The following sample reads rows from two tables in one request. Create a separate MultiRowQueryCriteria object for each table.

primaryKey1 := &tablestore.PrimaryKey{}
primaryKey1.AddPrimaryKeyColumn("id", "row1")
criteria1 := &tablestore.MultiRowQueryCriteria{TableName: "example_table", MaxVersion: 1}
criteria1.AddRow(primaryKey1)

primaryKey2 := &tablestore.PrimaryKey{}
primaryKey2.AddPrimaryKeyColumn("id", "row2")
criteria2 := &tablestore.MultiRowQueryCriteria{TableName: "example_table_2", MaxVersion: 1}
criteria2.AddRow(primaryKey2)

request := &tablestore.BatchGetRowRequest{
    MultiRowQueryCriteria: []*tablestore.MultiRowQueryCriteria{criteria1, criteria2},
}
response, err := client.BatchGetRow(request)
if err != nil {
    log.Fatal(err)
}
for tableName, rows := range response.TableToRowsResult {
    for _, row := range rows {
        fmt.Println(tableName, row.IsSucceed, row.PrimaryKey, row.Columns, row.Error)
    }
}

Read data versions in a time range

The following sample reads data versions in a specified time range. The time range is left-closed and right-open.

endTime := time.Now().UnixMilli()
startTime := endTime - int64(time.Hour/time.Millisecond)

criteria.MaxVersion = 0
criteria.TimeRange = &tablestore.TimeRange{
    Start: startTime,
    End:   endTime,
}