近段时间忙着做项目,中间也遇到了另一个新的需求,那就是在大量数据交互(如:消息通讯)时采用关系型数据库有点为难,所以就要用到阿里云的表格存储(是种NoSQL型),但这个有点陌生所以也只能大致记下它的使用。
通用服务的配置
var client = new TableStore.Client({ accessKeyId: '', secretAccessKey: '', endpoint: 'https://[instancename同名].cn-hangzhou.ots.aliyuncs.com', instancename: '', maxRetries: 20,//默认20次重试,可以省略这个参数。 });
一、单行存储
主要依赖:
import * as TableStore from 'TableStore'; import * as md5 from 'md5';
入表前的配置信息
// 阿里云 - 表格存储 数据类型处理 var Long = TableStore.Long; // 用户请求参数 var body = {From_Account:'testABC',To_Account:'testDEF',MsgTime:Date.now(),body:'测试存储'}; // 阿里云 - 表格存储 入表前的参数配置 var params = { tableName: "message", condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null), // 所有的主键字段 必做按创建时的顺序都填 primaryKey: [{ 'partition_key': md5(body.From_Account + "|" + body.To_Account).slice(0, 4) }, { 'receive_id': body.To_Account }, { 'from_id': body.From_Account }, { 'msg_time': Long.fromNumber(body.MsgTime) }], // 非主键字段 attributeColumns: [{ 'msg_body': JSON.stringify(body.body) }], returnContent: { returnType: TableStore.ReturnType.Primarykey } };
入表的主操作
client.putRow(params, async function (err, data) { if (err) { console.error('error:', err); } console.log('成功了'); console.log(data); });
转载请注明:隨習筆記 » NodeJs 阿里云的表格存储