This topic describes how to use the .NET SDK to perform a batch write in Tablestore. A batch write allows you to write, update, and delete data across multiple tables in a single request.
Usage notes
If the server detects a parameter error in any of the individual operations, it throws an exception. The entire batch request fails, and no operations are executed.
A single batch write operation supports a maximum of 200 rows. The total data size of all rows cannot exceed 4 MB.
Prerequisites
Method
public BatchWriteRowResponse BatchWriteRow(BatchWriteRowRequest request)Asynchronous method:
public Task<BatchWriteRowResponse> BatchWriteRowAsync(BatchWriteRowRequest request)Code examples
The following code shows how to use a batch write operation to insert a row into the test_table 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}");
}The following code shows examples of different types of data operations.
RowPutChange: Writes a row.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);You can also add attribute columns when you write a row.
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: Updates a row. You can modify the values of attribute columns, add attribute columns, or delete a specific version of an attribute column or an entire attribute column.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);You can also add or delete attribute columns when you update a row.
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: Deletes a row.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);