Use o Tablestore .NET SDK para gravar, atualizar e excluir linhas em várias tabelas em uma única solicitação em lote.
Observações de uso
Se o servidor detectar um erro de parâmetro em qualquer operação, ele lançará uma exceção e toda a solicitação em lote falhará. O sistema não executará nenhuma operação.
Uma única gravação em lote aceita até 200 linhas, com tamanho total de dados limitado a 4 MB.
Pré-requisitos
Método
public BatchWriteRowResponse BatchWriteRow(BatchWriteRowRequest request)
Método assíncrono:
public Task<BatchWriteRowResponse> BatchWriteRowAsync(BatchWriteRowRequest request)
Exemplos de código
Insira uma linha na tabela test_table:
try
{
// Construct the request.
BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
// Add RowChanges.
RowChanges rowChanges = new RowChanges("test_table");
PrimaryKey primaryKey = new PrimaryKey
{
{ "id", new ColumnValue("row1") }
};
// A condition for the write operation is required. `RowExistenceExpectation.IGNORE` specifies to skip the row existence check.
Condition condition = new Condition(RowExistenceExpectation.IGNORE);
rowChanges.AddPut(condition, primaryKey, null);
batchWriteRowRequest.Add("test_table", rowChanges);
// Call the BatchWriteRow method to perform the batch write.
BatchWriteRowResponse batchWriteRowResponse = client.BatchWriteRow(batchWriteRowRequest);
// Process the response.
Console.WriteLine($"RequestId: {batchWriteRowResponse.RequestID}");
if (!batchWriteRowResponse.IsAllSucceed)
{
foreach (var item in batchWriteRowResponse.GetFailedRows())
{
Console.WriteLine($"Table name: {item.TableName}. Error message: {item.ErrorMessage}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Batch write row failed, exception: {ex.Message}");
}
Exemplos de cada tipo de operação:
-
RowPutChange: Gravar uma linha.PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; // A condition for the write operation is required. `RowExistenceExpectation.IGNORE` specifies to skip the row existence check. Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddPut(condition, primaryKey, null);Grave uma linha com colunas de atributo:
PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; AttributeColumns columns = new AttributeColumns { { "col1", new ColumnValue("val1") } }; // A condition for the write operation is required. `RowExistenceExpectation.IGNORE` specifies to skip the row existence check. Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddPut(condition, primaryKey, columns); -
RowUpdateChange: Atualizar uma linha. Permite modificar valores de colunas de atributo, adicionar colunas ou excluir uma versão específica ou a coluna inteira.PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute(); updateOfAttribute.AddAttributeColumnToPut("col1", new ColumnValue("changed_val1")); // A condition for the update operation is required. `RowExistenceExpectation.IGNORE` specifies to skip the row existence check. Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddUpdate(condition, primaryKey, updateOfAttribute);Adicione e exclua colunas de atributo em uma única atualização:
PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute(); // Add an attribute column. updateOfAttribute.AddAttributeColumnToPut("col3", new ColumnValue("val3")); // Delete an attribute column. updateOfAttribute.AddAttributeColumnToDelete("col2"); // A condition for the update operation is required. `RowExistenceExpectation.IGNORE` specifies to skip the row existence check. Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddUpdate(condition, primaryKey, updateOfAttribute); -
RowDeleteChange: Excluir uma linha.PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; // A condition for the delete operation is required. `RowExistenceExpectation.IGNORE` specifies to skip the row existence check. Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddDelete(condition, primaryKey);