使用CreateTable介面建立資料表時,需要指定資料表的結構資訊和配置資訊,高效能執行個體中的資料表還可以根據需要設定預留讀/寫輸送量。建立資料表的同時支援建立一個或者多個索引表。
说明
- 建立資料表後需要幾秒鐘進行載入,在此期間對該資料表的讀/寫資料操作均會失敗。請等待資料表載入完畢後再進行資料操作。
- 建立資料表時必須指定資料表的主鍵。主鍵包含1個~4個主鍵列,每一個主鍵列都有名稱和類型。
前提條件
參數
參數 | 說明 |
---|---|
tableMeta | 資料表的結構資訊,包括如下內容:
|
tableOptions | 資料表的配置資訊。更多資訊,請參見資料版本和生命週期。 配置資訊包括如下內容:
|
reservedThroughtput | 為資料表配置預留讀輸送量或預留寫輸送量。 容量型執行個體中的資料表的預留讀/寫輸送量只能設定為0,不允許預留。 預設值為0,即完全隨用隨付。 單位為CU。
|
localTxnEnabled | 是否開啟局部事務功能。類型為Boolean。預設值為false,表示不開啟局部事務。 如果要在建立資料表時開啟局部事務,請設定此參數為true。 重要
|
indexMetas | 索引表的結構資訊,每個indexMeta都包括如下內容:
|
樣本
- 建立資料表時不帶索引
private static void createTable(SyncClient client) { TableMeta tableMeta = new TableMeta("TableName"); tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema(PRIMARY_KEY_NAME, PrimaryKeyType.STRING)); //為主表添加主鍵列。 int timeToLive = -1; //資料的到期時間,單位為秒,-1表示永不到期。帶索引表的資料表資料生命週期必須設定為-1。 int maxVersions = 3; //儲存的最大版本數,1表示每列上最多儲存一個版本即儲存最新的版本。帶索引表的資料表最大版本數必須設定為1。 TableOptions tableOptions = new TableOptions(timeToLive, maxVersions); CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions); request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0))); //設定預留讀寫輸送量,容量型執行個體中的資料表只能設定為0,高效能執行個體中的資料表可以設定為非零值。 client.createTable(request); }
- 建立資料表時帶索引且索引類型為全域二級索引
private static void createTable(SyncClient client) { TableMeta tableMeta = new TableMeta("TableName"); tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING)); //為資料表添加主鍵列。 tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER)); //為資料表添加主鍵列。 tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING)); //為資料表添加預定義列。 tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER)); //為資料表添加預定義列。 int timeToLive = -1; //資料的到期時間,單位為秒,-1表示永不到期。帶索引表的資料表資料生命週期必須設定為-1。 int maxVersions = 1; //儲存的最大版本數,1表示每列上最多儲存一個版本即儲存最新的版本。帶索引表的資料表最大版本數必須設定為1。 TableOptions tableOptions = new TableOptions(timeToLive, maxVersions); ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>(); IndexMeta indexMeta = new IndexMeta("IndexName"); indexMeta.addPrimaryKeyColumn("defcol1"); //為索引表添加主鍵列。 indexMeta.addDefinedColumn("defcol2"); //為索引表添加屬性列。 indexMetas.add(indexMeta); CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); //建立資料表的同時建立索引表。 client.createTable(request); }
- 建立資料表時帶索引且索引類型為本地二級索引
private static void createTable(SyncClient client) { TableMeta tableMeta = new TableMeta("TableName"); tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING)); //為資料表添加主鍵列。 tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER)); //為資料表添加主鍵列。 tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING)); //為資料表添加預定義列。 tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER)); //為資料表添加預定義列。 int timeToLive = -1; //資料的到期時間,單位為秒,-1表示永不到期。帶索引表的資料表資料生命週期必須設定為-1。 int maxVersions = 1; //儲存的最大版本數,1表示每列上最多儲存一個版本即儲存最新的版本。帶索引表的資料表最大版本數必須設定為1。 TableOptions tableOptions = new TableOptions(timeToLive, maxVersions); ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>(); IndexMeta indexMeta = new IndexMeta("IndexName"); indexMeta.setIndexType(IT_LOCAL_INDEX); //設定索引類型為本地二級索引(IT_LOCAL_INDEX)。 indexMeta.setIndexUpdateMode(IUM_SYNC_INDEX); //設定索引更新模式為同步更新(IUM_SYNC_INDEX)。當索引類型為本地二級索引時,索引更新模式必須為同步更新。 indexMeta.addPrimaryKeyColumn("pk1"); //為索引表添加主鍵列。索引表的第一列主鍵必須與資料表的第一列主鍵相同。 indexMeta.addPrimaryKeyColumn("defcol1"); //為索引表添加主鍵列。 indexMeta.addDefinedColumn("defcol2"); //為索引表添加屬性列。 indexMetas.add(indexMeta); CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); //建立資料表的同時建立索引表。 client.createTable(request); }
- 建立資料表時開啟局部事務功能
private static void createTable(SyncClient client) { TableMeta tableMeta = new TableMeta("TableName"); tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING)); tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER)); int timeToLive = -1; //資料的到期時間,單位為秒,-1表示永不到期。帶索引表的資料表資料生命週期必須設定為-1。 int maxVersions = 1; //儲存的最大版本數,1表示每列上最多儲存一個版本即儲存最新的版本。帶索引表的資料表最大版本數必須設定為1。 TableOptions tableOptions = new TableOptions(timeToLive, maxVersions); CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions); request.setLocalTxnEnabled(true); //開啟局部事務。當為資料表配置了主鍵自增列時,此配置無效。 client.createTable(request); }