批量更新数据

更新时间:
复制为 MD 格式

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

注意事项

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

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

前提条件

初始化Tablestore Client

方法说明

public function batchWriteRow(array $request)

$request参数说明

  • tables(必选)array:行数据操作列表,包含以下参数。

    名称

    类型

    说明

    table_name(必选)

    string

    数据表名称。

    rows(必选)

    array

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

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

示例代码

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

$table = array (
    'table_name' => 'test_table',
    'rows' => array (
        array (
            'operation_type' => OperationTypeConst::CONST_PUT,
            'primary_key' => array ( array('id', 'row1') )
        )
    )
);
$request = array(
    'tables' => array (
        $table
    )
);

try {
    // 调用 batchWriteRow 方法进行批量数据操作
    $response = $client->batchWriteRow ($request);

     // 返回结果处理
    foreach ($response['tables'] as $tableGroup) {
        foreach ($tableGroup['rows'] as $row) {
            if (!$row['is_ok']) {
                echo "TableName: {$tableGroup['table_name']}. ErrorCode: {$row['error']['code']}. ErrorMessage: {$row['error']['message']} \n";
            }
        }
    }
} catch (Exception $e){
    echo 'Batch write row failed.';
}

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

  • PutRowItem:写入行数据。

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_PUT,
                'primary_key' => array ( array('id', 'row1') )
            )
        )
    );

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

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_PUT,
                'primary_key' => array ( array('id', 'row1') ),
                'attribute_columns' => array (
                    array('col1', 'val1'),
                    array('col2', 'val2', null, intval(microtime(true) * 1000))
                )
            )
        )
    );
  • UpdateRowItem:更新行数据,您可以修改属性列的值、添加数据列、删除属性列的某个版本或整个属性列。

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_UPDATE,
                'primary_key' => array ( array('id', 'row1') ),
                'update_of_attribute_columns'=> array(
                    'PUT' => array ( array('col1', 'changed_val1') )
                ),
                // 更新行数据时必须指定更新条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );

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

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_UPDATE,
                'primary_key' => array ( array('id', 'row1') ),
                'update_of_attribute_columns'=> array(
                    'PUT' => array ( 
                        // 添加属性列
                        array('col3', 'val3'),
                        // 添加自定义数据版本号的属性列
                        array('col4', 'val4', null, intval(microtime(true) * 1000))
                    ),
                    // 删除属性列
                    'DELETE_ALL' => array('col2')
                ),
                // 更新行数据时必须指定更新条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );
  • DeleteRowItem:删除行数据。

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_DELETE,
                'primary_key' => array ( array('id', 'row1') ),
                // 删除行数据时必须指定删除条件 (RowExistenceExpectation.IGNORE,表示不做行存在性判断)
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );

相关文档