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
Method
func (tableStoreClient *TableStoreClient) BatchGetRow(request *BatchGetRowRequest) (*BatchGetRowResponse, error)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
MultiRowQueryCriteriaobject 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
TimeRangeparameter. 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 = timeRangeSpecify the attribute columns to read.
multiRowQueryCriteria.AddColumnToGet("col1")