This topic describes how to create a data table by calling the CreateTable operation. When you call the CreateTable operation to create a data table, you must specify schema information and configuration information for the data table. If the data table belongs to a high-performance instance, you can configure the reserved read throughput and the reserved write throughput based on your business requirements. You can create one or more index tables when you create a data table.
Usage notes
It takes several seconds to load a data table after the data table is created. During this period, all read and write operations on the data table fail. Perform operations on the data table after the data table is loaded.
You must specify the primary key when you create a data table. A primary key consists of one to four primary key columns. Specify a name and data type for each primary key column.
In system design scenarios that require an auto-increment primary key column, such as item IDs on e-commerce websites, user IDs on large websites, post IDs in forums, and message IDs in chat tools, you can specify an auto-increment primary key column when you create a data table. For more information, see Configure an auto-increment primary key column.
Prerequisites
An instance is created. For more information, see Step 2: Create an instance.
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
API operation
// Description: Create a data table based on the specified table schema.
// request is an instance of the CreateTableRequest class and contains TableMeta, TableOption, and ReservedThroughput.
// For more information, see the documentation of the TableMeta class.
// After you create a data table, several seconds are required to load the partitions in the data table. You can perform operations on the data table only after the partitions are loaded.
// Response: CreateTableResponse
CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)
Parameters
Configure the parameters in the code based on the parameters described in the following table and the "Request syntax" section of the CreateTable topic.
Parameter | Required | Description |
TableMeta | Yes | The schema of the data table. This parameter includes the following configuration items:
|
TableOption | No | Optional. The configuration information about the data table. For more information, see Data versions and TTL. The configuration information includes the following items:
|
ReservedThroughput | No | Optional. The reserved read throughput and the reserved write throughput of the data table. Important You can set the reserved read throughput and the reserved write throughput only to 0 for data tables in capacity instances. Reserved throughput does not apply to the instances. The default value 0 indicates that you are charged for all throughput on a pay-as-you-go basis. Unit: capacity unit (CU).
|
IndexMeta | No | Optional. The schema information about the index table. This parameter includes the following configuration items: Important If you want to create a data table and a secondary index at the same time, you must configure this parameter.
|
Examples
Create a data table without an index table
The following sample code provides an example on how to create a data table that contains two primary key columns. In this example, the two primary key columns are named pk0 of the Integer type and pk1 of the String type. Up to three versions of data can be retained for each attribute column and data in the data table never expires.
func CreateTableSample(client *tablestore.TableStoreClient, tableName string) {
createTableRequest := new(tablestore.CreateTableRequest)
// Create a schema for primary key columns, which includes the number, names, and types of the 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 a String 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
_, err := client.CreateTable(createTableRequest)
if (err != nil) {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create table finished")
}
}
Create a data table and a global secondary index
The following sample code provides an example on how to create a data table and configure a global secondary index for the data table at the same time. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. The data table also contains two predefined columns: the defcol1 column of the STRING type and the defcol2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires. The primary key columns of the global secondary index are the defcol1, pk1, and pk2 columns. The attribute column of the global secondary index is the defcol2 column.
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) // Create IndexMeta for the index table.
indexMeta.AddPrimaryKeyColumn("defcol1") // Specify a primary key column for the index table. Specify the definedcol1 column of the data table as a primary key column of the index table.
indexMeta.AddDefinedColumn("defcol2") // Specify attribute columns for the index table. Specify the definedcol2 column of the data table as an attribute column of the index table.
indexMeta.IndexName = "indexSample"
tableOption := new(tablestore.TableOption)
tableOption.TimeToAlive = -1 // Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the TimeToAlive parameter to -1 for a data table for which index tables are created.
tableOption.MaxVersion = 1 // Specify the maximum number of versions that can be retained for each attribute column. A value of 1 specifies that only the latest version is retained for each attribute column. You must set the MaxVersion parameter to 1 for a data table for which index tables are created.
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")
}
}
Create a data table and a local secondary index
The following sample code provides an example on how to create a data table and configure a local secondary index for the data table at the same time. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. The data table also contains two predefined columns: the defcol1 column of the STRING type and the defcol2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires. The primary key columns of the local secondary index are the defcol1, pk1, and pk2 columns. The attribute column of the local secondary index is the defcol2 column.
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) // Create IndexMeta for the index table.
indexMeta.IndexType = tablestore.IT_LOCAL_INDEX // Set the IndexType parameter to IT_LOCAL_INDEX to create a local secondary index.
indexMeta.AddPrimaryKeyColumn("pk1") // Specify a primary key column for the index table. The first primary key column of the index table must be the same as the first primary key column of the data table.
indexMeta.AddPrimaryKeyColumn("defcol1") // Specify a primary key column for the index table. Specify the definedcol1 column of the data table as a primary key column of the index table.
indexMeta.AddDefinedColumn("defcol2") // Specify attribute columns for the index table. Specify the definedcol2 column of the data table as an attribute column of the index table.
indexMeta.IndexName = "indexSample"
tableOption := new(tablestore.TableOption)
tableOption.TimeToAlive = -1 // Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the TimeToAlive parameter to -1 for a data table for which index tables are created.
tableOption.MaxVersion = 1 // Specify the maximum number of versions that can be retained for each attribute column. A value of 1 specifies that only the latest version is retained for each attribute column. You must set the MaxVersion parameter to 1 for a data table for which index tables are created.
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")
}
}
References
For information about the API operation that you can call to create a data table, see CreateTable. For the detailed sample code, visit CreateTable@GitHub.
You can call API operations to read and write data in a data table. For more information, see Basic operations on data.
You can update a table to modify the information about the table, such as the time to live (TTL) and max versions. For more information, see Update a table.
After you enable the local transaction feature for a data table, you can create a local transaction and perform read and write operations on the data in the local transaction. For more information, see Local transaction.
You can query the names of tables to view all existing tables in an instance. For more information, see Query the names of tables.
You can query the description of a table to view the configuration information about the table, such as the max versions and TTL. For more information, see Query the description of a table.
If you want to use a secondary index to accelerate data queries and no predefined columns are specified when the data table is created, you can add or remove predefined columns to the data table after the data table is created. For more information, see Add or remove predefined columns.
You can delete a data table that you no longer require. For more information, see Delete tables.