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
Méthode
public function batchWriteRow(array $request)
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 ) ) );