全部产品
Search
文档中心

表格存储:批量更新数据

更新时间:May 11, 2026

本文介绍如何通过 .NET SDK 对表格存储的数据进行批量更新操作,包括写入数据、修改数据和删除数据,支持同时操作多个表的数据。

注意事项

  • 服务端检查到部分操作的参数错误时会抛出参数错误异常,此时该请求中的所有操作都将不执行。

  • 批量更新操作单次支持写入的最大行数为200行,且所有行的数据量总和不能超过4MB。

前提条件

初始化Tablestore Client

方法说明

public BatchWriteRowResponse BatchWriteRow(BatchWriteRowRequest request)

异步方法:

public Task<BatchWriteRowResponse> BatchWriteRowAsync(BatchWriteRowRequest request)

BatchWriteRowRequest参数说明

  • RowChangesGroupByTable(必选)IDictionary<string, RowChanges>:行数据操作列表,包含以下参数。

    名称

    类型

    说明

    tableName(必选)

    string

    数据表名称。

    rowChanges(必选)

    RowChanges

    数据操作类型,包括写入数据、更新数据和删除数据。

示例代码

以下示例代码使用批量数据操作方法在 test_table 表中插入一行数据。

try
{
    // 构造请求数据
    BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();

    // 添加 RowChanges
    RowChanges rowChanges = new RowChanges("test_table");
    PrimaryKey primaryKey = new PrimaryKey
    {
        { "id", new ColumnValue("row1") }
    };
    // 写入行数据时必须配置写入条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
    Condition condition = new Condition(RowExistenceExpectation.IGNORE);
    rowChanges.AddPut(condition, primaryKey, null);
    batchWriteRowRequest.Add("test_table", rowChanges);

    // 调用 BatchWriteRow 方法进行批量数据操作
    BatchWriteRowResponse batchWriteRowResponse = client.BatchWriteRow(batchWriteRowRequest);

    // 返回结果处理
    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}");
}

不同类型的数据操作示例代码参考如下。

  • RowPutChange:写入行数据。

    PrimaryKey primaryKey = new PrimaryKey
    {
        { "id", new ColumnValue("row1") }
    };
    // 写入行数据时必须配置写入条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
    Condition condition = new Condition(RowExistenceExpectation.IGNORE);
    rowChanges.AddPut(condition, primaryKey, null);

    写入行数据时添加属性列。

    PrimaryKey primaryKey = new PrimaryKey
    {
        { "id", new ColumnValue("row1") }
    };
    AttributeColumns columns = new AttributeColumns
    {
        { "col1", new ColumnValue("val1") }
    };
    // 写入行数据时必须配置写入条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
    Condition condition = new Condition(RowExistenceExpectation.IGNORE);
    rowChanges.AddPut(condition, primaryKey, columns);
  • RowUpdateChange:更新行数据,您可以修改属性列的值、添加数据列、删除属性列的某个版本或整个属性列。

    PrimaryKey primaryKey = new PrimaryKey
    {
        { "id", new ColumnValue("row1") }
    };
    UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute();
    updateOfAttribute.AddAttributeColumnToPut("col1", new ColumnValue("changed_val1"));
    // 更新行数据时必须配置更新条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
    Condition condition = new Condition(RowExistenceExpectation.IGNORE);
    rowChanges.AddUpdate(condition, primaryKey, updateOfAttribute);

    更新行数据时添加或删除属性列。

    PrimaryKey primaryKey = new PrimaryKey
    {
        { "id", new ColumnValue("row1") }
    };
    UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute();
    // 添加属性列
    updateOfAttribute.AddAttributeColumnToPut("col3", new ColumnValue("val3"));
    // 删除属性列
    updateOfAttribute.AddAttributeColumnToDelete("col2");
    // 更新行数据时必须配置更新条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
    Condition condition = new Condition(RowExistenceExpectation.IGNORE);
    rowChanges.AddUpdate(condition, primaryKey, updateOfAttribute);
  • RowDeleteChange:删除行数据。

    PrimaryKey primaryKey = new PrimaryKey
    {
        { "id", new ColumnValue("row1") }
    };
    // 删除行数据时必须配置删除条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
    Condition condition = new Condition(RowExistenceExpectation.IGNORE);
    rowChanges.AddDelete(condition, primaryKey);

相关文档