表格存储提供了PutRow、GetRow、UpdateRow和DeleteRow等单行操作的接口。
如果需要了解表格存储各场景的应用案例,请参见快速玩转Tablestore入门与实战。
前提条件
- 已初始化Client,详情请参见初始化。
- 已创建数据表并写入数据。
插入一行数据(PutRow)
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': '表格存储' },
{ '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。
读取一行数据(GetRow)
GetRow接口用于读取一行数据。
- 如果该行存在,则返回该行的各主键列以及属性列。
- 如果该行不存在,则返回中不包含行,并且不会报错。
接口
/**
* 根据给定的主键读取单行数据。
*/
getRow(params, callback)
参数
参数 | 说明 |
---|---|
tableName | 数据表名称。 |
primaryKey | 行的主键。
说明 设置的主键个数和类型必须和数据表的主键个数和类型一致。
|
columnsToGet | 读取的列集合,列名可以是主键列或属性列。
如果不设置返回的列名,则返回整行数据。 说明
|
maxVersions | 最多读取的版本数。
说明 maxVersions与timeRange必须至少设置一个。
|
timeRange | 读取版本号范围或特定版本号的数据。更多信息,请参见TimeRange。
说明 maxVersions与timeRange必须至少设置一个。
specificTime和 时间戳的单位为毫秒,最小值为0,最大值为Long.MAX_VALUE。 |
columnFilter | 使用过滤器,在服务端对读取结果再进行一次过滤,只返回符合过滤器中条件的数据行。更多信息,请参见过滤器。
说明 当columnsToGet和columnFilter同时使用时,执行顺序是先获取columnsToGet指定的列,再在返回的列中进行条件过滤。
|
示例
读取一行数据。
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 //最多可读取的版本数,设置为2即代表最多可读取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)
接口
/**
* 更新指定行的数据。如果该行不存在,则新增一行;如果该行存在,则根据请求的内容在此行中新增、修改或者删除指定列的值。
*/
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。
删除一行数据(DeleteRow)
DeleteRow接口用于删除一行数据。如果删除的行不存在,则不会发生任何变化。
接口
/**
* 删除一行数据。
*/
deleteRow(params, callback)
参数
参数 | 说明 |
---|---|
tableName | 数据表名称。 |
primaryKey | 行的主键。
说明 设置的主键个数和类型必须和数据表的主键个数和类型一致。
|
condition | 使用条件更新,可以设置原行的存在性条件或者原行中某列的列值条件。更多信息,请参见条件更新。 |
示例
删除一行数据。
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。