Use the Tablestore SDK for Go to create a Wide Column model table and configure its schema, properties, Stream settings, local transactions, secondary indexes, and data encryption.
Prerequisites
Install the Tablestore SDK for Go and initialize the client.
Description
Call CreateTable to create a table. You must specify the table schema, table options, and reserved throughput. You can also configure secondary indexes, Stream, local transactions, and data encryption.
func (tableStoreClient *TableStoreClient) CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)
After you create a table, wait until the table is loaded before you perform data operations. The loading process usually takes a few seconds.
The following sample creates the example_table table with the user_id and record_id primary key columns.
tableMeta := &tablestore.TableMeta{TableName: "example_table"}
tableMeta.AddPrimaryKeyColumn("user_id", tablestore.PrimaryKeyType_STRING)
tableMeta.AddPrimaryKeyColumn("record_id", tablestore.PrimaryKeyType_INTEGER)
request := &tablestore.CreateTableRequest{
TableMeta: tableMeta,
TableOption: &tablestore.TableOption{
TimeToAlive: -1,
MaxVersion: 1,
DeviationCellVersionInSec: 86400,
},
ReservedThroughput: &tablestore.ReservedThroughput{},
}
_, err := client.CreateTable(request)
if err != nil {
log.Fatal(err)
}
Parameters
CreateTableRequest contains the following parameters.
|
Name |
Type |
Description |
|
TableMeta (required) |
|
The table schema. |
|
TableOption (required) |
|
The table options. |
|
ReservedThroughput (required) |
|
The reserved read/write throughput, in CUs. The default read and write CUs are both |
|
IndexMetas (optional) |
|
The secondary indexes to create with the table. |
|
StreamSpec (optional) |
|
The Stream settings. |
|
EnableLocalTxn (optional) |
|
Specifies whether to enable local transactions. Default value: |
|
SSESpecification (optional) |
|
The data encryption settings. Data encryption can be enabled and configured only when the table is created. It cannot be disabled or modified after creation. |
Table schema
TableMeta contains the following parameters.
|
Name |
Type |
Description |
|
TableName (required) |
|
The table name. |
|
SchemaEntry (required) |
|
The primary key schema. A table must contain one to four primary key columns. The first primary key column is the partition key. |
|
DefinedColumns (optional) |
|
The predefined columns. Predefined columns can be used to create secondary indexes. |
Primary key column
Each element in TableMeta.SchemaEntry is of the PrimaryKeySchema type and contains the following parameters.
|
Name |
Type |
Description |
|
Name (required) |
|
The primary key column name. |
|
Type (required) |
|
The primary key column type. Valid values: |
|
Option (optional) |
|
The primary key column option. A non-partition INTEGER primary key column can be set to |
Predefined column
Each element in TableMeta.DefinedColumns is of the DefinedColumnSchema type and contains the following parameters.
|
Name |
Type |
Description |
|
Name (required) |
|
The predefined column name. |
|
ColumnType (required) |
|
The predefined column type. Valid values: |
Table options
TableOption contains the following parameters.
|
Name |
Type |
Description |
|
TimeToAlive (required) |
|
The time to live (TTL), in seconds. A value of |
|
MaxVersion (required) |
|
The maximum number of versions retained for each attribute column. To use a search index or secondary index, set this parameter to |
|
DeviationCellVersionInSec (optional) |
|
The maximum version offset, in seconds. Default value: |
|
AllowUpdate (optional) |
|
Specifies whether data can be updated by calling |
|
UpdateFullRow (optional) |
|
Specifies whether to enable full-row updates. This parameter can be configured only when the table is created and cannot be modified by calling |
Reserved throughput
ReservedThroughput contains the following parameters.
|
Name |
Type |
Description |
|
Readcap (optional) |
|
The reserved read throughput, in CUs. Default value: |
|
Writecap (optional) |
|
The reserved write throughput, in CUs. Default value: |
Secondary index
Each element in IndexMetas is of the IndexMeta type and contains the following parameters.
|
Name |
Type |
Description |
|
IndexName (required) |
|
The secondary index name. |
|
Primarykey (required) |
|
The index primary key columns, which can consist of table primary key columns and predefined columns. For a local secondary index, the first index primary key column must be the first table primary key column. |
|
DefinedColumns (optional) |
|
The index predefined columns. |
|
IndexType (optional) |
|
The index type. |
Stream settings
StreamSpec is of the StreamSpecification type and contains the following parameters.
|
Name |
Type |
Description |
|
EnableStream (optional) |
|
Specifies whether to enable Stream. Default value: |
|
ExpirationTime (optional) |
|
The retention period of incremental logs, in hours. Maximum value: |
Local transaction settings
Set EnableLocalTxn to true to enable local transactions. Take note of the following items:
This parameter is supported in Tablestore SDK for Go 1.7.8 and later. We recommend that you use the latest version.
Local transactions and auto-increment primary key columns cannot be used together. If an auto-increment primary key column is configured, local transactions do not take effect even when enabled.
To enable local transactions on an existing table, submit a ticket.
Data encryption settings
SSESpecification contains the following parameters.
|
Name |
Type |
Description |
|
Enable (optional) |
|
Specifies whether to enable data encryption. Default value: |
|
KeyType (optional) |
|
The encryption type. |
|
KeyId (optional) |
|
The customer master key ID. This parameter is required for BYOK encryption. |
|
RoleArn (optional) |
|
The RAM role ARN. This parameter is required for BYOK encryption. |
Examples
Create a table with Stream enabled
The following sample enables Stream when the table is created and sets the incremental log retention period to 168 hours.
tableMeta := &tablestore.TableMeta{TableName: "stream_table"}
tableMeta.AddPrimaryKeyColumn("id", tablestore.PrimaryKeyType_STRING)
request := &tablestore.CreateTableRequest{
TableMeta: tableMeta,
TableOption: &tablestore.TableOption{TimeToAlive: -1, MaxVersion: 1},
ReservedThroughput: &tablestore.ReservedThroughput{},
StreamSpec: &tablestore.StreamSpecification{
EnableStream: true,
ExpirationTime: 168,
},
}
_, err := client.CreateTable(request)
if err != nil {
log.Fatal(err)
}
Create a table with local transactions enabled
The following sample enables local transactions when the table is created.
tableMeta := &tablestore.TableMeta{TableName: "transaction_table"}
tableMeta.AddPrimaryKeyColumn("user_id", tablestore.PrimaryKeyType_STRING)
tableMeta.AddPrimaryKeyColumn("record_id", tablestore.PrimaryKeyType_INTEGER)
request := &tablestore.CreateTableRequest{
TableMeta: tableMeta,
TableOption: &tablestore.TableOption{TimeToAlive: -1, MaxVersion: 1},
ReservedThroughput: &tablestore.ReservedThroughput{},
EnableLocalTxn: proto.Bool(true),
}
_, err := client.CreateTable(request)
if err != nil {
log.Fatal(err)
}