Topik ini menjelaskan cara membaca data secara batch dari Tablestore menggunakan .NET SDK. Anda dapat mengkueri data dari beberapa tabel dalam satu operasi.
Usage notes
Satu operasi batch read dapat membaca hingga 100 baris.
Prasyarat
Method signature
public BatchGetRowResponse BatchGetRow(BatchGetRowRequest request)Metode asinkron:
public Task<BatchGetRowResponse> BatchGetRowAsync(BatchGetRowRequest request)Contoh
Contoh kode berikut menunjukkan cara membaca dua baris data dengan nilai primary key row1 dan row2 dari tabel test_table.
try
{
List<PrimaryKey> primaryKeys = new List<PrimaryKey>
{
// Tambahkan primary key baris pertama.
new PrimaryKey {{ "id", new ColumnValue("row1") }},
// Tambahkan primary key baris kedua.
new PrimaryKey {{ "id", new ColumnValue("row2") }},
};
// Bangun kriteria kueri.
BatchGetRowRequest batchGetRowRequest = new BatchGetRowRequest();
batchGetRowRequest.Add("test_table", primaryKeys);
// Panggil metode BatchGetRow untuk membaca data baris.
BatchGetRowResponse batchGetRowResponse = client.BatchGetRow(batchGetRowRequest);
// Proses respons.
Console.WriteLine($"* RequestId: {batchGetRowResponse.RequestID}");
Console.WriteLine($"* Apakah semua berhasil: {batchGetRowResponse.IsAllSucceed}");
foreach (var tableGroup in batchGetRowResponse.RowDataGroupByTable)
{
Console.WriteLine($"* Tabel: {tableGroup.Key}");
foreach (var item in tableGroup.Value)
{
if (item.IsOK)
{
if (item.Row != null)
{
Console.WriteLine($"Baris Berhasil: {item.Row}");
}
else
{
Console.WriteLine("Baris Berhasil: Baris ini tidak ada.");
}
}
else
{
Console.WriteLine($"Baris Gagal: {item.ErrorMessage}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Batch get row gagal, exception: {ex.Message}");
}Contoh berikut menunjukkan cara mengonfigurasi parameter untuk operasi batch read data.
Untuk membaca data dari beberapa tabel dalam satu operasi batch read, Anda harus menentukan MultiRowQueryCriteria untuk setiap tabel.
// Bangun kriteria kueri untuk tabel kedua. List<PrimaryKey> primaryKeys1 = new List<PrimaryKey> { new PrimaryKey {{ "order_id", new ColumnValue("90fb478c-1360-11f0-a34d-00163e30a2a9") }} }; batchGetRowRequest.Add("orders_small", primaryKeys1);Tentukan kolom atribut yang akan dibaca.
HashSet<string> columnsToGet = new HashSet<string> { "col2" }; batchGetRowRequest.Add("test_table", primaryKeys, columnsToGet);