使用CreateTable介面建立資料表時,需要指定資料表的結構資訊和配置資訊,高效能執行個體中的資料表還可以根據需要設定預留讀/寫輸送量。建立資料表的同時支援建立一個或者多個索引表。
说明
- 建立資料表後需要幾秒鐘進行載入,在此期間對該資料表的讀/寫資料操作均會失敗。請等待資料表載入完畢後再進行資料操作。
- 建立資料表時必須指定資料表的主鍵。主鍵包含1個~4個主鍵列,每一個主鍵列都有名稱和類型。
前提條件
介面
//說明:根據指定的表結構資訊建立資料表。
//request是CreateTableRequest類的執行個體,它包含TableMeta和TableOption以及ReservedThroughput。
//請參見TableMeta類的文檔。
//當建立一個資料表後,通常需要等待幾秒鐘時間使partition load完成,才能進行各種操作。
//返回:CreateTableResponse
CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)
參數
參數 | 說明 |
---|---|
TableMeta | 資料表的結構資訊,包括如下內容:
|
TableOption | 資料表的配置資訊。更多資訊,請參見資料版本和生命週期。
配置資訊包括如下內容:
|
ReservedThroughput | 為資料表配置預留讀輸送量或預留寫輸送量。
容量型執行個體中的資料表的預留讀/寫輸送量只能設定為0,不允許預留。 預設值為0,即完全隨用隨付。 單位為CU。
|
IndexMeta | 索引表的結構資訊,包括如下內容:
|
樣本
- 建立資料表(不帶索引)
建立一個含有2個主鍵列,預留讀/寫輸送量為(0, 0)的資料表。
func CreateTableSample(client *tablestore.TableStoreClient, tableName string) { createTableRequest := new(tablestore.CreateTableRequest) //建立主鍵列的schema,包括PK的個數、名稱和類型。 //第一個PK列為整數,名稱是pk0,此列同時也是分區鍵。 //第二個PK列為整數,名稱是pk1。 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") } }
詳細代碼請參見CreateTable@GitHub。
- 建立資料表(帶索引且索引類型為全域二級索引)
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) //建立索引表Meta。 indexMeta.AddPrimaryKeyColumn("definedcol1") //為索引表添加主鍵列。設定資料表的definedcol1列作為索引表的主鍵。 indexMeta.AddDefinedColumn("definedcol2") //為索引表添加屬性列。設定資料表的definedcol2列作為索引表的屬性列。 indexMeta.IndexName = "indexSample" tableOption := new(tablestore.TableOption) tableOption.TimeToAlive = -1 //資料的到期時間,單位為秒,-1表示永不到期。帶索引表的資料表資料生命週期必須設定為-1。 tableOption.MaxVersion = 1 //儲存的最大版本數,1表示每列上最多儲存一個版本即儲存最新的版本。帶索引表的資料表最大版本數必須設定為1。 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") } }
- 建立資料表(帶索引且索引類型為本地二級索引)
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("definedcol1", tablestore.DefinedColumn_STRING) tableMeta.AddDefinedColumn("definedcol2", tablestore.DefinedColumn_INTEGER) indexMeta := new(tablestore.IndexMeta) //建立索引表Meta。 indexMeta.IndexType = IT_LOCAL_INDEX //設定索引類型為本地二級索引(IT_LOCAL_INDEX)。 indexMeta.AddPrimaryKeyColumn("pk1") //為索引表添加主鍵列。索引表的第一列主鍵必須與資料表的第一列主鍵相同。 indexMeta.AddPrimaryKeyColumn("definedcol1") //為索引表添加主鍵列。設定資料表的definedcol1列作為索引表的主鍵。 indexMeta.AddDefinedColumn("definedcol2") //為索引表添加屬性列。設定資料表的definedcol2列作為索引表的屬性列。 indexMeta.IndexName = "indexSample" tableOption := new(tablestore.TableOption) tableOption.TimeToAlive = -1 //資料的到期時間,單位為秒,-1表示永不到期。帶索引表的資料表資料生命週期必須設定為-1。 tableOption.MaxVersion = 1 //儲存的最大版本數,1表示每列上最多儲存一個版本即儲存最新的版本。帶索引表的資料表最大版本數必須設定為1。 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") } }