Todos os produtos
Search
Central de documentação

Tablestore:Atualização de dados em lote

Última atualização: Jun 30, 2026

Chame batchWriteRow no Tablestore PHP SDK para inserir, atualize e exclua dados em lote em várias tabelas.

Observações de uso

  • Se o servidor detectar um erro de parâmetro em qualquer operação, todas as operações da solicitação falharão.

  • Uma única operação de atualização em lote aceita até 200 linhas. O tamanho total dos dados de todas as linhas não deve exceder 4 MB.

Pré-requisitos

Inicializar o cliente do Tablestore

Método

public function batchWriteRow(array $request)

Parâmetros de $request

  • tables (Obrigatório) array: Operações de linha a executar. Este parâmetro contém os seguintes campos.

    Nome

    Tipo

    Descrição

    table_name (Obrigatório)

    string

    Nome da tabela de dados.

    rows (Obrigatório)

    array

    Operações de linha a executar. Tipos compatíveis: escrita, atualização e exclusão.

  • transaction_id (Opcional) string: ID da transação local. Para mais informações, consulte Transações locais.

Exemplos

O exemplo a seguir insere uma linha na tabela 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 {
    // Call the batchWriteRow method to perform batch data operations.
    $response = $client->batchWriteRow ($request);

     // Process the response.
    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.';
}

Os exemplos a seguir mostram diferentes tipos de operação.

  • PutRowItem: Insere uma linha.

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

    Insira uma linha com colunas de atributo.

    $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: Atualiza uma linha ao modificar valores de colunas de atributo, adicionar colunas de atributo ou exclua uma versão específica ou todas as versões de uma coluna de atributo.

    $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') )
                ),
                // A condition is required to update a row. RowExistenceExpectation.IGNORE skips the row existence check.
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );

    Adicione ou exclua colunas de atributo ao atualize uma linha.

    $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 ( 
                        // Add an attribute column.
                        array('col3', 'val3'),
                        // Add an attribute column with a custom version number.
                        array('col4', 'val4', null, intval(microtime(true) * 1000))
                    ),
                    // Delete an attribute column.
                    'DELETE_ALL' => array('col2')
                ),
                // A condition is required to update a row. RowExistenceExpectation.IGNORE skips the row existence check.
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );
  • DeleteRowItem: Exclui uma linha.

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_DELETE,
                'primary_key' => array ( array('id', 'row1') ),
                // A condition is required to delete a row. RowExistenceExpectation.IGNORE skips the row existence check.
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );

Referências