This topic describes how to create a table by calling the CreateTable operation. You must specify the schema information and configuration information of the table when you call the CreateTable operation. You can set reserved read and write throughput for tables in high performance instances. You can create one or more index tables when you create the base table.
- After a table is created, it takes several seconds to load the table. During this period, the read and write operations performed on the table may fail. Perform operations on a table after the table is loaded.
- You must specify the primary key when you create a table. A primary key can contain one to four primary key columns. Each primary key column has a name and a data type.
Prerequisites
- An instance is created in the console. For more information, see Create instances.
- TableStoreClient is initialized. For more information, see Initialization.
Operations
// Create a table based on the specified table schema.
// request is an instance of the CreateTableRequest class, which contains TableMeta, TableOption, and ReservedThroughput.
// For more information, see the documentation of the TableMeta class.
After you create a table, it takes several seconds to load the partitions in the table. You can perform operations on the table only after the partitions are loaded.
// Response: CreateTableResponse
CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)
Parameters
Parameter | Description |
---|---|
TableMeta | The schema information of the table, which includes the following items:
|
TableOption | The configurations of the table. For more information, see Max versions and TTL.
The configurations include the following items:
|
ReservedThroughput | The reserved read and write throughput of the table.
For capacity instances, the reserved read and write throughput can be set only to 0. Reserved throughput does not apply to these instances. The default value 0 indicates that all throughput is billed on a pay-as-you-go basis. Unit: CU.
|
IndexMeta | The schema information of the index table, which includes the following items:
|
Examples
- Create a base table without creating an index table
The following code provides an example on how to create a table that contains two primary key columns and a reserved read and write throughput of (0,0):
//Create a schema for the primary key columns, which includes the count, names, and types of primary key columns. //The first primary key column is named pk0 and requires an INTEGER value. The first primary key column is also the partition key. //The second primary key column is named pk1 and requires an INTEGER value. 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") }
For the complete sample code, visit CreateTable@GitHub.
- Create an index table when you create the base table
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) // Create an index table named Meta. indexMeta.AddPrimaryKeyColumn("definedcol1") // Set the definedcol1 column of the base table to the primary key column of the index table. indexMeta.AddDefinedColumn("definedcol2") // Set the definedcol2 column of the base table to the attribute column of the index table. indexMeta.IndexName = "indexSample" tableOption := new(tablestore.TableOption) tableOption.TimeToAlive = -1 // Specify the validity period of data in seconds. A value of -1 indicates that the data never expires. You must set TimeToAlive to -1 for the base table that has index tables. tableOption.MaxVersion = 1 // Specify the maximum number of versions that can be retained for each column. A value of 1 indicates that only the latest version is retained for each column. You must set MaxVersion to 1 for the base table that has index tables. 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") } }