Batch read rows
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) |
|
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) |
|
The table name. |
|
PrimaryKey (required) |
|
The complete primary keys of the rows to read. Primary key values support the |
|
MaxVersion (optional) |
|
The maximum number of versions to return for each attribute column. At least one of 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) |
|
The version time range, which specifies a start time, end time, or exact version in milliseconds. At least one of 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) |
|
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 |
|
Filter (optional) |
|
The attribute column filter. For more information, see Use filters. If |
|
StartColumn (optional) |
|
The start attribute column of the name range. This column is included. |
|
EndColumn (optional) |
|
The end attribute column of the name range. This column is excluded. |
Response
BatchGetRowResponse contains the following business information.
|
Field |
Type |
Description |
|
|
|
The row results grouped by table name. Each |
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,
}