Écrivez, mettez à jour et supprimez des lignes dans plusieurs tables Tablestore en une seule requête en appelant la méthode batchWriteRow du SDK Tablestore pour Node.js.
Notes
Si le serveur détecte des erreurs de paramètre dans l'une des opérations, il lève une exception d'erreur de paramètre et aucune des opérations de la requête n'est exécutée.
Une seule opération de mise à jour par lot prend en charge un maximum de 200 lignes, et la taille totale des données de toutes les lignes ne peut pas dépasser 4 Mo.
Prérequis
Méthode
batchWriteRow: function batchWriteRow(params, callback)
Exemples de code
L'exemple suivant insère une ligne dans la table test_table à l'aide de la méthode d'opération par lot.
var table = {
tableName: 'test_table',
rows: [
{
type: 'PUT',
// The condition for the write operation.
// TableStore.RowExistenceExpectation.IGNORE specifies that the row is written regardless of whether it already exists.
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'id': 'row1' }]
}
]
};
var params = {
tables: [table]
};
// Call the batchWriteRow method to perform the batch operation.
client.batchWriteRow(params, function (err, data) {
if (err) {
console.log('Batch write row failed with error: %s', err);
return;
}
// Process the response.
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);
}
});
});
Les exemples suivants illustrent différents types d'opérations sur les données.
-
PutRowChange : écrit une ligne.
var table = { tableName: 'test_table', rows: [ { type: 'PUT', // Specify a condition for the write operation. TableStore.RowExistenceExpectation.IGNORE specifies not to check for row existence. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }] } ] };Vous pouvez également ajouter des colonnes d'attributs lors de l'écriture d'une ligne.
var table = { tableName: 'test_table', rows: [ { type: 'PUT', // Specify a condition for the write operation. TableStore.RowExistenceExpectation.IGNORE specifies not to check for row existence. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }], attributeColumns: [ // Add an attribute column. { 'col1': 'val1' }, // Add an attribute column with a custom data version number. { 'col2': 'val2', 'timestamp': Date.now() } ] } ] }; -
UpdateRowChange : met à jour une ligne. Vous pouvez modifier les valeurs des colonnes d'attributs, ajouter des colonnes d'attributs ou supprimer une version spécifique ou toutes les versions d'une colonne d'attribut.
var table = { tableName: 'test_table', rows: [ { type: 'UPDATE', // Specify a condition for the update operation. TableStore.RowExistenceExpectation.IGNORE specifies not to check for row existence. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }], attributeColumns: [{ 'PUT': [{ 'col1': 'changed_val1' }] }], } ] };Vous pouvez également ajouter ou supprimer des colonnes d'attributs lors de la mise à jour d'une ligne.
var table = { tableName: 'test_table', rows: [ { type: 'UPDATE', // Specify a condition for the update operation. TableStore.RowExistenceExpectation.IGNORE specifies not to check for row existence. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }], attributeColumns: [ { 'PUT': [ // Add an attribute column. { 'col3': 'val3' }, // Add an attribute column with a custom data version number. { 'col4': 'val4', 'timestamp': Date.now() } ] }, { // Delete an attribute column. 'DELETE_ALL': ['col2'] } ], } ] }; -
DeleteRowChange : supprime une ligne.
var table = { tableName: 'test_table', rows: [ { type: 'DELETE', // Specify a condition for the delete operation. TableStore.RowExistenceExpectation.IGNORE specifies not to check for row existence. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }] } ] };