Write, update, and delete rows across multiple Tablestore tables in a single request by using the `batchWriteRow` method in the Node.js SDK.
Notes
-
If the server detects parameter errors for an operation, it returns a parameter error exception. In this case, none of the operations in the request are executed.
-
A single batch update operation supports a maximum of 200 rows. The total data volume of all rows cannot exceed 4 MB.
Prerequisites
Method
batchWriteRow: function batchWriteRow(params, callback)
Code examples
The following code example shows how to use the batch operation method to insert a row into the test_table table.
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' }]
}
]
};
var params = {
tables: [table]
};
// Call the batchWriteRow method to perform batch data operations.
client.batchWriteRow(params, function (err, data) {
if (err) {
console.log('Batch write row failed with error: %s', err);
return;
}
// Process the returned result.
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);
}
});
});
The following code examples show different types of data operations.
-
PutRowChange: Writes a row.
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' }] } ] };Also add attribute columns when you write a row.
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: Updates a row. You can modify attribute column values, add attribute columns, or delete a specific version of an attribute column or an entire attribute column.
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' }] }], } ] };Also add or delete attribute columns when you update a row.
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: Deletes a row.
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' }] } ] };