Create a table
Create a Wide Column model table with the Tablestore SDK for Java, and configure its schema, data versions, indexes, Stream, and encryption in the same request.
Prerequisites
Install the Tablestore SDK for Java and initialize the client.
Description
Call createTable to create a Wide Column model table. The request must contain at least one primary key column and specify the time to live (TTL) and maximum number of versions.
public CreateTableResponse createTable(CreateTableRequest createTableRequest) throws TableStoreException, ClientExceptionThe following example creates a table named example_table. The table has one STRING primary key column, data never expires, and only one data version is retained.
TableMeta tableMeta = new TableMeta("example_table");
tableMeta.addPrimaryKeyColumn(
new PrimaryKeySchema("id", PrimaryKeyType.STRING));
TableOptions tableOptions = new TableOptions();
tableOptions.setTimeToLive(-1);
tableOptions.setMaxVersions(1);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
client.createTable(request);After you create a table, wait until the table is loaded before you perform data operations.
Parameters
CreateTableRequest contains the following parameters.
Name | Type | Description |
tableMeta (required) |
| The table schema. |
tableOptions (required) |
| The data version and update settings of the table. |
reservedThroughput (optional) |
| The reserved read/write throughput, in capacity units (CUs). The default read and write CUs are both |
indexMeta (optional) |
| The secondary indexes to create with the table. |
streamSpecification (optional) |
| The Stream configuration. |
enableLocalTxn (optional) |
| Specifies whether to enable local transactions. Default value: |
sseSpecification (optional) |
| The data encryption configuration. You can enable data encryption only when you create a table. After the table is created, you cannot disable encryption or change its configuration. |
Table schema
tableMeta is a TableMeta object that contains the following parameters.
Name | Type | Description |
tableName (required) |
| The name of the table. |
primaryKey (required) |
| The primary key column configurations. You can configure one to four primary key columns. The first primary key column is the partition key, and data is sorted in ascending order by primary key. |
definedColumns (optional) |
| The predefined column configurations. Predefined columns can be used to create secondary indexes and search indexes. |
Primary key columns
Each element in tableMeta.primaryKey[] is a PrimaryKeySchema object that contains the following parameters.
Name | Type | Description |
name (required) |
| The name of the primary key column. |
type (required) |
| The type of the primary key column. Valid values: |
option (optional) |
| The primary key column option. To use an auto-increment primary key column, set a non-partition |
Predefined columns
Each element in tableMeta.definedColumns[] is a DefinedColumnSchema object that contains the following parameters.
Name | Type | Description |
name (required) |
| The name of the predefined column. |
type (required) |
| The type of the predefined column. Valid values: |
Table configurations
tableOptions is a TableOptions object that contains the following parameters.
Name | Type | Description |
timeToLive (required) |
| The time to live (TTL) of data, in seconds. Set this parameter to |
maxVersions (required) |
| The maximum number of versions to retain for each attribute column. To use a search index or secondary index, set this parameter to |
maxTimeDeviation (optional) |
| The maximum version offset, in seconds. Default value: |
allowUpdate (optional) |
| Specifies whether data can be updated by calling |
updateFullRow (optional) |
| Specifies whether to enable whole-row updates. Default value: |
Reserved read/write throughput
reservedThroughput is a ReservedThroughput object that contains the following parameter.
Name | Type | Description |
capacityUnit (required) |
| The reserved read/write throughput configuration. |
Capacity units
reservedThroughput.capacityUnit is a CapacityUnit object that contains the following parameters.
Name | Type | Description |
readCapacityUnit (required) |
| The reserved read throughput, in CUs. |
writeCapacityUnit (required) |
| The reserved write throughput, in CUs. |
Secondary indexes
Each element in indexMeta[] is an IndexMeta object that contains the following parameters.
Name | Type | Description |
indexName (required) |
| The name of the index. |
primaryKey (required) |
| The primary key columns of the index. They can consist of primary key columns and predefined columns from the table. For a local secondary index, the first primary key column must match the first primary key column of the table. |
definedColumns (optional) |
| The predefined columns included in the index. They must be predefined columns of the table. |
indexType (optional) |
| The index type. Valid values: |
indexUpdateMode (optional) |
| The index update mode. A global secondary index must use |
Stream configuration
streamSpecification is a StreamSpecification object that contains the following parameters.
Name | Type | Description |
enableStream (required) |
| Specifies whether to enable Stream. Default value: |
expirationTime (optional) |
| The retention period of incremental logs, in hours. Maximum value: |
Local transaction configuration
enableLocalTxn is a Boolean value. Take note of the following limits when you set this parameter.
Local transactions are supported only in Tablestore SDK for Java 5.11.0 and later.
Local transactions and auto-increment primary key columns are mutually exclusive. If an auto-increment primary key column is configured, local transactions do not take effect even when enabled.
To enable local transactions on an existing table, submit a ticket or join DingTalk technical support group 36165029092.
Server-side encryption configuration
sseSpecification is an SSESpecification object that contains the following parameters.
Name | Type | Description |
enable (required) |
| Specifies whether to enable data encryption. Default value: |
keyType (optional) |
| The encryption type. Valid values: |
keyId (optional) |
| The ID of the customer master key. This parameter is required for BYOK encryption. |
roleArn (optional) |
| The Alibaba Cloud Resource Name (ARN) of the Resource Access Management (RAM) role. This parameter is required for BYOK encryption. |
Examples
Configure the table schema and data versions
The following example creates a table with two primary key columns and one predefined column. The example also configures the max version offset and disables updates.
TableMeta tableMeta = new TableMeta("example_table");
tableMeta.addPrimaryKeyColumn(
new PrimaryKeySchema("id", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(
new PrimaryKeySchema("device_id", PrimaryKeyType.INTEGER));
tableMeta.addDefinedColumn("status", DefinedColumnType.STRING);
TableOptions tableOptions = new TableOptions();
tableOptions.setTimeToLive(-1);
tableOptions.setMaxVersions(1);
tableOptions.setMaxTimeDeviation(86400L);
tableOptions.setAllowUpdate(false);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
client.createTable(request);Create a secondary index together with the table
The following example creates a global secondary index named example_table_index together with the table. The index primary key column category must first be defined as a predefined column of the table.
TableMeta tableMeta = new TableMeta("example_table");
tableMeta.addPrimaryKeyColumn(
new PrimaryKeySchema("id", PrimaryKeyType.STRING));
tableMeta.addDefinedColumn("category", DefinedColumnType.STRING);
TableOptions tableOptions = new TableOptions();
tableOptions.setTimeToLive(-1);
tableOptions.setMaxVersions(1);
IndexMeta indexMeta = new IndexMeta("example_table_index");
indexMeta.addPrimaryKeyColumn("category");
indexMeta.setIndexType(IndexType.IT_GLOBAL_INDEX);
indexMeta.setIndexUpdateMode(IndexUpdateMode.IUM_ASYNC_INDEX);
List<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
indexMetas.add(indexMeta);
CreateTableRequest request =
new CreateTableRequest(tableMeta, tableOptions, indexMetas);
client.createTable(request);Enable Stream
The following example enables Stream when the table is created and sets the expiration time of incremental logs to 24 hours.
TableMeta tableMeta = new TableMeta("example_table");
tableMeta.addPrimaryKeyColumn(
new PrimaryKeySchema("id", PrimaryKeyType.STRING));
TableOptions tableOptions = new TableOptions();
tableOptions.setTimeToLive(-1);
tableOptions.setMaxVersions(1);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
request.setStreamSpecification(new StreamSpecification(true, 24));
client.createTable(request);Enable local transactions
The following example enables local transactions when the table is created by calling setLocalTxnEnabled.
TableMeta tableMeta = new TableMeta("example_table");
tableMeta.addPrimaryKeyColumn(
new PrimaryKeySchema("id", PrimaryKeyType.STRING));
TableOptions tableOptions = new TableOptions();
tableOptions.setTimeToLive(-1);
tableOptions.setMaxVersions(1);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
request.setLocalTxnEnabled(true);
client.createTable(request);Encrypt a table
The following examples encrypt a table by using a KMS service key or BYOK.
You can configure data encryption only when you create the table. After the table is created, you cannot disable encryption or change the encryption configuration.
KMS service key encryption
TableMeta tableMeta = new TableMeta("example_table");
tableMeta.addPrimaryKeyColumn(
new PrimaryKeySchema("id", PrimaryKeyType.STRING));
TableOptions tableOptions = new TableOptions();
tableOptions.setTimeToLive(-1);
tableOptions.setMaxVersions(1);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
request.setSseSpecification(
new SSESpecification(true, SSEKeyType.SSE_KMS_SERVICE));
client.createTable(request);BYOK encryption
To use BYOK encryption, first obtain the customer master key (CMK) ID and the RAM role ARN. For more information, see BYOK encryption.
TableMeta tableMeta = new TableMeta("example_table");
tableMeta.addPrimaryKeyColumn(
new PrimaryKeySchema("id", PrimaryKeyType.STRING));
TableOptions tableOptions = new TableOptions();
tableOptions.setTimeToLive(-1);
tableOptions.setMaxVersions(1);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
String keyId = "key-xxxx****";
String roleArn = "acs:ram::1234****:role/example-role";
request.setSseSpecification(
new SSESpecification(true, SSEKeyType.SSE_BYOK, keyId, roleArn));
client.createTable(request);