Write, update, and delete rows across multiple Tablestore tables in a single request by calling the batchWriteRow method of the Tablestore SDK for Node.js.
Notes
-
If the server detects parameter errors in any operation, it throws a parameter error exception and none of the operations in the request are executed.
-
A single batch update operation supports a maximum of 200 rows, and the total data size of all rows cannot exceed 4 MB.
Prerequisites
Method
batchWriteRow: function batchWriteRow(params, callback)
Code examples
The following example inserts a row into the test_table table by using the batch operation method.
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);
}
});
});
The following 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' }] } ] };You can also add attribute columns when writing 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 or all versions of an 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' }] }], } ] };You can also add or delete attribute columns when updating 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' }] } ] };