本文介紹如何通過.NET SDK建立Table Store的資料表。
注意事項
建立資料表後,請等待資料表載入完成後再進行資料操作,否則資料操作會失敗,這個過程通常需要幾秒鐘。
前提條件
方法說明
public CreateTableResponse CreateTable(CreateTableRequest request)非同步方法呼叫:
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request)範例程式碼
以下範例程式碼建立了一張test_table表,該表包含1個 String類型的主鍵。
try
{
// 建立資料表至少需要添加一個主鍵
PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
{
{ "id", ColumnValueType.String }
};
// 構造資料表的結構資訊
TableMeta tableMeta = new TableMeta("test_table", primaryKeySchema);
// 建立資料表時必須設定預留讀寫輸送量(僅CU模式高效能執行個體支援設定資料表的預留讀寫輸送量為非零值)
CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
// 構造request並發起請求
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
client.CreateTable(request);
Console.WriteLine("Create table succeeded.");
}
catch (Exception ex)
{
Console.WriteLine($"Create table failed, exception:{ex.Message}");
}您也可以在建立資料表的同時參考範例程式碼進行以下設定。
添加預定義列
DefinedColumnSchema definedColumnSchema = new DefinedColumnSchema { { "name" , DefinedColumnType.STRING } }; tableMeta.DefinedColumnSchema = definedColumnSchema;設定有效版本偏差
TableOptions tableOptions = new TableOptions(); tableOptions.DeviationCellVersionInSec = 86400; request.TableOptions = tableOptions;設定是否允許更新
TableOptions tableOptions = new TableOptions(); tableOptions.AllowUpdate = false; request.TableOptions = tableOptions;添加二級索引
// 構造二級索引列表 List<IndexMeta> indexMetas = new List<IndexMeta>(); // 構造二級索引 IndexMeta indexMeta = new IndexMeta("test_table_index"); // 設定索引主鍵 indexMeta.PrimaryKey = new List<string>() { "id", "name" }; // 設定索引類型 indexMeta.IndexType = IndexType.IT_GLOBAL_INDEX; // 設定索引更新模型 indexMeta.IndexUpdateModel = IndexUpdateMode.IUM_ASYNC_INDEX; // 添加二級索引 indexMetas.Add(indexMeta); // 構造request請求 CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput, indexMetas);設定Stream資訊
StreamSpecification streamSpecification = new StreamSpecification(true); streamSpecification.ExpirationTime = 168; request.StreamSpecification = streamSpecification;