使用CreateTable接口创建数据表时,需要指定数据表的结构信息和配置信息,高性能实例中的数据表还可以根据需要设置预留读/写吞吐量。创建数据表时支持创建一个或者多个索引表。
注意事项
创建数据表后需要几秒钟进行加载,在此期间对该数据表的读写数据操作均会失败。请等待数据表加载完毕后再进行数据操作。
创建数据表时必须指定数据表的主键。主键包含1个~4个主键列,每一个主键列都包括名称和类型。
如果在系统设计中存在使用主键列自增功能的场景,例如电商网站的商品ID、大型网站的用户ID、论坛帖子的ID、聊天工具的消息ID等,您可以通过在创建数据表时配置主键列自增实现。具体操作,请参见主键列自增。
前提条件
已创建实例。具体操作,请参见创建实例。
已初始化Client。具体操作,请参见初始化OTSClient。
接口
//说明:根据指定的表结构信息创建数据表。
//request是CreateTableRequest类的实例,它包含TableMeta和TableOption以及ReservedThroughput。
//请参见TableMeta类的文档。
//当创建一个数据表后,通常需要等待几秒钟时间使partition load完成,才能进行各种操作。
//返回:CreateTableResponse
CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)
参数
请结合下表参数说明和创建数据表APICreateTable中的消息结构介绍进行代码中的参数配置。
参数 | 是否必选 | 说明 |
TableMeta | 是 | 数据表的结构信息。包括如下内容:
|
TableOption | 否 | 数据表的配置信息,可选配置。更多信息,请参见数据版本和生命周期。 配置信息包括如下内容:
|
ReservedThroughput | 否 | 为数据表配置预留读吞吐量或预留写吞吐量, 可选配置。 重要 容量型实例中的数据表的预留读/写吞吐量只能设置为0,不允许预留。 默认值为0,即完全按量计费。 单位为CU。
|
IndexMeta | 否 | 索引表的结构信息,可选配置。包括如下内容: 重要 如果要在创建数据表时同时配置二级索引,则需要配置此项。
|
示例
创建数据表时不带索引
以下示例用于创建一个含有2个主键列的数据表。该表的主键为pk0(Integer类型)和pk1(String类型),属性列值最多保留3个版本的数据,并且数据永不过期。
func CreateTableSample(client *tablestore.TableStoreClient, tableName string) {
createTableRequest := new(tablestore.CreateTableRequest)
//创建主键列的schema,包括PK的个数、名称和类型。
//第一个PK列为整数,名称是pk0,此列同时也是分区键。
//第二个PK列为整数,名称是pk1。
tableMeta := new(tablestore.TableMeta)
tableMeta.TableName = tableName
tableMeta.AddPrimaryKeyColumn("pk0", tablestore.PrimaryKeyType_INTEGER)
tableMeta.AddPrimaryKeyColumn("pk1", tablestore.PrimaryKeyType_STRING)
tableOption := new(tablestore.TableOption)
tableOption.TimeToAlive = -1
tableOption.MaxVersion = 3
reservedThroughput := new(tablestore.ReservedThroughput)
reservedThroughput.Readcap = 0
reservedThroughput.Writecap = 0
createTableRequest.TableMeta = tableMeta
createTableRequest.TableOption = tableOption
createTableRequest.ReservedThroughput = reservedThroughput
_, err := client.CreateTable(createTableRequest)
if (err != nil) {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create table finished")
}
}
创建数据表时配置全局二级索引
以下示例用于同时创建数据表和全局二级索引。该表包含pk1(String类型)和pk2(Integer类型)两列主键且包含defcol1(String类型)和defcol2(Integer类型)两个预定义列,属性列值只保留最新版本数据以及数据永不过期。该全局二级索引的主键列为defcol1、pk1和pk2,属性列为defcol2。
func CreateTableWithGlobalIndexSample(client *tablestore.TableStoreClient, tableName string) {
createTableRequest := new(tablestore.CreateTableRequest)
tableMeta := new(tablestore.TableMeta)
tableMeta.TableName = tableName
tableMeta.AddPrimaryKeyColumn("pk1", tablestore.PrimaryKeyType_STRING)
tableMeta.AddPrimaryKeyColumn("pk2", tablestore.PrimaryKeyType_INTEGER)
tableMeta.AddDefinedColumn("defcol1", tablestore.DefinedColumn_STRING)
tableMeta.AddDefinedColumn("defcol2", tablestore.DefinedColumn_INTEGER)
indexMeta := new(tablestore.IndexMeta) //新建索引表Meta。
indexMeta.AddPrimaryKeyColumn("defcol1") //为索引表添加主键列。设置数据表的definedcol1列作为索引表的主键。
indexMeta.AddDefinedColumn("defcol2") //为索引表添加属性列。设置数据表的definedcol2列作为索引表的属性列。
indexMeta.IndexName = "indexSample"
tableOption := new(tablestore.TableOption)
tableOption.TimeToAlive = -1 //数据的过期时间,单位为秒,-1表示永不过期。带索引表的数据表数据生命周期必须设置为-1。
tableOption.MaxVersion = 1 //保存的最大版本数,1表示每列上最多保存一个版本即保存最新的版本。带索引表的数据表最大版本数必须设置为1。
reservedThroughput := new(tablestore.ReservedThroughput)
createTableRequest.TableMeta = tableMeta
createTableRequest.TableOption = tableOption
createTableRequest.ReservedThroughput = reservedThroughput
createTableRequest.AddIndexMeta(indexMeta)
_, err := client.CreateTable(createTableRequest)
if err != nil {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create table finished")
}
}
创建数据表时配置本地二级索引
以下示例用于同时创建数据表和本地二级索引。该表的主键为pk1(String类型)和pk2(Integer类型)且包括defcol1(String类型)和defcol2(Integer类型)两个预定义列,属性列值只保留最新版本数据以及数据永不过期。该本地二级索引的主键列为pk1、defcol1和pk2,属性列为defcol2。
func CreateTableWithLocalIndexSample(client *tablestore.TableStoreClient, tableName string) {
createTableRequest := new(tablestore.CreateTableRequest)
tableMeta := new(tablestore.TableMeta)
tableMeta.TableName = tableName
tableMeta.AddPrimaryKeyColumn("pk1", tablestore.PrimaryKeyType_STRING)
tableMeta.AddPrimaryKeyColumn("pk2", tablestore.PrimaryKeyType_INTEGER)
tableMeta.AddDefinedColumn("defcol1", tablestore.DefinedColumn_STRING)
tableMeta.AddDefinedColumn("defcol2", tablestore.DefinedColumn_INTEGER)
indexMeta := new(tablestore.IndexMeta) //新建索引表Meta。
indexMeta.IndexType = tablestore.IT_LOCAL_INDEX //设置索引类型为本地二级索引(IT_LOCAL_INDEX)。
indexMeta.AddPrimaryKeyColumn("pk1") //为索引表添加主键列。索引表的第一列主键必须与数据表的第一列主键相同。
indexMeta.AddPrimaryKeyColumn("defcol1") //为索引表添加主键列。设置数据表的definedcol1列作为索引表的主键。
indexMeta.AddDefinedColumn("defcol2") //为索引表添加属性列。设置数据表的definedcol2列作为索引表的属性列。
indexMeta.IndexName = "indexSample"
tableOption := new(tablestore.TableOption)
tableOption.TimeToAlive = -1 //数据的过期时间,单位为秒,-1表示永不过期。带索引表的数据表数据生命周期必须设置为-1。
tableOption.MaxVersion = 1 //保存的最大版本数,1表示每列上最多保存一个版本即保存最新的版本。带索引表的数据表最大版本数必须设置为1。
reservedThroughput := new(tablestore.ReservedThroughput)
createTableRequest.TableMeta = tableMeta
createTableRequest.TableOption = tableOption
createTableRequest.ReservedThroughput = reservedThroughput
createTableRequest.AddIndexMeta(indexMeta)
_, err := client.CreateTable(createTableRequest)
if err != nil {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create table finished")
}
}
相关文档
关于API说明的更多信息,请参见CreateTable。详细代码请参见CreateTable@GitHub。
如果要读写表中的数据,您可以通过调用读写数据接口实现。具体操作,请参见基础数据操作。
如果要修改表的数据生命周期、最大版本数等信息,您可以通过更新表实现。具体操作,请参见更新表。
为数据表开启局部事务后,您可以创建局部事务用于读写局部事务范围内的数据。具体操作,请参见局部事务。
如果要查看实例中已存在的所有表,您可以通过列出表名称来实现。具体操作,请参见列出表名称。
如果要查看表的最大版本数、数据生命周期等配置信息,您可以通过查看表描述信息实现。具体操作,请参见查询表描述信息。
如果要使用二级索引加速数据查询,但是在创建数据表时未设置预定义列,您可以在创建数据表后为数据表增加或删除预定义列。具体操作,请参见预定义列操作。
如果不再使用数据表,您可以删除数据表。具体操作,请参见删除数据表。
- 本页导读 (1)