本文介紹如何通過 .NET SDK 批量讀取Table Store中的資料,支援查詢多個表的資料。
注意事項
單次批量讀取操作最多支援讀取 100 行資料。
前提條件
方法說明
public BatchGetRowResponse BatchGetRow(BatchGetRowRequest request)非同步方法呼叫:
public Task<BatchGetRowResponse> BatchGetRowAsync(BatchGetRowRequest request)範例程式碼
以下範例程式碼用於讀取 test_table 表中主索引值為 row1 和 row2 的兩行資料。
try
{
List<PrimaryKey> primaryKeys = new List<PrimaryKey>
{
// 添加第 1 行主鍵資訊
new PrimaryKey {{ "id", new ColumnValue("row1") }},
// 添加第 2 行主鍵資訊
new PrimaryKey {{ "id", new ColumnValue("row2") }},
};
// 構造查詢條件
BatchGetRowRequest batchGetRowRequest = new BatchGetRowRequest();
batchGetRowRequest.Add("test_table", primaryKeys);
// 調用 BatchGetRow 方法讀取行資料
BatchGetRowResponse batchGetRowResponse = client.BatchGetRow(batchGetRowRequest);
// 返回結果處理
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 is not exist.");
}
}
else
{
Console.WriteLine($"Failed Row: {item.ErrorMessage}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Batch get row failed, exception: {ex.Message}");
}您可以在批量讀取資料時參考以下範例程式碼進行參數設定。
讀取多張表的資料。批量讀取支援一次讀取多張表的資料,您需要為每張表指定一個MultiRowQueryCriteria。
// 構造第 2 個表的查詢條件 List<PrimaryKey> primaryKeys1 = new List<PrimaryKey> { new PrimaryKey {{ "order_id", new ColumnValue("90fb478c-1360-11f0-a34d-00163e30a2a9") }} }; batchGetRowRequest.Add("orders_small", primaryKeys1);指定讀取的屬性列。
HashSet<string> columnsToGet = new HashSet<string> { "col2" }; batchGetRowRequest.Add("test_table", primaryKeys, columnsToGet);