Batch read data
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
Method
func (tableStoreClient *TableStoreClient) BatchGetRow(request *BatchGetRowRequest) (*BatchGetRowResponse, error)
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
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 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")