Table Store SDK には、PutRow、GetRow、UpdateRow、DeleteRow の単一行操作 API が用意されています。
PutRow
指定された行にデータを挿入します。
API
/**
* 指定された行にデータを挿入します。 指定された行が存在しない場合は、新しい行が追加されます。 行が存在する場合は、元の行が上書きされます。
*/
putRow(params, callback)
例
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);
});
-
RowExistenceExpectation.IGNORE は、指定された行が存在するかどうかにかかわらず、新しいデータが挿入されることを示します。 挿入されたデータが既存のデータと同じ場合、既存のデータは上書きされます。
-
RowExistenceExpectation.EXPECT_EXIST は、指定された行が存在する場合にのみ新しいデータが挿入されることを示します。 既存のデータは上書きされます。
-
RowExistenceExpectation.EXPECT_NOT_EXIST は、指定された行が存在しない場合にのみデータが挿入されることを示します。
注 完全なサンプルコードは、『PutRow@GitHub』をご参照ください。
GetRow
この API は、指定されたプライマリキーに基づいて単一のデータ行を読み取ります。
API
/**
* 与えられたプライマリキーに基づいて単一のデータ行を読み込みます。
*/
getRow(params, callback)
例
Read a data row.
var TableStore = require('../index.js');
var Long = TableStore.Long;
var client = require('./client');
var params = {
tableName: "sampleTable",
primaryKey: [{ 'gid': Long.fromNumber(20004) }, { 'uid': Long.fromNumber(20004) }],
maxVersions: 2
};
var condition = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
condition.addSubCondition(new TableStore.SingleColumnCondition('name', 'john', TableStore.ComparatorType.EQUAL));
condition.addSubCondition(new TableStore.SingleColumnCondition('addr', 'china', TableStore.ComparatorType.EQUAL));
params.columnFilter = condition;
client.getRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
注 完全なサンプルコードは、『GetRow@GitHub』をご参照ください。
UpdateRow
指定された行のデータを更新します。 指定された行が存在しない場合は、新しい行が追加されます。 指定された行が存在する場合は、リクエスト内容に基づいて指定された列の値が追加、変更、削除されます。
API
/**
* 指定された行のデータを更新します。 指定された行が存在しない場合は、新しい行が追加されます。 指定された行が存在する場合は、指定された列の値は、リクエストに合わせて、追加、変更、又は削除されます。
*/
updateRow(params, callback)
例
データ行を更新します。
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』をご参照ください。
DeleteRow
API
/**
* データ行を削除します。
*/
deleteRow(params, callback)
例
データ行を削除します。
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(8) }, { 'uid': Long.fromNumber(80) }]
};
client.deleteRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
注 完全なサンプルコードは、『DeleteRow@GitHub』をご参照ください。