This topic describes how to use the .NET SDK to update a single row in a Tablestore table. You can update the values of attribute columns, add attribute columns, or delete a specific version or an entire attribute column.
Prerequisites
Method
public UpdateRowResponse UpdateRow(UpdateRowRequest request)Asynchronous method:
public Task<UpdateRowResponse> UpdateRowAsync(UpdateRowRequest request)Sample code
This example updates the row whose primary key value is row1 by changing the value of the col1 attribute column to changed_val1.
try
{
// Construct the primary key.
PrimaryKey primaryKey = new PrimaryKey
{
{ "id", new ColumnValue("row1") }
};
// Define the attribute columns to update.
UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute();
updateOfAttribute.AddAttributeColumnToPut("col1", new ColumnValue("changed_val1"));
// You must specify a condition when updating a row. (RowExistenceExpectation.IGNORE skips the row existence check.)
Condition condition = new Condition(RowExistenceExpectation.IGNORE);
// Call the UpdateRow method to update the row.
UpdateRowRequest updateRowRequest = new UpdateRowRequest("test_table", condition, primaryKey, updateOfAttribute);
UpdateRowResponse updateRowResponse = client.UpdateRow(updateRowRequest);
Console.WriteLine($"RequestId: {updateRowResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {updateRowResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {updateRowResponse.ConsumedCapacityUnit.Write}");
}
catch (Exception ex)
{
Console.WriteLine($"Update row failed, exception: {ex.Message}");
}Modify the sample code to perform the following row operations:
Add an attribute column.
updateOfAttribute.AddAttributeColumnToPut("col2", new ColumnValue("val2"));Delete an entire attribute column.
updateOfAttribute.AddAttributeColumnToDelete("col2");