This topic describes how to batch read data from Tablestore using the .NET SDK. You can query data from multiple tables in a single operation.
Usage notes
A single batch read operation can read up to 100 rows.
Prerequisites
Method signature
public BatchGetRowResponse BatchGetRow(BatchGetRowRequest request)Asynchronous method:
public Task<BatchGetRowResponse> BatchGetRowAsync(BatchGetRowRequest request)Examples
The following code example shows how to read two rows of data whose primary key values are row1 and row2 from the test_table table.
try
{
List<PrimaryKey> primaryKeys = new List<PrimaryKey>
{
// Add the primary key of the first row.
new PrimaryKey {{ "id", new ColumnValue("row1") }},
// Add the primary key of the second row.
new PrimaryKey {{ "id", new ColumnValue("row2") }},
};
// Construct the query criteria.
BatchGetRowRequest batchGetRowRequest = new BatchGetRowRequest();
batchGetRowRequest.Add("test_table", primaryKeys);
// Call the BatchGetRow method to read row data.
BatchGetRowResponse batchGetRowResponse = client.BatchGetRow(batchGetRowRequest);
// Process the response.
Console.WriteLine($"* RequestId: {batchGetRowResponse.RequestID}");
Console.WriteLine($"* Is all succeeded: {batchGetRowResponse.IsAllSucceed}");
foreach (var tableGroup in batchGetRowResponse.RowDataGroupByTable)
{
Console.WriteLine($"* Table: {tableGroup.Key}");
foreach (var item in tableGroup.Value)
{
if (item.IsOK)
{
if (item.Row != null)
{
Console.WriteLine($"Succeeded Row: {item.Row}");
}
else
{
Console.WriteLine("Succeeded Row: This row does not exist.");
}
}
else
{
Console.WriteLine($"Failed Row: {item.ErrorMessage}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Batch get row failed, exception: {ex.Message}");
}The following examples show how to configure parameters for a batch read data operation.
To read data from multiple tables in a single batch read operation, you must specify a MultiRowQueryCriteria for each table.
// Construct the query criteria for the second table. List<PrimaryKey> primaryKeys1 = new List<PrimaryKey> { new PrimaryKey {{ "order_id", new ColumnValue("90fb478c-1360-11f0-a34d-00163e30a2a9") }} }; batchGetRowRequest.Add("orders_small", primaryKeys1);Specify the attribute columns to read.
HashSet<string> columnsToGet = new HashSet<string> { "col2" }; batchGetRowRequest.Add("test_table", primaryKeys, columnsToGet);