使用CreateTable介面建立資料表時,需要指定資料表的結構資訊和配置資訊,高效能執行個體中的資料表還可以根據需要設定預留讀/寫輸送量。建立資料表的同時支援建立一個或者多個索引表。
说明
- 建立資料表後需要幾秒鐘進行載入,在此期間對該資料表的讀/寫資料操作均會失敗。請等待資料表載入完畢後再進行資料操作。
- 建立資料表時必須指定資料表的主鍵。主鍵包含1個~4個主鍵列,每一個主鍵列都有名稱和類型。
前提條件
介面
/// <summary>
/// 根據表資訊(包含資料表名稱、主鍵的定義和預留讀寫輸送量)建立資料表。
/// </summary>
/// <param name="request">請求參數</param>
/// <returns>CreateTable的返回,此返回執行個體是空的,不包含具體資訊。
/// </returns>
public CreateTableResponse CreateTable(CreateTableRequest request);
/// <summary>
/// CreateTable的非同步形式。
/// </summary>
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request);
參數
參數 | 說明 |
---|---|
tableMeta | 資料表的結構資訊,包括如下內容:
|
tableOptions | 資料表的配置資訊,詳情請參見資料版本和生命週期。
配置資訊包括如下內容:
|
reservedThroughput | 為資料表配置預留讀輸送量或預留寫輸送量。
容量型執行個體中的資料表的預留讀/寫輸送量只能設定為0,不允許預留。 預設值為0,即完全隨用隨付。 單位為CU。
|
indexMetas | 索引表的結構資訊,每個indexMeta都包括如下內容:
|
樣本
- 建立資料表(不帶索引)
建立一個有2個主鍵列,預留讀/寫輸送量為(0, 0)的資料表。
//建立主鍵列的schema,包括PK的個數、名稱和類型。 //第一個PK列為整數,名稱是pk0,這個同時也是分區鍵。 //第二個PK列為字串,名稱是pk1。 var primaryKeySchema = new PrimaryKeySchema(); primaryKeySchema.Add("pk0", ColumnValueType.Integer); primaryKeySchema.Add("pk1", ColumnValueType.String); //通過表名和主鍵列的schema建立一個tableMeta。 var tableMeta = new TableMeta("SampleTable", primaryKeySchema); //設定預留讀輸送量為0,預留寫輸送量為0。 var reservedThroughput = new CapacityUnit(0, 0); try { //構造CreateTableRequest對象。 var request = new CreateTableRequest(tableMeta, reservedThroughput); //調用client的CreateTable介面,如果沒有拋出異常,則說明執行成功。 otsClient.CreateTable(request); Console.WriteLine("Create table succeeded."); } //如果拋出異常,則說明失敗,處理異常。 catch (Exception ex) { Console.WriteLine("Create table failed, exception:{0}", ex.Message); }
詳細代碼請參見CreateTable@GitHub。
- 建立資料表(帶索引)
public static void CreateTableWithGlobalIndex() { //建立資料表,兩列主鍵為Pk1、Pk2,預定義列為Col1、Col2。 //建立索引表,索引表中Col1放Pk0。 OTSClient otsClient = Config.GetClient(); Console.WriteLine("Start create table with globalIndex..."); PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema { { Pk1, ColumnValueType.String }, { Pk2, ColumnValueType.String } }; TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema); tableMeta.DefinedColumnSchema = new DefinedColumnSchema { { Col1, DefinedColumnType.STRING}, { Col2, DefinedColumnType.STRING} }; IndexMeta indexMeta = new IndexMeta(IndexName); indexMeta.PrimaryKey = new List<string>() { Col1 }; indexMeta.DefinedColumns = new List<string>() { Col2 }; //indexMeta.IndexType = IndexType.IT_GLOBAL_INDEX; //indexMeta.IndexUpdateModel = IndexUpdateMode.IUM_ASYNC_INDEX; List<IndexMeta> indexMetas = new List<IndexMeta>() { }; indexMetas.Add(indexMeta); CapacityUnit reservedThroughput = new CapacityUnit(0, 0); CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput, indexMetas); otsClient.CreateTable(request); Console.WriteLine("Table is created: " + TableName); }