使用CreateTable接口创建数据表时,需要指定数据表的结构信息和配置信息,高性能实例中的数据表还可以根据需要设置预留读/写吞吐量。创建数据表的同时支持创建一个或者多个索引表。
说明
- 创建数据表后需要几秒钟进行加载,在此期间对该数据表的读/写数据操作均会失败。请等待数据表加载完毕后再进行数据操作。
- 创建数据表时必须指定数据表的主键。主键包含1个~4个主键列,每一个主键列都有名称和类型。
前提条件
接口
//说明:根据指定的表结构信息创建数据表。
//request是CreateTableRequest类的实例,它包含TableMeta和TableOption以及ReservedThroughput。
//请参见TableMeta类的文档。
//当创建一个数据表后,通常需要等待几秒钟时间使partition load完成,才能进行各种操作。
//返回:CreateTableResponse
CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)
参数
参数 | 说明 |
---|---|
TableMeta | 数据表的结构信息,包括如下内容:
|
TableOption | 数据表的配置信息。更多信息,请参见数据版本和生命周期。 配置信息包括如下内容:
|
ReservedThroughput | 为数据表配置预留读吞吐量或预留写吞吐量。 容量型实例中的数据表的预留读/写吞吐量只能设置为0,不允许预留。 默认值为0,即完全按量计费。 单位为CU。
|
IndexMeta | 索引表的结构信息,包括如下内容:
|
示例
- 创建数据表(不带索引)
创建一个含有2个主键列,预留读/写吞吐量为(0, 0)的数据表。
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 response, err := client.CreateTable(createTableRequest) if (err != nil) { fmt.Println("Failed to create table with error:", err) } else { fmt.Println("Create table finished") } }
详细代码请参见CreateTable@GitHub。
- 创建数据表(带索引且索引类型为全局二级索引)
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("definedcol1", tablestore.DefinedColumn_STRING) tableMeta.AddDefinedColumn("definedcol2", tablestore.DefinedColumn_INTEGER) indexMeta := new(tablestore.IndexMeta) //新建索引表Meta。 indexMeta.AddPrimaryKeyColumn("definedcol1") //为索引表添加主键列。设置数据表的definedcol1列作为索引表的主键。 indexMeta.AddDefinedColumn("definedcol2") //为索引表添加属性列。设置数据表的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") } }
- 创建数据表(带索引且索引类型为本地二级索引)
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("definedcol1", tablestore.DefinedColumn_STRING) tableMeta.AddDefinedColumn("definedcol2", tablestore.DefinedColumn_INTEGER) indexMeta := new(tablestore.IndexMeta) //新建索引表Meta。 indexMeta.IndexType = IT_LOCAL_INDEX //设置索引类型为本地二级索引(IT_LOCAL_INDEX)。 indexMeta.AddPrimaryKeyColumn("pk1") //为索引表添加主键列。索引表的第一列主键必须与数据表的第一列主键相同。 indexMeta.AddPrimaryKeyColumn("definedcol1") //为索引表添加主键列。设置数据表的definedcol1列作为索引表的主键。 indexMeta.AddDefinedColumn("definedcol2") //为索引表添加属性列。设置数据表的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") } }