Tous les produits
Search
Centre de documentation

Tablestore:Batch update data

Dernière mise à jour :Aug 19, 2026

Appelez la méthode batchWriteRow du SDK PHP Tablestore pour insérer, mettre à jour et supprimer des données par lot dans plusieurs tables.

Remarques d'utilisation

  • Si le serveur détecte une erreur de paramètre dans l'une des opérations, toutes les opérations de la requête échouent.

  • Une seule opération de mise à jour par lot prend en charge jusqu'à 200 lignes. La taille totale des données de toutes les lignes ne doit pas dépasser 4 Mo.

Prérequis

Initialiser le client Tablestore

Méthode

public function batchWriteRow(array $request)

Paramètres $request

  • tables (Obligatoire) array : Opérations de ligne à effectuer. Ce paramètre contient les champs suivants.

    Nom

    Type

    Description

    table_name (Obligatoire)

    string

    Nom de la table de données.

    rows (Obligatoire)

    array

    Opérations de ligne à effectuer. Types pris en charge : write, update et delete.

  • transaction_id (Facultatif) string : ID de la transaction locale. Pour plus d'informations, consultez la rubrique Transactions locales.

Exemples

L'exemple suivant insère une ligne dans la table 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.';
}

Les exemples suivants illustrent différents types d'opérations.

  • PutRowItem : Insérer une ligne.

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

    Insérez une ligne avec des colonnes d'attribut.

    $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 : Mettre à jour une ligne en modifiant les valeurs des colonnes d'attribut, en ajoutant des colonnes d'attribut ou en supprimant une version spécifique ou toutes les versions d'une colonne d'attribut.

    $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
            )
        )
    );

    Ajoutez ou supprimez des colonnes d'attribut lors de la mise à jour d'une ligne.

    $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 : Supprimer une ligne.

    $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
            )
        )
    );

Références