This topic describes how to create a data table by calling the CreateTable operation. When you call the CreateTable operation, you must specify the schema information and configuration information about the data table. You can also configure the reserved read throughput and reserved write throughout based on your business requirements if the data table belongs to a high-performance instance. You can create one or more index tables when you create a data table.
Usage notes
It takes several seconds to load a data table after the data table is created. During this period, all read and write operations on the data table fail. Perform operations on the data table after the data table is loaded.
You must specify the primary key when you create a data table. A primary key consists of one to four primary key columns. Specify a name and data type for each primary key column.
You can enable data at rest encryption (DARE) by configuring data encryption settings when you create a data table. For more information, see Create an encrypted table.
You can configure an auto-increment primary key column for data such as item IDs on e-commerce websites, user IDs of large websites, post IDs on forums, and message IDs in chat tools when you create a data table. For more information, see Configure an auto-increment primary key column.
Prerequisites
A Tablestore instance is created in the Tablestore console. For more information, see Create instances.
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
Syntax
/**
* When you call the CreateTable operation to create a data table, you must specify the required parameters, including the parameters about the table metadata, reserved read and write throughput, table configurations, server-side encryption configurations, and whether to enable the local transaction feature.
*/
public class CreateTableRequest implements Request {
/** The schema information about the data table. */
private TableMeta tableMeta;
/** The information about index tables. */
private List<IndexMeta> indexMeta;
/** The reserved read and write throughput of the data table. */
private ReservedThroughput reservedThroughput;
/** The configurations of the data table, including time-to-live (TTL) and max versions. */
private TableOptions tableOptions;
/** The stream configurations of the data table. Generally, this parameter is not required. */
private OptionalValue<StreamSpecification> streamSpecification;
/** The server-side encryption configurations of the data table. */
private OptionalValue<SSESpecification> sseSpecification;
/** Specifies whether to enable the local transaction feature. */
private OptionalValue<Boolean> enableLocalTxn;
}
Parameters
Configure the parameters in the code based on the parameter description in the following table and the "Request syntax" section of the CreateTable topic.
Parameter | Description |
tableMeta | The schema information about the data table. The schema information contains the following parameters:
|
indexMetas | The schema information about the index table. Each indexMeta contains the following parameters:
|
tableOptions | The configuration information of the data table. For more information, see Data versions and TTL. The configuration information contains the following parameters:
You can call the UpdateTable operation to modify the configurations of a data table, including TTL and max versions. For more information, see Update table information. |
reservedThroughtput | The reserved read throughput and reserved write throughput of the data table. You can set the reserved read throughput and reserved write throughput only to 0 for data tables in capacity instances. Reserved throughputs do not apply to these instances. The default value 0 indicates that you are charged for all throughput on a pay-as-you-go basis. Unit: CU.
|
sseSpecification | The encryption configurations of the data table. Tablestore provides two encryption methods: encryption based on a Key Management Service (KMS) key and encryption based on Bring Your Own Key (BYOK). Select a method based on your business requirements. For more information, see Create an encrypted table. |
enableLocalTxn | Specifies whether to enable the local transaction feature. The value of this parameter is of the BOOLEAN type. Default value: false. A value of false indicates that the local transaction feature is disabled. If you want to enable the local transaction feature when you create a data table, set this parameter to true. Important
|
Examples
Create a data table without an index
The following sample code provides an example on how to create a data table without an index. The primary key column of the data table is the pk column of the STRING type. A maximum of three versions of data can be retained for each attribute column, and the data never expires.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Add a primary key column to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk", PrimaryKeyType.STRING));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each column. A value of 1 specifies that only the latest version of data is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 3;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0))); // Specify the reserved read throughput and reserved write throughout. The reserved read throughput and reserved write throughout can be set only to 0 for data tables in capacity instances. You can set the reserved read throughput and reserved write throughout to a non-zero value for data tables in high-performance instances.
client.createTable(request);
}
Configure a global secondary index when you create a data table
The following sample code provides an example on how to create a data table and configure a global secondary index for the data table at the same time. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. The data table also contains two predefined columns: the defcol1 column of the STRING type and the defcol2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires. The primary key columns of the global secondary index are the defcol1, pk1, and pk2 columns. The attribute column of the global secondary index is the defcol2 column.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Add primary key columns to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Add predefined columns to the data table.
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each column. A value of 1 specifies that only the latest version of data is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Specify the name of the index table.
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
// Add a primary key column to the index table.
indexMeta.addPrimaryKeyColumn("defcol1");
// Add an attribute column to the index table.
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); // Create the index table when you create the data table.
client.createTable(request);
}
Configure a local secondary index when you create a data table
The following sample code provides an example on how to create a data table and configure a local secondary index for the data table at the same time. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. The data table also contains two predefined columns: the defcol1 column of the STRING type and the defcol2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires. The primary key columns of the local secondary index are the defcol1, pk1, and pk2 columns. The attribute column of the local secondary index is the defcol2 column.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Add primary key columns to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Add predefined columns to the data table.
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each column. A value of 1 specifies that only the latest version of data is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Specify the name of the index table.
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
// Set the index type to IT_LOCAL_INDEX.
indexMeta.setIndexType(IT_LOCAL_INDEX);
// Set the index update mode to IUM_SYNC_INDEX. If the indexType parameter is set to IT_LOCAL_INDEX, you must set the indexUpdateMode parameter to IUM_SYNC_INDEX.
indexMeta.setIndexUpdateMode(IUM_SYNC_INDEX);
// Add a primary key column to the index table. The first primary key column of the index table must be the same as the first primary key column of the data table.
indexMeta.addPrimaryKeyColumn("pk1");
// Add a primary key column to the index table.
indexMeta.addPrimaryKeyColumn("defcol1");
// Add an attribute column to the index table.
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); // Create the index table when you create the data table.
client.createTable(request);
}
Enable the local transaction feature when you create a data table
The following sample code provides an example on how to enable the local transaction feature when you create a data table. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Add primary key columns to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each column. A value of 1 specifies that only the latest version of data is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
// Enable the local transaction feature. If you enable the auto-increment primary key column feature for the data table, the configurations of the local transaction feature do not take effect.
request.setLocalTxnEnabled(true);
client.createTable(request);
}
References
To read data from and write data to a data table, you can call the related API operations. For more information, see Basic operations on data.
After you enable the local transaction feature for a data table, you can create a local transaction and perform read and write operations on the data in the local transaction. For more information, see Use the local transaction feature.
If you no longer need a data table, you can delete the data table. For more information, see Delete a table.