This topic describes how to create a table by calling the CreateTable operation. You must specify the schema information and configuration information of the table when you call the CreateTable operation. You can set reserved read and write throughput for tables in high performance instances. You can create one or more index tables when you create the base table.
- After a table is created, it takes several seconds to load the table. During this period, the read and write operations performed on the table may fail. Perform operations on a table after the table is loaded.
- You must specify the primary key when you create a table. A primary key can contain one to four primary key columns. Each primary key column has a name and a data type.
Prerequisites
- An instance is created in the console. For more information, see Create instances.
- OTSClient is initialized. For more information, see Initialization.
Parameters
Parameter | Description |
---|---|
tableMeta | The schema information of the table, which includes the following items:
|
tableOptions | The configurations of the table. For more information, see Max versions and TTL.
The configurations include the following items:
|
reservedThroughtput | The reserved read and write throughput of the table.
For capacity instances, the reserved read and write throughput can be set only to 0. Reserved throughput does not apply to these instances. The default value 0 indicates that all throughput is billed on a pay-as-you-go basis. Unit: CU.
|
indexMetas | The schema information of the index tables. Each indexMeta includes the following
items:
|
Examples
- Create a base table without creating an index table
private static void createTable(SyncClient client) { TableMeta tableMeta = new TableMeta(TABLE_NAME); tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema(PRIMARY_KEY_NAME, PrimaryKeyType.STRING)); // Specify a primary key column for the base table. int timeToLive = -1; // Specify the validity period of data in seconds. A value of -1 indicates that the data never expires. You must set timeToLive to -1 for the base table that has index tables. int maxVersions = 3; // Specify the maximum number of versions that can be retained for each column. A value of 1 indicates that only the latest version is retained for each column. You must set maxVersions to 1 for the base table that has index tables. TableOptions tableOptions = new TableOptions(timeToLive, maxVersions); CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions); request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0))); // Set the reserved read and write throughout. For capacity instances, the reserved read and write throughput can only be set to 0. However, you can set a non-zero value for high-performance instances. client.createTable(request); }
- Create an index table when you create the base table
private static void createTable(SyncClient client) { TableMeta tableMeta = new TableMeta(TABLE_NAME); tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema(PRIMARY_KEY_NAME_1, PrimaryKeyType.STRING)); // Add a primary key column to the table. tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema(PRIMARY_KEY_NAME_2, PrimaryKeyType.INTEGER)); // Add a primary key column to the table. tableMeta.addDefinedColumn(new DefinedColumnSchema(DEFINED_COL_NAME_1, DefinedColumnType.STRING)); // Add a predefined attribute column to the table. tableMeta.addDefinedColumn(new DefinedColumnSchema(DEFINED_COL_NAME_2, DefinedColumnType.INTEGER)); // Add a predefined attribute column to the table. int timeToLive = -1; // Specify the validity period of data in seconds. A value of -1 indicates that the data never expires. You must set timeToLive to -1 for the base table that has index tables. int maxVersions = 1; // Specify the maximum number of versions that can be saved in each column. A value of 1 indicates that only the latest version is saved in each column. You must set maxVersions to 1 for the base table that has index tables. TableOptions tableOptions = new TableOptions(timeToLive, maxVersions); ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>(); IndexMeta indexMeta = new IndexMeta(INDEX_NAME); indexMeta.addPrimaryKeyColumn(DEFINED_COL_NAME_1); // Add a primary key column to the index table. indexMeta.addDefinedColumn(DEFINED_COL_NAME_2); // Add an attribute column to the index table. indexMetas.add(indexMeta); CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); // Create an index table when you create a table. client.createTable(request); }