Escriba, actualice y elimine filas en varias tablas de Tablestore con una sola solicitud al invocar el método batchWriteRow del SDK de Tablestore para Node.js.
Notas
Si el servidor detecta errores de parámetros en alguna operación, genera una excepción de error de parámetro y no se ejecuta ninguna operación de la solicitud.
Una sola operación de actualización por lotes admite un máximo de 200 filas y el tamaño total de los datos no puede superar los 4 MB.
Requisitos previos
Método
batchWriteRow: function batchWriteRow(params, callback)
Ejemplos de código
El siguiente ejemplo inserta una fila en la tabla test_table mediante el método de operación por lotes.
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);
}
});
});
A continuación se muestran ejemplos de distintos tipos de operaciones de datos.
-
PutRowChange: Escribe una fila.
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' }] } ] };También puede agregar columnas de atributo al escribir una fila.
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: Actualiza una fila. Permite modificar valores de columnas de atributo, agregar columnas de atributo o eliminar una versión específica o todas las versiones de una columna de atributo.
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' }] }], } ] };También puede agregar o eliminar columnas de atributo al actualizar una fila.
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: Elimina una fila.
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' }] } ] };