全部產品
Search
文件中心

Tablestore:建立資料表

更新時間:Aug 12, 2026

Go SDK 可建立寬表模型資料表,並配置表結構、表屬性、Stream、局部事務、二級索引和資料加密等功能。

前提條件

安裝Tablestore Go SDK並初始化用戶端。

功能說明

調用 CreateTable 建立資料表。建立時必須指定表結構、表配置和預留讀寫輸送量,也可同時配置二級索引、Stream、局部事務和資料加密。

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

建立資料表後,等待資料表載入完成再進行資料操作。載入過程通常需要幾秒鐘。

以下樣本建立主鍵為 user_id 和 record_id 的 example_table 資料表。

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)
}

參數說明

CreateTableRequest 包含以下參數。

名稱

類型

說明

TableMeta(必選)

*TableMeta

表結構。

TableOption(必選)

*TableOption

表配置。

ReservedThroughput(必選)

*ReservedThroughput

預留讀寫輸送量,單位為 CU。讀寫 CU 預設均為 0。僅 CU 模式的高效能型執行個體支援設定非零值。

IndexMetas(可選)

[]*IndexMeta

隨資料表建立的二級索引列表。

StreamSpec(可選)

*StreamSpecification

Stream 配置。

EnableLocalTxn(可選)

*bool

是否開啟局部事務。預設值為 false。

SSESpecification(可選)

*SSESpecification

資料加密配置。只能在建立資料表時開啟和配置,建立後不能關閉或修改。

表結構

TableMeta 包含以下參數。

名稱

類型

說明

TableName(必選)

string

資料表名稱。

SchemaEntry(必選)

[]*PrimaryKeySchema

主鍵結構。資料表必須包含 1~4 個主鍵列,第一個主鍵列為分區鍵。

DefinedColumns(可選)

[]*DefinedColumnSchema

預定義列列表。預定義列可用於建立二級索引。

主鍵列

TableMeta.SchemaEntry[] 的類型為 PrimaryKeySchema,包含以下參數。

名稱

類型

說明

Name(必選)

*string

主鍵列名稱。

Type(必選)

*PrimaryKeyType

主鍵列類型。支援 PrimaryKeyType_STRING、PrimaryKeyType_INTEGER 和 PrimaryKeyType_BINARY。

Option(可選)

*PrimaryKeyOption

主鍵列配置。非分區的 INTEGER 主鍵列可設定為 AUTO_INCREMENT。更多資訊,請參見主鍵列自增。

預定義列

TableMeta.DefinedColumns[] 的類型為 DefinedColumnSchema,包含以下參數。

名稱

類型

說明

Name(必選)

string

預定義列名稱。

ColumnType(必選)

DefinedColumnType

預定義列類型。支援 STRING、INTEGER、BINARY、DOUBLE 和 BOOLEAN。

表配置

TableOption 包含以下參數。

名稱

類型

說明

TimeToAlive(必選)

int

資料生命週期,單位為秒。設定為 -1 時資料永不到期;設定為其他值時,最小值為 86400(1 天)。使用多元索引或二級索引時,需設定為 -1,或將 AllowUpdate 設定為 false。

MaxVersion(必選)

int

每個屬性列最多保留的版本數。使用多元索引或二級索引時必須設定為 1。

DeviationCellVersionInSec(可選)

int64

有效版本偏差,單位為秒,預設值為 86400(1 天)。寫入資料的時間戳記與系統目前時間的差值必須在有效版本偏差範圍內。屬性列資料的有效版本範圍為 [max(資料寫入時間-有效版本偏差, 資料寫入時間-資料生命週期), 資料寫入時間+有效版本偏差)。

AllowUpdate(可選)

*bool

是否允許通過 UpdateRow 更新資料。預設值為 true。

UpdateFullRow(可選)

*bool

是否啟用整行更新。只能在建立資料表時設定,不能通過 UpdateTable 修改。

預留讀寫輸送量

ReservedThroughput 包含以下參數。

名稱

類型

說明

Readcap(可選)

int

預留讀輸送量,單位為 CU,預設值為 0。

Writecap(可選)

int

預留寫輸送量,單位為 CU,預設值為 0。

二級索引

IndexMetas[] 的類型為 IndexMeta,包含以下參數。

名稱

類型

說明

IndexName(必選)

string

二級索引名稱。

Primarykey(必選)

[]string

索引主鍵列。可由資料表主鍵列和預定義列組成。本地二級索引的第一個主鍵列必須與資料表的第一個主鍵列相同。

DefinedColumns(可選)

[]string

索引預定義列。

IndexType(可選)

IndexType

索引類型。IT_GLOBAL_INDEX 表示全域二級索引,IT_LOCAL_INDEX 表示本地二級索引。預設值為 IT_GLOBAL_INDEX。

Stream 配置

StreamSpec 的類型為 StreamSpecification,包含以下參數。

名稱

類型

說明

EnableStream(可選)

bool

是否開啟 Stream,預設值為 false。

ExpirationTime(可選)

int32

增量日誌保留時間長度,單位為小時,最大值為 168(7 天)。EnableStream 設定為 true 時必須設定。

局部事務配置

EnableLocalTxn 設定為 true 時開啟局部事務。使用時需注意以下事項:

  • Go SDK 1.7.8 及以上版本支援此參數。建議使用最新版本。

  • 局部事務和主鍵列自增不能同時使用。如果配置了自增主鍵列,即使開啟局部事務也不會生效。

  • 如果建立資料表時未開啟局部事務,後續需要使用該功能,請提交工單。

資料加密配置

SSESpecification 包含以下參數。

名稱

類型

說明

Enable(可選)

bool

是否開啟資料加密,預設值為 false。

KeyType(可選)

*SSEKeyType

加密類型。SSE_KMS_SERVICE 表示 KMS 服務祕密金鑰加密,SSE_BYOK 表示 BYOK 加密。

KeyId(可選)

*string

使用者主要金鑰 ID。使用 BYOK 加密時必須設定。

RoleArn(可選)

*string

RAM 角色 ARN。使用 BYOK 加密時必須設定。

情境樣本

建立啟用 Stream 的資料表

以下樣本建立資料表時開啟 Stream,並將增量日誌保留時間長度設定為 168 小時。

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)
}

建立啟用局部事務的資料表

以下樣本建立資料表時開啟局部事務。

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)
}