Grave, atualize e exclua linhas em várias tabelas do Tablestore em uma única solicitação ao chamar o método batchWriteRow do Tablestore SDK for Node.js.
Observações
Se o servidor detectar erros de parâmetro em qualquer operação, ele gerará uma exceção de erro de parâmetro e nenhuma operação da solicitação será executada.
Uma única operação de atualização em lote aceita no máximo 200 linhas, e o tamanho total dos dados de todas as linhas não pode exceder 4 MB.
Pré-requisitos
Método
batchWriteRow: function batchWriteRow(params, callback)
Exemplos de código
O exemplo a seguir insere uma linha na tabela test_table usando o método de operação em lote.
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);
}
});
});
Os exemplos a seguir mostram diferentes tipos de operações de dados.
-
PutRowChange: Grava uma linha.
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' }] } ] };Também é possível adicionar colunas de atributo ao gravar uma linha.
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: Atualiza uma linha. Permite modificar valores de colunas de atributo, adicionar colunas de atributo ou excluir uma versão específica ou todas as versões de uma coluna 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' }] }], } ] };Ao atualizar uma linha, também é possível adicionar ou excluir colunas 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': [ // 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: Exclui uma linha.
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' }] } ] };