All Products
Search
Document Center

Tablestore:Create a data table

Last Updated:Jul 23, 2025

This topic describes how to use Tablestore SDK for Go to create a data table.

Usage notes

After you create a data table, wait until the data table is loaded before you perform operations on data. Otherwise, the operations will fail. This process typically takes several seconds.

Prerequisites

Initialize a Tablestore client.

Method description

func (tableStoreClient *TableStoreClient) CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)

CreateTableRequest parameters

  • TableMeta (required) *TableMeta: The schema information of the table, including the following parameters.

    Parameter

    Type

    Description

    TableName (required)

    string

    The name of the data table.

    SchemaEntry (required)

    []*PrimaryKeySchema

    The information about the primary key.

    • You can configure 1 to 4 primary key columns. By default, primary key columns are sorted in ascending order. The first primary key column serves as the partition key.

    • The data types of primary key columns include STRING, INTEGER, and BINARY. You can configure auto-increment primary key columns for integer-type primary key columns that are not partition keys.

    DefinedColumns (optional)

    []*DefinedColumnSchema

    The information about predefined columns.

    • Predefined columns are attribute columns that are predefined. Use predefined columns to create secondary indexes and search indexes.

    • The data types of predefined columns include STRING, INTEGER, BINARY, DOUBLE, and BOOLEAN.

  • TableOption (required) *TableOption: The configuration information of the table, including the following parameters.

    Parameter

    Type

    Description

    TimeToAlive (required)

    int

    The time to live (TTL) of data. Unit: seconds.

    • If you set this parameter to -1, the data never expires. Otherwise, the minimum value is 86400 (one day). Data that exceeds the TTL is automatically cleared.

    • If you want to use search indexes or secondary indexes, you must set the TTL to -1 or set the AllowUpdate parameter to false.

    MaxVersion (required)

    int

    The maximum number of versions.

    • If you want to use search indexes of versions to 1.

    DeviationCellVersionInSec (optional)

    int64

    The maximum version offset. Unit: seconds. Default value: 86400 (one day).

    • The difference between the timestamp of the written data and the current system time must be within the maximum version offset. Otherwise, the written data fails to be written.

    • The valid version range for data in attribute columns is [max(Data write time - Maximum version offset, Data write time - TTL), Data write time + Maximum version offset).

    AllowUpdate (optional)

    *bool

    Specifies whether to allow updates. Default value: true.

    • If you set this parameter to false, you cannot update data by using the UpdateRow() method.

  • IndexMetas (optional) []*IndexMeta: The list of secondary indexes. Configure the following parameters for each index.

    Name

    Type

    Description

    IndexName (required)

    string

    The name of the index.

    Primarykey (required)

    []string

    The primary key columns of the index.

    • The primary key columns of an index are a combination of primary key columns and predefined columns of the data table for which the index is created.

    • If you want to create a local secondary index, the first primary key column of the index must be the first primary key column of the data table.

    DefinedColumns (optional)

    []string

    The predefined columns of the index.

    • Consists of predefined columns in the data table.

    IndexType (optional)

    IndexType

    The type of the index. Valid values:

    • IT_GLOBAL_INDEX (default): global secondary index.

    • IT_LOCAL_INDEX: local secondary index.

  • StreamSpec (optional) *StreamSpecification: The Stream configuration information, including the following parameters.

    Parameter

    Type

    Description

    EnableStream (optional)

    bool

    Specifies whether to enable Stream. Default value: false.

    ExpirationTime (optional)

    int32

    The expiration time of the Stream, which indicates the validity period of incremental logs. Unit: hours. Maximum value: 168 (seven days).

    • If EnableStream is set to true, you must set ExpirationTime.

  • EnableLocalTxn (optional) *bool: Specifies whether to enable local transactions. Default value: false. A value of false indicates that local transactions are disabled.

    • This feature is supported only in Go SDK 1.7.8 and later.

    • You cannot use the auto-increment primary key column feature and the local transaction feature at the same time. If you configure an auto-increment primary key column, the local transaction settings do not take effect even if you enable local transactions.

    • If the local transaction feature is disabled when you create a data table and you want to use the feature after the data table is created, submit a ticket to apply.

  • SSESpecification (optional) *SSESpecification: The data encryption settings, including the following parameters.

    Important

    The data encryption feature can be enabled and configured only when you create a data table. You cannot disable the encryption feature after the data table is created.

    Parameter

    Type

    Description

    Enable (optional)

    bool

    Specifies whether to enable the data encryption feature. Default value: false.

    KeyType (optional)

    *SSEKeyType

    The encryption type. Valid values:

    • SSE_KMS_SERVICE: Key Management Service (KMS)-based encryption.

    • SSE_BYOK: Bring Your Own Key (BYOK) encryption.

    KeyId (optional)

    *string

    The customer master key (CMK) ID. This parameter is required only when keyType is set to SSE_BYOK.

    RoleArn (optional)

    *string

    The Alibaba Cloud Resource Name (ARN) of the RAM role. This parameter is required only when keyType is set to SSE_BYOK.

  • ReservedThroughput (required) *ReservedThroughput: The reserved read and write throughput. Unit: capacity units (CUs). Default value: 0. Only high-performance instances in CU mode support setting this parameter to a non-zero value.

