Tulis, perbarui, dan hapus baris dari beberapa tabel Tablestore dalam satu permintaan menggunakan metode batchWriteRow pada Node.js SDK.
Catatan
-
Jika server mendeteksi kesalahan parameter pada suatu operasi, pengecualian kesalahan parameter akan dikembalikan. Dalam kasus ini, tidak ada operasi dalam permintaan yang dieksekusi.
-
Satu operasi pembaruan batch mendukung maksimal 200 baris, dengan total volume data dari semua baris tidak melebihi 4 MB.
Prasyarat
Metode
batchWriteRow: function batchWriteRow(params, callback)
Contoh kode
Contoh kode berikut menunjukkan cara menggunakan operasi batch untuk menyisipkan satu baris ke dalam tabel test_table.
var table = {
tableName: 'test_table',
rows: [
{
type: 'PUT',
// Tentukan kondisi untuk operasi tulis. TableStore.RowExistenceExpectation.IGNORE menentukan agar keberadaan baris tidak diperiksa.
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'id': 'row1' }]
}
]
};
var params = {
tables: [table]
};
// Panggil metode batchWriteRow untuk melakukan operasi data batch.
client.batchWriteRow(params, function (err, data) {
if (err) {
console.log('Batch write row failed with error: %s', err);
return;
}
// Proses hasil yang dikembalikan.
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);
}
});
});
Contoh kode berikut menunjukkan berbagai jenis operasi data.
-
PutRowChange: Menulis satu baris.
var table = { tableName: 'test_table', rows: [ { type: 'PUT', // Tentukan kondisi untuk operasi tulis. TableStore.RowExistenceExpectation.IGNORE menentukan agar keberadaan baris tidak diperiksa. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }] } ] };Tambahkan juga kolom atribut saat menulis baris.
var table = { tableName: 'test_table', rows: [ { type: 'PUT', // Tentukan kondisi untuk operasi tulis. TableStore.RowExistenceExpectation.IGNORE menentukan agar keberadaan baris tidak diperiksa. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }], attributeColumns: [ // Tambahkan kolom atribut. { 'col1': 'val1' }, // Tambahkan kolom atribut dengan nomor versi data kustom. { 'col2': 'val2', 'timestamp': Date.now() } ] } ] }; -
UpdateRowChange: Memperbarui satu baris. Anda dapat mengubah nilai kolom atribut, menambahkan kolom atribut, atau menghapus versi tertentu dari kolom atribut maupun seluruh kolom atribut.
var table = { tableName: 'test_table', rows: [ { type: 'UPDATE', // Tentukan kondisi untuk operasi pembaruan. TableStore.RowExistenceExpectation.IGNORE menentukan agar keberadaan baris tidak diperiksa. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }], attributeColumns: [{ 'PUT': [{ 'col1': 'changed_val1' }] }], } ] };Tambahkan atau hapus kolom atribut saat memperbarui baris.
var table = { tableName: 'test_table', rows: [ { type: 'UPDATE', // Tentukan kondisi untuk operasi pembaruan. TableStore.RowExistenceExpectation.IGNORE menentukan agar keberadaan baris tidak diperiksa. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }], attributeColumns: [ { 'PUT': [ // Tambahkan kolom atribut. { 'col3': 'val3' }, // Tambahkan kolom atribut dengan nomor versi data kustom. { 'col4': 'val4', 'timestamp': Date.now() } ] }, { // Hapus kolom atribut. 'DELETE_ALL': ['col2'] } ], } ] }; -
DeleteRowChange: Menghapus satu baris.
var table = { tableName: 'test_table', rows: [ { type: 'DELETE', // Tentukan kondisi untuk operasi penghapusan. TableStore.RowExistenceExpectation.IGNORE menentukan agar keberadaan baris tidak diperiksa. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), primaryKey: [{ 'id': 'row1' }] } ] };