This topic describes how to use the Tablestore SDK for Java to create a Tablestore data table and configure its parameters.
Method
public CreateTableResponse createTable(CreateTableRequest createTableRequest) throws TableStoreException, ClientExceptionSample code
Basic usage
This example creates a data table named test_table. The table has one primary key column of the String type.
After you create a data table, you must wait for the table to load before you can perform data operations. This process typically takes a few seconds. If you do not wait, the operations fail.
You cannot change the encryption method after you create a data table. To create an encrypted table, see Set data table encryption.
package org.example.ots;
import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.core.auth.V4Credentials;
import com.alicloud.openservices.tablestore.model.*;
public class CreateTable {
public static void main(String[] args) {
// Obtain access credentials from environment variables. You must configure TABLESTORE_ACCESS_KEY_ID and TABLESTORE_ACCESS_KEY_SECRET.
final String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
final String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
// TODO: Modify the following configurations based on your instance information.
final String region = "yourRegion"; // The ID of the region where your instance is located. Example: "cn-hangzhou".
final String instanceName = "yourInstanceName"; // The name of your instance.
final String endpoint = "yourEndpoint"; // The endpoint of your instance.
SyncClient client = null;
try {
// Create credentials.
DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);
// Create a client instance.
client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));
// Define the table schema.
TableMeta tableMeta = new TableMeta("test_table"); // TODO: Modify the table name as needed.
// You must add at least one primary key column to create a table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("id", PrimaryKeyType.STRING)); // TODO: Modify the table primary key as needed.
// Configure the table.
TableOptions tableOptions = new TableOptions();
// You must specify the max versions when you create a data table.
tableOptions.setMaxVersions(1);
// You must specify the time to live (TTL) when you create a data table. A value of -1 means the data never expires.
tableOptions.setTimeToLive(-1);
// Create and send the request.
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
client.createTable(request);
System.out.println("The data table is created.");
} catch (Exception e) {
System.err.println("Failed to create the data table. Details:");
e.printStackTrace();
} finally {
// Shut down the client.
if (client != null) {
client.shutdown();
}
}
}
}
Add primary keys
You can add primary keys using the addPrimaryKeyColumn or addPrimaryKeyColumns method. The following example uses the addPrimaryKeyColumn method.
tableMeta.addPrimaryKeyColumn("name", PrimaryKeyType.STRING);Add predefined columns
You can add predefined columns using the addDefinedColumn or addDefinedColumns method. The following example uses the addDefinedColumn method.
tableMeta.addDefinedColumn("age", DefinedColumnType.INTEGER);Set the max version offset
You can set the max version offset using the setMaxTimeDeviation method.
tableOptions.setMaxTimeDeviation(86400);Set whether to allow updates
You can use the setAllowUpdate method to specify whether to allow data updates in the table.
tableOptions.setAllowUpdate(false);Add a secondary index
You can add a secondary index by specifying the indexMetas parameter when you create the request.
// Create a list of secondary indexes.
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Create a secondary index.
IndexMeta indexMeta = new IndexMeta("test_table_idx");
// Set the primary key of the index.
indexMeta.addPrimaryKeyColumn("id");
// To add more primary key columns, first define the corresponding primary keys or predefined columns in the data table.
// indexMeta.addPrimaryKeyColumn("additional_column");
// Set the index type.
indexMeta.setIndexType(IndexType.IT_LOCAL_INDEX);
// Set the index update mode.
indexMeta.setIndexUpdateMode(IndexUpdateMode.IUM_SYNC_INDEX);
// Add the secondary index.
indexMetas.add(indexMeta);
// Create the request.
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas);Set Stream information
You can set stream information using the setStreamSpecification method of the request.
StreamSpecification streamSpecification = new StreamSpecification(true, 168);
request.setStreamSpecification(streamSpecification);Enable local transactions
You can enable local transactions using the setLocalTxnEnabled method of the request.
request.setLocalTxnEnabled(true);Set the reserved read/write throughput
You can set the reserved read/write throughput for the table using the setReservedThroughput method of the request.
// Set the reserved read throughput to 10000 and the reserved write throughput to 5000.
ReservedThroughput reservedThroughput = new ReservedThroughput(10000, 5000);
request.setReservedThroughput(reservedThroughput);Set data table encryption
You can set the data table encryption method using the setSseSpecification method of the request.
KMS encryption
SSESpecification sseSpecification = new SSESpecification(true, SSEKeyType.SSE_KMS_SERVICE); request.setSseSpecification(sseSpecification);BYOK encryption
NoteBefore you run the code, you must obtain the customer master key (CMK) ID and the RAM role ARN. For more information, see BYOK encryption.
String keyId = "key-hzz6*****************"; String roleArn = "acs:ram::1705************:role/tabletorebyok"; SSESpecification sseSpecification = new SSESpecification(true, SSEKeyType.SSE_BYOK, keyId, roleArn); request.setSseSpecification(sseSpecification);