Este tópico descreve como ler dados em lote do Tablestore com o .NET SDK. Consulte dados de várias tabelas em uma única operação.
Observações de uso
Uma única operação de leitura em lote lê até 100 linhas.
Pré-requisitos
Assinatura do método
public BatchGetRowResponse BatchGetRow(BatchGetRowRequest request)
Método assíncrono:
public Task<BatchGetRowResponse> BatchGetRowAsync(BatchGetRowRequest request)
Exemplos
O exemplo de código a seguir mostra como ler duas linhas de dados cujos valores de chave primária são row1 e row2 da tabela test_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}");
}
Os exemplos a seguir mostram como configurar parâmetros para uma operação de leitura de dados em lote.
-
Para ler dados de várias tabelas em uma única operação de leitura em lote, especifique um MultiRowQueryCriteria para cada tabela.
// 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); -
Especifique as colunas de atributo a serem lidas.
HashSet<string> columnsToGet = new HashSet<string> { "col2" }; batchGetRowRequest.Add("test_table", primaryKeys, columnsToGet);