开发者社区 问答 正文

NodeJS-SDK的单行数据操作

表格存储的 SDK 提供了 PutRow、GetRow、UpdateRow 和 DeleteRow 等单行操作的接口。

插入一行数据(PutRow)


插入数据到指定的行。

接口/**
   * 插入数据到指定的行,如果该行不存在,则新增一行;若该行存在,则覆盖原有行。
   */
  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': '表格存储' },
    { '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 表示只有此行不存在时,才会插入数据,否则不执行。

[backcolor=transparent]说明:详细代码可在 PutRow@GitHub 获取。


读取一行数据(GetRow)


根据给定的主键读取单行数据。

接口/**
   * 根据给定的主键读取单行数据。
   */
  getRow(params, callback)





示例


读取一行数据。 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);
});


[backcolor=transparent]说明:详细代码可在 GetRow@GitHub 获取。


更新一行数据(UpdateRow)


更新指定行的数据,如果该行不存在,则新增一行;若该行存在,则根据请求的内容在这一行中新增、修改或者删除指定列的值。

接口/**
   * 更新指定行的数据。如果该行不存在,则新增一行;若该行存在,则根据请求的内容在这一行中新增、修改或者删除指定列的值。
   */
  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);
    });


[backcolor=transparent]说明:详细代码可在 UpdateRow@GitHub 获取。


删除一行数据(DeleteRow)



接口/**
   * 删除一行数据。
   */
  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);
});


[backcolor=transparent]说明:详细代码可在 DeleteRow@GitHub 获取。

展开
收起
云栖大讲堂 2017-10-25 16:35:39 2560 分享 版权
0 条回答
写回答
取消 提交回答