本文介紹如何通過 Node.js SDK 在Table Store的資料表中寫入單行資料。
前提條件
方法說明
putRow: function putRow(params, callback)範例程式碼
以下範例程式碼在 test_table 表中寫入一行資料,該行資料的主索引值為 row1。
var params = {
tableName: 'test_table',
primaryKey: [{ 'id': 'row1' }],
// 寫入資料時必須指定寫入條件 (TableStore.RowExistenceExpectation.IGNORE,表示不做行存在性判斷)
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null)
};
client.putRow(params, function (err, data) {
if (err) {
console.log('Put row failed with error: ', err);
return;
}
console.log('* RequestId: ', data.RequestId);
console.log('* Read CU Cost: ', data.consumed.capacityUnit.read);
console.log('* Write CU Cost: ', data.consumed.capacityUnit.write);
});添加屬性列。
params.attributeColumns = [{ 'col1': 'val1' }];指定資料版本號碼,您可以為每個屬性列指定單獨的版本號碼。
params.attributeColumns = [{ 'col1': 'val1', 'timestamp': Date.now() }];