全部產品
Search
文件中心

Tablestore:寫入單行資料

更新時間:Mar 12, 2026

本文介紹如何通過 Node.js SDK 在Table Store的資料表中寫入單行資料。

前提條件

初始化Tablestore Client

方法說明

putRow: function putRow(params, callback)

params參數說明

名稱

類型

說明

tableName(必選)

string

資料表名稱。

primaryKey(必選)

Array

主鍵資訊,包括主鍵列名稱和主索引值。

  • 主鍵列資料類型包括 STRING、INTEGER 和 BINARY。

  • 寫入資料的主鍵個數和類型必須與資料表的主鍵保持一致。

  • 主鍵列為自增列時,需將該列的值設定為預留位置,詳情請參見主鍵列自增

attributeColumns(可選)

Array

屬性列資訊,包括屬性列名稱、屬性列值和資料版本號碼。

  • 屬性列資料類型包括 STRING、INTEGER、BINARY、DOUBLE 和 BOOLEAN。

  • 資料版本號碼即時間戳記,預設由系統自動產生,也可以自行指定,詳情請參見資料版本和生命週期

condition(必選)

TableStore.Condition

寫入條件,詳情請參見條件更新

returnContent(可選)

object

返回的資料內容。

  • returnType(可選)number:傳回型別。

    • TableStore.ReturnType.NONE:預設值,不返回資料。

    • TableStore.ReturnType.Primarykey:返回主鍵列,可以用於主鍵列自增。

    • TableStore.ReturnType.AfterModify:返回更改後的列值,用於原子計數器

transactionId(可選)

string

局部事務ID,用於唯一標識局部事務,詳情請參見局部事務

範例程式碼

以下範例程式碼在 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() }];

相關文檔

批次更新資料