Node.js SDK の `batchWriteRow` メソッドを使用すると、単一のリクエストで複数の Tablestore テーブルにわたる行の書き込み、更新、削除を行うことができます。
注意事項
-
サーバーが操作のパラメーターエラーを検出した場合、パラメーターエラー例外が返されます。この場合、リクエスト内のどの操作も実行されません。
-
単一のバッチ更新操作は、最大 200 行をサポートします。すべての行の総データ量は 4 MB を超えることはできません。
前提条件
メソッド
batchWriteRow: function batchWriteRow(params, callback)
コード例
以下のコード例は、バッチ操作メソッドを使用して `test_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);
}
});
});
以下のコード例は、異なるタイプのデータ操作を示しています。
-
PutRowChange: 行を書き込みます。
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 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: 行を更新します。属性列の値を変更したり、属性列を追加したり、属性列の特定のバージョンまたは属性列全体を削除したりできます。
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' }] }], } ] };行を更新する際に属性列を追加または削除することもできます。
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: 行を削除します。
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' }] } ] };