创建数据表
使用CreateTable接口创建数据表时,需要指定数据表的结构信息和配置信息,高性能实例中的数据表还可以根据需要设置预留读/写吞吐量。创建数据表时支持创建一个或者多个索引表。
注意事项
前提条件
参数
请结合下表参数说明和创建数据表APICreateTable中的消息结构介绍进行代码中的参数配置。
参数 | 说明 |
tableMeta | 数据表的结构信息,包括如下内容:
|
tableOptions | 数据表的配置信息。更多信息,请参见数据版本和生命周期。 配置信息包括如下内容:
如果希望修改表的数据生命周期、最大版本数等配置信息,请通过调用UpdateTable接口实现。具体操作,请参见更新表。 |
reservedThroughtput | 为数据表配置预留读吞吐量或预留写吞吐量。 容量型实例中的数据表的预留读/写吞吐量只能设置为0,不允许预留。 默认值为0,即完全按量计费。 单位为CU。
|
localTxnEnabled | 是否开启局部事务功能。类型为Boolean。默认值为false,表示不开启局部事务。 如果要在创建数据表时开启局部事务,请设置此参数为true。 重要
|
indexMetas | 索引表的结构信息,每个indexMeta都包括如下内容:
|
示例
创建数据表时不带索引
以下示例用于创建数据表。该表的主键为pk(String类型),属性列值最多保留3个版本数据以及数据永不过期。
private static void createTable(SyncClient client) {
//设置数据表名称。
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
//为数据表添加主键列。
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk", PrimaryKeyType.STRING));
//数据的过期时间,单位为秒,-1表示永不过期。带索引表的数据表数据生命周期必须设置为-1。
int timeToLive = -1;
//保存的最大版本数,1表示每列上最多保存一个版本即保存最新的版本。带索引表的数据表最大版本数必须设置为1。
int maxVersions = 3;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0))); //设置预留读写吞吐量,容量型实例中的数据表只能设置为0,高性能实例中的数据表可以设置为非零值。
client.createTable(request);
}
创建数据表时配置全局二级索引
以下示例用于同时创建数据表和全局二级索引。该表包含pk1(String类型)和pk2(Integer类型)两列主键且包含defcol1(String类型)和defcol2(Integer类型)两个预定义列,属性列值只保留最新版本数据以及数据永不过期。该全局二级索引的主键列为defcol1、pk1和pk2,属性列为defcol2。
private static void createTable(SyncClient client) {
//设置数据表名称。
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
//为数据表添加主键列。
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
//为数据表添加预定义列。
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
//数据的过期时间,单位为秒,-1表示永不过期。带索引表的数据表数据生命周期必须设置为-1。
int timeToLive = -1;
//保存的最大版本数,1表示每列上最多保存一个版本即保存最新的版本。带索引表的数据表最大版本数必须设置为1。
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
//设置索引表名称。
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
//为索引表添加主键列。
indexMeta.addPrimaryKeyColumn("defcol1");
//为索引表添加属性列。
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); //创建数据表的同时创建索引表。
client.createTable(request);
}
创建数据表时配置本地二级索引
以下示例用于同时创建数据表和本地二级索引。该表的主键为pk1(String类型)和pk2(Integer类型)且包括defcol1(String类型)和defcol2(Integer类型)两个预定义列,属性列值只保留最新版本数据以及数据永不过期。该本地二级索引的主键列为pk1、defcol1和pk2,属性列为defcol2。
private static void createTable(SyncClient client) {
//设置数据表名称。
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
//为数据表添加主键列。
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
//为数据表添加预定义列。
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
//数据的过期时间,单位为秒,-1表示永不过期。带索引表的数据表数据生命周期必须设置为-1。
int timeToLive = -1;
//保存的最大版本数,1表示每列上最多保存一个版本即保存最新的版本。带索引表的数据表最大版本数必须设置为1。
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
//设置索引表名称。
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
//设置索引类型为本地二级索引(IT_LOCAL_INDEX)。
indexMeta.setIndexType(IT_LOCAL_INDEX);
//设置索引更新模式为同步更新(IUM_SYNC_INDEX)。当索引类型为本地二级索引时,索引更新模式必须为同步更新。
indexMeta.setIndexUpdateMode(IUM_SYNC_INDEX);
//为索引表添加主键列。索引表的第一列主键必须与数据表的第一列主键相同。
indexMeta.addPrimaryKeyColumn("pk1");
//为索引表添加主键列。
indexMeta.addPrimaryKeyColumn("defcol1");
//为索引表添加属性列。
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); //创建数据表的同时创建索引表。
client.createTable(request);
}
创建表时开启局部事务
以下示例用于创建数据表时开启局部事务功能。该表的主键为pk1(String类型)和pk2(Integer类型),属性列值只保留最新版本数据以及数据永不过期。
private static void createTable(SyncClient client) {
//设置数据表名称。
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
//为数据表添加主键列。
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
//数据的过期时间,单位为秒,-1表示永不过期。带索引表的数据表数据生命周期必须设置为-1。
int timeToLive = -1;
//保存的最大版本数,1表示每列上最多保存一个版本即保存最新的版本。带索引表的数据表最大版本数必须设置为1。
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
//开启局部事务。当为数据表配置了主键自增列时,此配置无效。
request.setLocalTxnEnabled(true);
client.createTable(request);
}