批量更新数据

更新时间:
复制为 MD 格式

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

注意事项

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

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

前提条件

初始化Tablestore Client

方法说明

batchWriteRow: function batchWriteRow(params, callback)

params参数说明

  • tables(必选)Arrayitems(必选)List[TableInBatchWriteRowItem]:行数据操作列表,包含以下参数。

    名称

    类型

    说明

    tableName(必选)

    string

    数据表名称。

    rows(必选)

    Array

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

  • transactionId(可选)string:局部事务ID,用于唯一标识局部事务,详情请参见局部事务

示例代码

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

var table = {
    tableName: 'test_table',
    rows: [
        {
            type: 'PUT',
            // 写入数据时必须指定写入条件 (TableStore.RowExistenceExpectation.IGNORE,表示不做行存在性判断)
            condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
            primaryKey: [{ 'id': 'row1' }]
        }
    ]
};
var params = {
    tables: [table]
};

// 调用 batchWriteRow 方法进行批量数据查询
client.batchWriteRow(params, function (err, data) {
    if (err) {
        console.log('Batch write row failed with error: %s', err);
        return;
    }

    // 返回结果处理
    console.log('RequestId: %s', data.RequestId);
    data.tables.forEach(function (item) {
        if (!item.isOk) {
            console.log('Table name: %s. Error message: %s', item.tableName, item.errorMessage);
        }
    });
});

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

  • PutRowChange:写入行数据。

    var table = {
        tableName: 'test_table',
        rows: [
            {
                type: 'PUT',
                // 写入数据时必须指定写入条件 (TableStore.RowExistenceExpectation.IGNORE,表示不做行存在性判断)
                condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
                primaryKey: [{ 'id': 'row1' }]
            }
        ]
    };

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

    var table = {
        tableName: 'test_table',
        rows: [
            {
                type: 'PUT',
                // 写入数据时必须指定写入条件 (TableStore.RowExistenceExpectation.IGNORE,表示不做行存在性判断)
                condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
                primaryKey: [{ 'id': 'row1' }],
                attributeColumns: [
                    // 添加属性列
                    { 'col1': 'val1' }, 
                    // 添加自定义数据版本号的属性列
                    { 'col2': 'val2', 'timestamp': Date.now() }
                ]
            }
        ]
    };
  • UpdateRowChange:更新行数据,您可以修改属性列的值、添加数据列、删除属性列的某个版本或整个属性列。

    var table = {
        tableName: 'test_table',
        rows: [
            {
                type: 'UPDATE',
                // 更新数据时必须指定更新条件 (TableStore.RowExistenceExpectation.IGNORE,表示不做行存在性判断)
                condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
                primaryKey: [{ 'id': 'row1' }],
                attributeColumns: [{ 'PUT': [{ 'col1': 'changed_val1' }] }],
            }
        ]
    };

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

    var table = {
        tableName: 'test_table',
        rows: [
            {
                type: 'UPDATE',
                // 更新数据时必须指定更新条件 (TableStore.RowExistenceExpectation.IGNORE,表示不做行存在性判断)
                condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
                primaryKey: [{ 'id': 'row1' }],
                attributeColumns: [
                    {
                        'PUT':
                            [
                                // 添加属性列
                                { 'col3': 'val3' },
                                // 添加自定义数据版本号的属性列
                                { 'col4': 'val4', 'timestamp': Date.now() }
                            ]
                    },
                    {   // 删除属性列
                        'DELETE_ALL':
                            ['col2']
                    }
                ],
            }
        ]
    };
  • DeleteRowChange:删除行数据。

    var table = {
        tableName: 'test_table',
        rows: [
            {
                type: 'DELETE',
                // 删除行数据时必须指定删除条件 (TableStore.RowExistenceExpectation.IGNORE,表示不做行存在性判断)
                condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
                primaryKey: [{ 'id': 'row1' }]
            }
        ]
    };

相关文档