Tablestore提供了PutRow和UpdateRow介面用於寫入單行資料以及BatchWriteRow介面用於批量寫入資料。
前提條件
- 已初始化Client,詳情請參見初始化。
- 已建立資料表並寫入資料。
插入單行資料
調用PutRow介面新寫入一行資料。如果該行已存在,則先刪除原行資料(原行的所有列以及所有版本的資料),再寫入新行資料。
介面
/**
* 插入資料到指定的行,如果該行不存在,則新增一行;如果該行存在,則覆蓋原有行。
*/
putRow(params, callback)
參數
參數 | 說明 |
---|---|
tableName | 資料表名稱。 |
primaryKey | 行的主鍵。
说明
|
condition | 使用條件更新,可以設定原行的存在性條件或者原行中某列的列值條件。更多資訊,請參見條件更新。
说明
|
attributeColumns | 行的屬性列。
|
returnContent | 表示傳回型別。
returnType:設定為TableStore.ReturnType.Primarykey,表示返回主索引值,主要用於主鍵列自增情境。 |
樣本
插入一行資料。
var TableStore = require('../index.js');
var Long = TableStore.Long;
var client = require('./client');
var currentTimeStamp = Date.now();
var params = {
tableName: "sampleTable",
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(20013) }, { 'uid': Long.fromNumber(20013) }],
attributeColumns: [
{ 'col1': 'Table Store' },
{ 'col2': '2', 'timestamp': currentTimeStamp },
{ 'col3': 3.1 },
{ 'col4': -0.32 },
{ 'col5': Long.fromNumber(123456789) }
],
returnContent: { returnType: TableStore.ReturnType.Primarykey }
};
client.putRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
詳細代碼請參見PutRow@GitHub。
更新單行資料
介面
/**
* 更新指定行的資料。如果該行不存在,則新增一行;如果該行存在,則根據請求的內容在此行中新增、修改或者刪除指定列的值。
*/
updateRow(params, callback)
參數
參數 | 說明 |
---|---|
tableName | 資料表名稱。 |
primaryKey | 行的主鍵。
说明 設定的主鍵個數和類型必須和資料表的主鍵個數和類型一致。
|
condition | 使用條件更新,可以設定原行的存在性條件或者原行中某列的列值條件。更多資訊,請參見條件更新。 |
updateOfAttributeColumns | 更新的屬性列。
|
樣本
更新一行資料。
var TableStore = require('../index.js');
var Long = TableStore.Long;
var client = require('./client');
var params = {
tableName: "sampleTable",
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(9) }, { 'uid': Long.fromNumber(90) }],
updateOfAttributeColumns: [
{ 'PUT': [{ 'col4': Long.fromNumber(4) }, { 'col5': '5' }, { 'col6': Long.fromNumber(6) }] },
{ 'DELETE': [{ 'col1': Long.fromNumber(1496826473186) }] },
{ 'DELETE_ALL': ['col2'] }
]
};
client.updateRow(params,
function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
詳細代碼請參見UpdateRow@GitHub。
批量寫入資料
調用BatchWriteRow介面在一次請求中進行批量的寫入操作,也支援一次對多張表進行寫入。BatchWriteRow操作由多個PutRow、UpdateRow、DeleteRow子操作組成,構造子操作的過程與使用PutRow介面、UpdateRow介面和DeleteRow介面時相同,也支援使用條件更新。
BatchWriteRow的各個子操作獨立執行,Tablestore會分別返回各個子操作的執行結果。
注意事項
由於批量寫入可能存在部分行失敗的情況,失敗行的Index及錯誤資訊在返回的BatchWriteRowResponse中,但並不拋出異常。因此調用BatchWriteRow介面時,需要檢查傳回值,判斷每行的狀態是否成功;如果不檢查傳回值,則可能會忽略掉部分操作的失敗。
當服務端檢查到某些操作出現參數錯誤時,BatchWriteRow介面可能會拋出參數錯誤的異常,此時該請求中所有的操作都未執行。
介面
/**
* 批量修改行。
*/
batchWriteRow(params, callback)
參數
- 增加了資料表的層級結構,可以一次處理多個資料表。
tables以資料表為單位組織,後續為各個資料表的操作,設定需要寫入、修改或刪除的行資訊。
- 增加了type參數,用於區分操作類型。
操作類型可以為PUT、UPDATE、DELETE。
- 當操作類型為PUT或UPDATE時,primaryKey和attributeColumns有效。
- 當操作類型為DELETE時,primaryKey有效。
樣本
批量寫入資料。
var client = require('./client');
var TableStore = require('../index.js');
var Long = TableStore.Long;
var params = {
tables: [
{
tableName: 'sampleTable',
rows: [
{
type: 'UPDATE',
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(20010) }, { 'uid': Long.fromNumber(20010) }],
attributeColumns: [{ 'PUT': [{ 'col1': 'test3' }, { 'col2': 'test4' }] }],
returnContent: { returnType: 1 }
},
{
type: 'PUT',
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(20020) }, { 'uid': Long.fromNumber(20020) }],
attributeColumns: [{ 'col1': 'test1' }, { 'col2': 'test2' }],
returnContent: { returnType: TableStore.ReturnType.Primarykey }
},
{
type: 'DELETE',
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(20018) }, { 'uid': Long.fromNumber(20018) }],
}
]
}
],
};
client.batchWriteRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
詳細代碼請參見BatchWriteRow@GitHub。