このトピックでは、.NET SDK を使用して Tablestore からデータをバッチで読み取る方法について説明します。1 回の操作で複数のテーブルからデータをクエリできます。
使用上の注意
1 回のバッチ読み取り操作で、最大 100 行まで読み取ることができます。
前提条件
メソッドシグネチャ
public BatchGetRowResponse BatchGetRow(BatchGetRowRequest request)非同期メソッド:
public Task<BatchGetRowResponse> BatchGetRowAsync(BatchGetRowRequest request)例
次のコード例は、test_table テーブルから、プライマリキー値が row1 と row2 の 2 行のデータを読み取る方法を示しています。
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 does not exist.");
}
}
else
{
Console.WriteLine($"Failed Row: {item.ErrorMessage}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Batch get row failed, exception: {ex.Message}");
}次の例は、バッチ読み取り操作のパラメーターを設定する方法を示しています。
1 回のバッチ読み取り操作で複数のテーブルからデータを読み取るには、各テーブルに対して 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);