Use the Tablestore .NET SDK to write, update, and delete rows across multiple tables in a single batch request.
Usage notes
-
If the server detects a parameter error in any operation, it throws an exception and the entire batch request fails. No operations are executed.
-
A single batch write supports up to 200 rows with a total data size of at most 4 MB.
Prerequisites
Method
public BatchWriteRowResponse BatchWriteRow(BatchWriteRowRequest request)
Asynchronous method:
public Task<BatchWriteRowResponse> BatchWriteRowAsync(BatchWriteRowRequest request)
Code examples
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}");
}
Examples of each operation type:
-
RowPutChange: Write 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);Write a row with attribute columns:
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: Update a row. Supports modifying attribute column values, adding columns, or deleting a column version or an entire 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);Add and delete attribute columns in a single update:
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: Delete 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);