使用CreateTable介面建立資料表時,需要指定資料表的結構資訊和配置資訊,高效能執行個體中的資料表還可以根據需要設定預留讀/寫輸送量。建立資料表的同時支援建立一個或者多個索引表。
说明
- 建立資料表後需要幾秒鐘進行載入,在此期間對該資料表的讀/寫資料操作均會失敗。請等待資料表載入完畢後再進行資料操作。
- 建立資料表時必須指定資料表的主鍵。主鍵包含1個~4個主鍵列,每一個主鍵列都有名稱和類型。
前提條件
介面
/**
* 根據指定的表結構資訊建立相應的資料表。
*/
createTable(params, callback)
參數
參數 | 說明 |
---|---|
tableMeta | 資料表的結構資訊,包括如下內容:
|
tableOptions | 資料表的配置資訊。更多資訊,請參見資料版本和生命週期。
配置資訊包括如下內容:
|
reservedThroughput | 為資料表配置預留讀輸送量或預留寫輸送量。
容量型執行個體中的資料表的預留讀/寫輸送量只能設定為0,不允許預留。 預設值為0,即完全隨用隨付。 單位為CU。
|
indexMetas | 索引表的結構資訊,每個indexMeta包括如下內容:
|
樣本
- 建立資料表(不帶索引)
建立一個有2個主鍵列,預留讀/寫輸送量為(0, 0)的資料表。
var client = require('./client'); var params = { tableMeta: { tableName: 'sampleTable', primaryKey: [ { name: 'gid', type: 'INTEGER' }, { name: 'uid', type: 'INTEGER' } ] }, reservedThroughput: { capacityUnit: { read: 0, write: 0 } }, tableOptions: { timeToLive: -1, //資料的到期時間,單位為秒,-1代表永不到期。如果設定到期時間為一年,即為365*24*3600。 maxVersions: 1 //儲存的最大版本數,設定為1代表每列上最多儲存一個版本(儲存最新的版本)。 } }; client.createTable(params, function (err, data) { if (err) { console.log('error:', err); return; } console.log('success:', data); });
詳細代碼請參見CreateTable@GitHub。
- 建立資料表(帶索引且索引類型為全域二級索引)
var client = require('./client'); var TableStore = require('../index.js'); var params = { tableMeta: { tableName: 'sdkGlobalTest', primaryKey: [ { name: 'pk1', type: TableStore.PrimaryKeyType.INTEGER }, { name: 'pk2', type: TableStore.PrimaryKeyType.INTEGER } ], definedColumn: [ { "name": "col1", "type": TableStore.DefinedColumnType.DCT_INTEGER }, { "name": "col2", "type": TableStore.DefinedColumnType.DCT_INTEGER } ], }, reservedThroughput: { capacityUnit: { read: 0, write: 0 } }, tableOptions: { timeToLive: -1, //資料的到期時間,單位為秒,-1表示永不到期。帶索引的資料表資料生命週期必須設定為-1。 maxVersions: 1 //儲存的最大版本數,1表示每列上最多儲存一個版本即儲存最新的版本。帶索引的資料表最大版本數必須設定為1。 }, streamSpecification: { enableStream: false, //二級索引不支援開啟Stream。 }, indexMetas: [ { name: "sdkGlobalIndex1", primaryKey: ["pk2"], definedColumn: ["col1", "col2"] }, { name: "sdkGlobalIndex2", primaryKey: ["col1"], definedColumn: ["col2"] } ] }; client.createTable(params, function (err, data) { if (err) { console.log('error:', err); return; } console.log('success:', data); });
- 建立資料表(帶索引且索引類型為本地二級索引)
var client = require('./client'); var TableStore = require('../index.js'); var params = { tableMeta: { tableName: 'sdkLocalTest', primaryKey: [ { name: 'pk1', type: TableStore.PrimaryKeyType.INTEGER }, { name: 'pk2', type: TableStore.PrimaryKeyType.INTEGER } ], definedColumn: [ { "name": "col1", "type": TableStore.DefinedColumnType.DCT_INTEGER }, { "name": "col2", "type": TableStore.DefinedColumnType.DCT_INTEGER } ], }, reservedThroughput: { capacityUnit: { read: 0, write: 0 } }, tableOptions: { timeToLive: -1, //資料的到期時間,單位為秒,-1表示永不到期。帶索引的資料表資料生命週期必須設定為-1。 maxVersions: 1 //儲存的最大版本數,1表示每列上最多儲存一個版本即儲存最新的版本。帶索引的資料表最大版本數必須設定為1。 }, streamSpecification: { enableStream: false, //二級索引不支援開啟Stream。 }, indexMetas: [ { name: "sdklocalIndex1", primaryKey: ["pk1","col1"],//為索引表添加主鍵列。索引表的第一列主鍵必須與資料表的第一列主鍵相同。 definedColumn: ["col2"], indexUpdateMode: TableStore.IndexUpdateMode.IUM_SYNC_INDEX,//設定索引更新模式為同步更新(IUM_SYNC_INDEX)。當索引類型為本地二級索引時,索引更新模式必須為同步更新。 indexType: TableStore.IndexType.IT_LOCAL_INDEX,//設定索引類型為本地二級索引(IT_LOCAL_INDEX)。 }, { name: "sdklocalIndex2", primaryKey: ["pk1","col2"], definedColumn: ["col1"], indexUpdateMode: TableStore.IndexUpdateMode.IUM_SYNC_INDEX,//設定索引更新模式為同步更新(IUM_SYNC_INDEX)。當索引類型為本地二級索引時,索引更新模式必須為同步更新。 indexType: TableStore.IndexType.IT_LOCAL_INDEX,//設定索引類型為本地二級索引(IT_LOCAL_INDEX)。 } ] }; client.createTable(params, function (err, data) { if (err) { console.log('error:', err); return; } console.log('success:', data); });