This topic describes how to use Tablestore SDK for Java to create a data table by using parameters and sample code. When you create a data table, you must specify the schema information and configuration information about the data table. You can specify the reserved read and write throughput for a data table in a high-performance instance.
Usage notes
After you create a data table, a few seconds are required to load the data table. 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.
Tablestore provides the data encryption feature. You can configure encryption-related parameters when you create a data table to increase the security of your data and meet compliance requirements. For more information, see Create an encrypted table.
In scenarios that require an auto-increment primary key column, such as item IDs on e-commerce websites and post IDs in forums, you can specify an auto-increment primary key column 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 an instance.
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
API operation
/**
* Description: Create a data table based on the specified table schema.
* After you create a data table, several seconds are required to load the partitions in the data table. You can perform operations on the data table only after the partitions are loaded.
* Response: CreateTableResponse.
*/
CreateTableResponse createTable(CreateTableRequest createTableRequest) throws TableStoreException, ClientException
Parameters
The following table describes the parameters that are included in createTableRequest
.
Parameter | Description |
tableMeta (required) | The schema information about the data table. You can configure the following parameters to specify the schema information:
|
tableOptions (required) | The configuration information about the data table. You can configure the following parameters to specify the configuration information:
|
indexMeta (optional) | The list of indexes. You can configure the following parameters for each index:
|
reservedThroughtput (optional) | The reserved read and write throughput. Unit: capacity unit (CU). Default value: 0. Important You can set the reserved read or write throughput to a value other than 0 only for a data table in a high-performance instance. |
streamSpecification (optional) | The Stream configuration information about the data table. You can configure the following parameters to specify the Stream configuration information:
|
sseSpecification (optional) | The encryption configurations of the data table. Important After you create a data table, you cannot modify the encryption configurations of the data table. |
enableLocalTxn (optional) | Specifies whether to enable the local transaction feature. Default value: false. A value of false specifies that the local transaction feature is disabled. Important
|
Examples
Create a data table
The following sample code provides an example on how to create a data table:
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. The table contains the pk1 primary key column of the String type and the pk2 primary key column of the Integer type. The pk1 primary key column is the partition key. The pk2 primary key column is an auto-increment primary key column.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER, PrimaryKeyOption.AUTO_INCREMENT));
// Specify the TTL of data in the data table. A value of -1 specifies that data in the data table never expires.
int timeToLive = -1;
// Specify the maximum number of versions that can be retained for data in each attribute column of the data table. In this example, only the latest version of data can be retained for each attribute column.
int maxVersions = 1;
// Specify the maximum difference between the current system time and the timestamp of the written data. In this example, the maximum difference is set to 86,400 seconds (one day).
long maxTimeDeviation = 86400L;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions, maxTimeDeviation);
// Specify the reserved read or write throughput. You can set the reserved read or write throughput only to 0 for a data table in a capacity instance. You can set the reserved read or write throughput to a value other than 0 for a data table in a high-performance instance.
ReservedThroughput reservedThroughput = new ReservedThroughput(new CapacityUnit(0, 0));
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, reservedThroughput);
client.createTable(request);
}
Create a data table and a secondary index
Create a data table and a global secondary index
The following sample code provides an example on how to create a data table and a global secondary index for the data table at the same time:
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. The data table contains the pk1 primary key column of the String type and the pk2 primary key column of the Integer type.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Add predefined columns to the data table. The predefined columns of the data table are the defcol1 column of the String type and the defcol2 column of the Integer type.
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
// Specify the TTL of data in the data table. A value of -1 specifies that data in the data table never expires.
int timeToLive = -1;
// Specify the maximum number of versions that can be retained for data in each attribute column of the data table. In this example, only the latest version of data can be retained for each attribute column.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
// Specify the configurations of the global secondary index.
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Specify the name of the index.
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
// Add primary key columns to the index. The primary key columns of the index are defcol1, pk1, and pk2.
indexMeta.addPrimaryKeyColumn("defcol1");
indexMeta.addPrimaryKeyColumn("pk1");
indexMeta.addPrimaryKeyColumn("pk2");
// Add predefined columns to the index. The predefined column of the index is defcol2.
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
// Create the data table and global secondary index at the same time.
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas);
client.createTable(request);
}
Create a data table and a local secondary index
The following sample code provides an example on how to create a data table and a local secondary index for the data table at the same time:
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. The data table contains the pk1 primary key column of the String type and the pk2 primary key column of the Integer type.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Add predefined columns to the data table. The predefined columns of the data table are the defcol1 column of the String type and the defcol2 column of the Integer type.
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
// Specify the TTL of data in the data table. A value of -1 specifies that data in the data table never expires.
int timeToLive = -1;
// Specify the maximum number of versions that can be retained for data in each attribute column of the data table. In this example, only the latest version of data can be retained for each attribute column.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
// Specify the configurations of the local secondary index.
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Specify the name of the index.
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 you set the indexType parameter to IT_LOCAL_INDEX, you must set the indexUpdateMode parameter to IUM_SYNC_INDEX.
indexMeta.setIndexUpdateMode(IUM_SYNC_INDEX);
// Add primary key columns to the index. The primary key columns of the index are pk1, defcol1, and pk2.
indexMeta.addPrimaryKeyColumn("pk1");
indexMeta.addPrimaryKeyColumn("defcol1");
indexMeta.addPrimaryKeyColumn("pk2");
// Add predefined columns to the index. The predefined column of the index is defcol2.
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
// Create the data table and local secondary index at the same time.
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas);
client.createTable(request);
}
Create a data table with the local transaction feature enabled
The following sample code provides an example on how to create a data table with the local transaction feature enabled:
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. The data table contains the pk1 primary key column of the String type and the pk2 primary key column of the Integer type.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Specify the TTL of data in the data table. A value of -1 specifies that data in the data table never expires.
int timeToLive = -1;
// Specify the maximum number of versions that can be retained for data in each attribute column of the data table. In this example, only the latest version of data can be retained for each attribute column.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
// Enable the local transaction feature. If you specify an auto-increment primary key column for a data table, the configurations of the local transaction feature do not take effect for the table.
request.setLocalTxnEnabled(true);
client.createTable(request);
}
References
For information about the API operation that you can call to create a data table, see CreateTable.
For information about data versions and TTL, secondary indexes, and reserved read and write throughput, see Data versions and TTL, Overview, and What is reserved read and write throughput?
After you create a data table, you can perform the following operations:
Operations on the table. For more information, see Operations on tables.
Operations on data. For more information, see Basic operations on data.