Sample code

The following sample code creates a data table named test_table that contains one primary key column of the string type.

func CreateTableSample(client *tablestore.TableStoreClient) {
    // Construct the schema information of the data table
    tableMeta := new(tablestore.TableMeta)
    tableMeta.TableName = "test_table"
    // At least one primary key column is required to create a data table
    tableMeta.AddPrimaryKeyColumn("id", tablestore.PrimaryKeyType_STRING)

    // Construct the configuration information of the data table
    tableOption := new(tablestore.TableOption)
    // You must specify the maximum number of versions when you create a data table
    tableOption.MaxVersion = 1
    // You must specify the TTL when you create a data table. A value of -1 indicates that the data never expires
    tableOption.TimeToAlive = -1

    // You must set the reserved read and write throughput when you create a data table. The default value is 0 (Only high-performance instances in CU mode support setting the reserved read and write throughput of a data table to a non-zero value)
    reservedThroughput := new(tablestore.ReservedThroughput)
    reservedThroughput.Readcap = 0
    reservedThroughput.Writecap = 0

    // Construct the request and send it
    createTableRequest := new(tablestore.CreateTableRequest)
    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.")
    }
}

Refer to the following sample code to configure additional settings when creating a data table.

  • Add predefined columns

    tableMeta.AddDefinedColumn("name", tablestore.DefinedColumn_STRING)
  • Set the maximum version offset

    tableOption.DeviationCellVersionInSec = 86400
  • Set whether to allow updates

    tableOption.AllowUpdate = proto.Bool(false)
  • Add a secondary index

    // Construct a secondary index
    indexMeta := new(tablestore.IndexMeta)
    indexMeta.IndexName = "test_table_index"
    // Set the primary key columns of the index
    indexMeta.AddPrimaryKeyColumn("id")
    indexMeta.AddPrimaryKeyColumn("name")
    // Set the index type
    indexMeta.IndexType = tablestore.IT_LOCAL_INDEX
    // Add the secondary index
    createTableRequest.AddIndexMeta(indexMeta)
  • Configure Stream settings

    streamSpec := new(tablestore.StreamSpecification)
    streamSpec.EnableStream = true
    streamSpec.ExpirationTime = 168
    createTableRequest.StreamSpec = streamSpec
  • Set whether to enable local transactions

    enableLocalTxn := proto.Bool(true)
    createTableRequest.EnableLocalTxn = enableLocalTxn
  • Set the data encryption type

    • KMS-based encryption

      sseSpec := new(tablestore.SSESpecification)
      sseSpec.SetEnable(true)
      sseSpec.SetKeyType(tablestore.SSE_KMS_SERVICE)
      createTableRequest.SSESpecification = sseSpec
    • BYOK encryption

      Note

      Before running the code, obtain the CMK ID and the ARN of the RAM role. For more information, see BYOK encryption.

      sseSpec := new(tablestore.SSESpecification)
      sseSpec.SetEnable(true)
      sseSpec.SetKeyType(tablestore.SSE_BYOK)
      sseSpec.SetKeyId("key-hzz65****************")
      sseSpec.SetRoleArn("acs:ram::1705************:role/tabletorebyok")
      createTableRequest.SSESpecification = sseSpec

References