表操作
所有文档
menu
没有找到结果,请重新输入

云数据库 TableStorage

表操作

创建表

描述

在指定实例下创建一张表,表名限制字符个数1-255,满足正则:[a-zA-Z_][a-za-z0-9_]{0,254},以字母或下划线开头。

建表是一个异步流程,提交建表请求成功后,用户需要调用获取表状态的接口,当表状态为Normal后方可使用此表。

请求参数

参数名称 是否必须 参数类型 说明
tableVersion Number 表版本号,建表时固定传0
compressType String 数据压缩方式,可取以下值:
- "NONE" : 不压缩
- "SNAPPY_ALL" : 使用snappy压缩
默认为"NONE",即不压缩
开启压缩后,读取数据时后台需要解压,读延时将会增加
ttl Number 数据过期时间,为0时永不过期,单位秒。默认为0,即永不过期
storageType String 表存储类型,仅支持HighPerformance和CommonPerformance两种,若无此参数则默认使用实例中定义的表存储类型
maxVersions Number 最多保留版本数,取值范围[1, 50000]

返回参数

示例

let Client = require('@baiducloud/sdk').BtsClient;
let myClient = new Client.BtsClient(config);
  
let createTableRequest = new Client.CreateTableRequest();
createTableRequest.ttl = 0;
createTableRequest.maxVersions = 10;
  
myClient.createTable('{instanceName}', '{tableName}', createTableRequest)
    .then(response => console.log(response))    // 成功
    .catch(error => console.error(error));      // 失败

更新表

描述

更新一张表的指定信息。请求必须携带ttl,maxVersions和compressType中至少一个字段,否则返回400。该操作的最小时间间隔为 2 分钟,如果本次操作距上次不到 2 分钟将被拒绝,返回503。

请求参数

参数名称 是否必须 参数类型 说明
tableVersion Number 必须先获取表的版本信息后,再带入此次请求
compressType String 数据压缩方式,默认为不压缩,可取以下值:
- "NONE" : 不压缩
- "SNAPPY_ALL" : 使用snappy压缩
ttl Number 数据过期时间,为0时永不过期,默认永不过期,单位秒
maxVersions Number 最多保留版本数,取值范围[1, 50000]

注意

  • 更新表不允许修改表的名称(tableName);
  • 请求必须携带ttl,maxVersions和compressType中至少一个字段,否则返回400;

返回参数

示例

let Client = require('@baiducloud/sdk').BtsClient;
let myClient = new Client.BtsClient(config);
  
let updateTableRequest = new Client.UpdateTableRequest();
updateTableRequest.maxVersions = 100;
updateTableRequest.compressType = 'NONE';
  
myClient.updateTable('{instanceName}', '{tableName}', updateTableRequest)
    .then(response => console.log(response))    // 成功
    .catch(error => console.error(error));      // 失败

删除表

描述

删除一张表。

请求参数

返回参数

示例

let Client = require('@baiducloud/sdk').BtsClient;
let myClient = new Client.BtsClient(config);
  
myClient.dropTable('{instanceName}', '{tableName}')
    .then(response => console.log(response))    // 成功
    .catch(error => console.error(error));      // 失败

显示表信息

描述

查询一张表的信息。

请求参数

参数名称 是否必须 参数类型 说明
onlyState Boolean 如果把该值设为true,响应只会反回tableState字段,否则会全额反回。
主要用于建表成功后轮询状态使用 。

返回参数

参数名称 参数类型 说明
instance String 表属于哪个实例
tableName String 表名称
tableState String 表状态信息,有如下取值:
- Normal:表状态正常,可正常读写
- Creating:表正在创建中
- Updating:表正在更新中
- Dropping:表正在被删除
tableVersion Number 当前表版本号,值是最后一次更新的微秒级时间戳
createTime String 表创建的UTC时间
compressType String 数据压缩方式,默认为不压缩,可取以下值:
- "NONE" : 不压缩
-"SNAPPY_ALL" : 使用snappy压缩
ttl Number 数据过期时间,为0时永不过期,默认永不过期,单位秒
storageType String 表存储类型
maxVersions Number 最多保留版本数,取值范围[1, 50000]

示例

let Client = require('@baiducloud/sdk').BtsClient;
let myClient = new Client.BtsClient(config);
  
myClient.showTable('{instanceName}', '{tableName}', false)
    .then(response => console.log(response))    // 成功
    .catch(error => console.error(error));      // 失败

列举所有表

描述

列举实例下所有表信息。

请求参数

返回参数

参数名称 参数类型 说明
tables Array 查询返回的所有表
+createTime String 表创建的UTC时间
+tableName String 表名称
+tableState String 表状态信息,有如下取值:
- Normal:表状态正常,可正常读写
- Creating:表正在创建中
- Dropping:表正在被删除
+tableVersion Number 当前表版本号,值是最后一次更新的微秒级时间戳
+storageType String 表存储类型
+maxVersions Number 最多保留版本数,取值范围[1, 50000]

示例

let Client = require('@baiducloud/sdk').BtsClient;
let myClient = new Client.BtsClient(config);
  
myClient.listTables('{instanceName}')
    .then(response => console.log(response))    // 成功
    .catch(error => console.error(error));      // 失败
上一篇
实例操作
下一篇
行操作