准备工作
在您开始Tablestore SDK开发前,需确保已开通表格存储服务并且已创建表格存储实例。
您需要提前获取到以下几个参数
- 服务地址(Endpoint)。可选择公网地址进行测试。
- 实例名(InstanceName)。
- 访问密钥(AccessKeyID、AccessKeySecret)。
开发简介
开发示例中将以订单场景为例,使用Tablestore SDK实现如下几个功能。
字段名 | 字段类型 | | 字段描述 |
order_id | String | 主键 | 订单号 |
customer_name | String | 属性列 | 消费者姓名 |
product_name | String | 属性列 | 产品名 |
product_type | String | 属性列 | 产品类型 |
order_time | String | 属性列 | 下单时间 |
pay_time | String | 属性列 | 支付时间 |
订单表
开发步骤
初始化连接
Tablestore支持Http/Https协议访问服务端,使用Node.js SDK发起请求前,您需要初始化一个OTSClinet实例,初始化需要获取到服务地址(endpoint)、实例名(instanceName)、密钥(accessKeyId、accessSecret)等信息。代码如下
var client = new TableStore.Client({
accessKeyId: '',
accessKeySecret: '',
endpoint: '"https://order-instance.cn-beijing.ots.aliyuncs.com"',
instancename: 'order-instance',
maxRetries:20,//默认20次重试,可以省略此参数。
});
创建数据表
示例代码中创建了一张订单数据表order。
var client = require('./client');
var params = {
tableMeta: {
tableName: 'order',
primaryKey: [
{
name: 'order_id',
type: 'STRING'
}
]
},
reservedThroughput: {
capacityUnit: {
read: 0,
write: 0
}
},
tableOptions: {
timeToLive: -1,
maxVersions: 1
}
};
client.createTable(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
写入数据
示例代码中写入了一条订单数据,订单号order_id为“o1”。样例中模拟了一万条订单数据,这里不作展示。
var TableStore = require('../index.js');
var client = require('./client');
var params = {
tableName: "order",
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'order_id': 'o1' }],
attributeColumns: [
{ 'customer_name': '消十一' },
{ 'product_name': 'iphone 6'},
{ 'product_type': '手机' },
{ 'order_time': '2021-10-25 09:20:01' },
{ 'pay_time': '2017-10-25 10:00:01') }
],
returnContent: { returnType: TableStore.ReturnType.Primarykey }
};
client.putRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
查询数据
示例代码中查询订单号order_id为“o1”的记录
var TableStore = require('../index.js');
var client = require('./client');
var params = {
tableName: "order",
primaryKey: [{ 'order_id': 'o1' }],
maxVersions: 1
};
client.getRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
创建多元索引
示例代码中创建了一个多元索引order_index。分别设置customer_name字符串类型、order_time字符串类型、pay_time字符串类型、product_name分词类型、product_type字符串类型。关于索引字段类型的介绍请参考多元索引概述。
var client = require('./client');
var TableStore = require('../../index.js');
client.createSearchIndex({
tableName: 'order',
indexName: 'order_index',
schema: {
fieldSchemas: [
{
fieldName: "customer_name",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: false,
isAnArray: false
},
{
fieldName: "order_time",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false
},
{
fieldName: "pay_time",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false
},
{
fieldName: "product_name",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzer: "single_word"
},
{
fieldName: "pay_time",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false
}
]
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
搜索数据
示例代码中查询产品类型为“手机”的订单,并统计了符合条件的行数。
var client = require('./client');
var TableStore = require('../../index.js');
client.search({
tableName: 'order',
indexName: 'order_index',
searchQuery: {
offset: 0,
limit: 10,
query: {
queryType: TableStore.QueryType.TERM_QUERY,
query: {
fieldName: "product_type",
term: "手机"
}
},
getTotalCount: true
},
columnToGet: {
returnType: TableStore.ColumnReturnType.RETURN_ALL
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', JSON.stringify(data, null, 2));
});
示例代码中搜索产品名包含“iphone”的订单,并统计了符合条件的行数。
var client = require('./client');
var TableStore = require('../../index.js');
client.search({
tableName: 'order',
indexName: 'order_index',
searchQuery: {
offset: 0,
limit: 10,
query: {
queryType: TableStore.QueryType.MATCH_QUERY,
query: {
fieldName: "product_name",
text: "iphone"
}
},
getTotalCount: true
},
columnToGet: {
returnType: TableStore.ColumnReturnType.RETURN_ALL
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', JSON.stringify(data, null, 2));
});
示例代码中查询了消费者姓名为“消十一”并且下单时间在“2021-10-24 00:00:00”之间的订单。并统计了行数。
var client = require('../client');
var TableStore = require('../../index.js');
client.search({
tableName: "order",
indexName: "order_index",
searchQuery: {
offset: 0,
limit: 10,
getTotalCount: false,
query: {
queryType: TableStore.QueryType.BOOL_QUERY,
query: {
mustQueries: [
{
queryType:TableStore.QueryType.TERM_QUERY,
query: {
fieldName : "customer_name",
term: "消十一"
}
},
{
queryType: TableStore.QueryType.RANGE_QUERY,
query: {
fieldName: "order_time",
rangeTo: '2021-10-24 00:00:00',
includeUpper: false
}
}
]
}
},
},
columnToGet: {
returnType: TableStore.ColumnReturnType.RETURN_ALL
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', JSON.stringify(data, null, 2));
});
删除多元索引
示例代码中展示了删除订单表order中的order_index多元索引。
var client = require('./client');
client.deleteSearchIndex({
tableName: 'order',
indexName: 'order_index'
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
删除数据表
示例代码中展示了删除订单表order。删除表之前需确保先删除表中的多元索引。
var client = require('./client');
var params = {
tableName: 'order'
};
client.deleteTable(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
更多关于Tablestore Node.js SDK的介绍请参考Tablestore Node.js SDK。