Data tables are used to store and query data. After you create data tables, you can manage the data tables based on your business requirements. For example, you can query the names of all tables in an instance, query the configurations of a data table, and update the configurations of a data table.
Usage notes
You cannot use the auto-increment primary key column feature and the local transaction feature at the same time. If you specify an auto-increment primary key column when you create a data table, the configurations of the local transaction feature do not take effect.
After you create a data table, you cannot immediately perform read and write operations on the data table. In most cases, you can perform read and write operations on a data table about 1 minute after the data table is created.
You can create up to 64 data tables in an instance.
Features
Tablestore provides the CreateTable, ListTable, UpdateTable, DescribeTable, and DeleteTable operations.
Before you store data in Tablestore, you must call the CreateTable operation to create a data table.
When you create a data table, you must configure the schema and features of the data table based on your business requirements. The schema of a data table includes the table name, primary key information, and predefined columns. In addition, you can configure the following features for a data table:
Specify the max versions and time to live (TTL) of data. For more information, see Data versions and TTL.
Specify the reserved read throughput or reserved write throughput for the data table if the data table resides in a high-performance instance. For more information, see Reserved read and write throughput.
Create one or more index tables for the data table. For more information, see Overview.
When you create a data table, you can define some columns that are not primary key columns and their data types. When you use the secondary index feature, you can use the predefined columns as the index columns or attribute columns of an index table. You can also add or remove predefined columns after the data table is created.
Enable the data encryption feature. For more information, see Data encryption.
By default, you can call the UpdateRow operation to write data to a data table. However, if you want to use the TTL feature of a search index, you must prohibit the UpdateRow operation. You can also select No for the Allow Updates parameter after a data table is created.
Enable the local transaction feature. For more information, see Local transactions.
If you do not enable the local transaction feature when you create a data table, you must submit a ticket to apply for the use of the feature.
Important Only Tablestore SDK for Java V5.11.0 and later support the local transaction feature.
Configure an auto-increment primary key column. For more information, see Configure an auto-increment primary key column.
After you create a data table, you can perform the following operations based on your business requirements.
Operation | Description |
Query the names of tables | You can call the ListTable operation to query the names of all tables in the current instance. |
Update the configurations of a table | You can call the UpdateTable operation to modify the table configurations, the reserved read and write throughput, and whether the UpdateRow operation is allowed. |
Query the information about a table | You can call the DescribeTable operation to query the information about a table, such as the schema and the reserved read and write throughput of the table. |
Delete a data table | You can call the DeleteTable operation to delete a data table from the current instance.
Important If you delete a data table, the table becomes unavailable and cannot be recovered. Proceed with caution. Before you delete a data table, make sure that the secondary indexes, search indexes, and data delivery tasks that are created for the table are deleted.
|
Manage predefined columns | Predefined columns are used as the index columns or attribute columns of an index table. When you use the secondary index feature but no predefined columns are added to the data table or the existing predefined columns do not meet your business requirements, you can add predefined columns to the data table. You can remove the predefined columns that you no longer require. |
Methods
Use the Tablestore console
To create a data table in the Tablestore console, perform the following steps:
Log on to the Tablestore console.
In the top navigation bar, select a resource group and a region.
On the Overview page, find the instance that you want to manage. Click the name of the instance or click Manage Instance in the Actions column.
On the Instance Details tab, click Create Table.
In the Create Table dialog box, configure the Table Name and Primary Key parameters. Configure other parameters based on your business requirements.
If you want to use the auto-increment primary key column feature, select Auto Increment for a primary key column that is not the partition key.
If you want to use the TTL feature for a search index, you must select No for Allow Updates.
If you want to encrypt data that is stored in the data table, turn on Encryption and select CMK of KMS or BYOK-based Key for Encryption Type based on your business requirements. For more information, see Data encryption.
If you want to create a local secondary index or a global secondary index, turn on Create Secondary Index, add predefined columns, and then create a secondary index.
If you want to configure other parameters, such as the TTL, turn on Advanced Settings and configure the Time to Live, Max Versions, Max Version Offset, Reserved Read Throughput, and Reserved Write Throughput parameters.
Click Create.
After you create a data table, you can perform operations to manage the data table. The following table describes the operations.
Operation | Description |
Query the names of tables | On the Tables tab, you can view the names of all tables in the current instance. |
Update the configurations of a table | On the Tables tab, click the name of the table that you want to manage. In the Description section of the Basic Information tab, click Modify Attributes. In the Modify Attributes dialog box, modify the parameters such as Time to Live, Max Versions, Max Version Offset, and Allow Updates based on your business requirements. Click OK.
|
Query the information about a table | On the Tables tab, click the name of the table that you want to manage. On the Basic Information tab, view the information about the table. In the Description section, you can view the values of the parameters such as Table Name, Time to Live, Max Versions, Max Version Offset, and Allow Updates. In the Advanced Features section, you can view the information about search indexes, primary key columns, and predefined columns.
|
Manage predefined columns | On the Tables tab, click the name of the table that you want to manage. In the Advanced Features section of the Basic Information tab, click Add Pre-Defined Column. In the Add Pre-Defined Column dialog box, click Add and specify the name and data type of a predefined column. Click OK.
After you add a predefined column, click the icon in the Remove Pre-Defined Column column of the predefined column in the Advanced Features section of the Basic Information tab. In the Remove Pre-Defined Column message, click OK. |
Delete a data table | On the Tables tab, find the data table that you want to delete, click the icon in the Actions column, and then click Delete. In the Delete Table message, confirm the information about the table and click OK.
|
Use the Tablestore CLI
Run the create
command in the Tablestore CLI to create a data table. For more information, see Create a table.
The following sample code provides an example on how to create a data table named mytable. In this example, the data table contains the following primary key columns: the uid primary key column of the STRING type and the pid primary key column of the INTEGER type. Data in the data table never expires.
create -t mytable --pk '[{"c":"uid", "t":"string"}, {"c":"pid", "t":"integer"}]'
To perform operations on a table, you must run the use
command to select the table that you want to manage. For more information, see Use a table.
The following sample code provides an example on how to select the mytable table:
use --wc -t mytable
After you create a data table, you can perform operations to manage the data table based on your business requirements. The following table describes the operations.
Operation | Description |
Query the names of tables | Run the list command to query the names of all tables in the current instance. For more information, see List the names of tables. The following sample code provides an example on how to query the names of all tables in the current instance: list -w
|
Update the configurations of a table | Run the alter command to update the configurations of a table. For more information, see Update a table. The following sample code provides an example on how to update the configurations of the mytable table. In this example, the TTL of data is set to 86,400 seconds (one day), the max versions is set to 1, and the number of reserved read and write capacity units (CUs) is set to 0. alter -t mytable --ttl 86400 --version 1 --read_cu 0 --write_cu 0
|
Query the information about a table | Run the desc command to query the information about a table. For more information, see Query information about a table. The following sample code provides an example on how to query the information about the current table: desc
|
Delete a data table | Run the drop command to delete a table that you no longer require. For more information, see Delete a table. The following sample code provides an example on how to delete the mytable table: drop -t mytable -y
|
Use Tablestore SDKs
You can use Tablestore SDK for Java, Tablestore SDK for Go, Tablestore SDK for Python, Tablestore SDK for Node.js, Tablestore SDK for .NET, or Tablestore SDK for PHP to create and manage data tables. In this example, Tablestore SDK for Java is used.
Important The features supported by SDKs may vary. The actual features prevail.
Create a data table based on your business requirements.
Create a data table without indexes
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);
}
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 at the same time:
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Specify the primary key columns of the data table. In this example, the primary key columns are pk1 of the String type and pk2 of the Integer type.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Specify the predefined columns of the data table. In this example, the predefined columns are defcol1 of the String type and defcol2 of the Integer type.
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
// Configure the timeToLive parameter. A value of -1 specifies that data in the data table never expires.
int timeToLive = -1;
// Configure the maxVersions parameter. A value of 1 specifies that only the latest version of data is retained for each attribute column.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
// Create a global secondary index.
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Specify the name of the global secondary index.
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
// Specify the primary key columns of the index. In this example, the primary key columns are defcol1, pk1, and pk2.
indexMeta.addPrimaryKeyColumn("defcol1");
indexMeta.addPrimaryKeyColumn("pk1");
indexMeta.addPrimaryKeyColumn("pk2");
// Specify the predefined columns of the index. In this example, the predefined column is defcol2.
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
// Create the data table and 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 at the same time:
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Specify the primary key columns of the data table. In this example, the primary key columns are pk1 of the String type and pk2 of the Integer type.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Specify the predefined columns of the data table. In this example, the predefined columns are defcol1 of the String type and defcol2 of the Integer type.
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
// Configure the timeToLive parameter. A value of -1 specifies that data in the data table never expires.
int timeToLive = -1;
// Configure the maxVersions parameter. A value of 1 specifies that only the latest version of data is retained for each attribute column.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
// Create a local secondary index.
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Specify the name of the local secondary index.
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
// Set the IndexType parameter to IT_LOCAL_INDEX to create a local secondary index.
indexMeta.setIndexType(IT_LOCAL_INDEX);
// Set the IndexUpdateMode parameter to IUM_SYNC_INDEX. When you create a local secondary index, you must set the index update mode to synchronous update.
indexMeta.setIndexUpdateMode(IUM_SYNC_INDEX);
// Specify the primary key columns of the index. In this example, the primary key columns are defcol1, pk1, and pk2.
indexMeta.addPrimaryKeyColumn("pk1");
indexMeta.addPrimaryKeyColumn("defcol1");
indexMeta.addPrimaryKeyColumn("pk2");
// Specify the predefined columns of the index. In this example, the predefined column is defcol2.
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
// Create the data table and index at the same time.
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas);
client.createTable(request);
}
Create a table with local transaction enabled
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);
}
Create a table with KMS key-based encryption enabled
The following sample code provides an example on how to enable KMS key-based encryption for a data table when you create the data table. In this example, the data table contains the pk primary key column of the STRING type, only the latest version of data is retained for each attribute column, and the data in the data table never expires.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// If you want to configure data encryption, configure encryption-related parameters in the createTable request. The following encryption modes are supported: KMS key-based encryption and BYOK-based encryption. In this example, KMS key-based encryption is enabled.
SSESpecification sseKms = new SSESpecification(true, SSEKeyType.SSE_KMS_SERVICE);
// 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 attribute column. A value of 1 specifies that only the latest version of data is retained for each attribute 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);
// Specify the reserved read throughput and reserved write throughput. The values must be set to 0 for data tables in capacity instances and can be set to non-zero values for data tables in high-performance instances.
request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0)));
request.setSseSpecification(sseKms);
client.createTable(request);
}
Create a table with an auto-increment primary key column
The following sample code provides an example on how to create a table that contains an auto-increment primary key column. The table contains the pk1 primary key column whose data type is STRING and the pk2 primary key column whose data type is INTEGER. The pk1 column is a partition key column, and the pk2 column is an auto-increment primary key column.
private static void createTable(SyncClient client) {
// Specify the name of the table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Create the first primary key column. The first primary key column is a partition key column.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
// Create the second primary key column whose data type is INTEGER and set the attribute of the column to AUTO_INCREMENT.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER, PrimaryKeyOption.AUTO_INCREMENT));
// Specify the time to live (TTL) of data. A value of -1 specifies that the data never expires. Unit: seconds.
int timeToLive = -1;
// Specify the maximum number of versions that can be retained for each column. A value of 1 specifies that only the latest version is retained for each column.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
client.createTable(request);
}
After you create a data table, you can perform operations to manage the data table. The following table describes the operations.
Query the names of tables
The following sample code provides an example on how to query the names of all tables in an instance:
private static void listTable(SyncClient client) {
ListTableResponse response = client.listTable();
System.out.println("List of tables:");
for (String tableName : response.getTableNames()) {
System.out.println(tableName);
}
}
Update the configurations of a table
Update the configurations of a data table
The following sample code provides an example on how to update the configurations of a data table:
private static void updateTable(SyncClient client) {
// Update the value of the timeToLive parameter. A value of -1 specifies that data in the data table never expires.
int timeToLive = -1;
// Update the value of the maxVersions parameter. A value of 5 specifies that up to five versions of data is retained for each attribute column.
int maxVersions = 5;
// Update the value of the maxTimeDeviation parameter. A value of 86400 specifies that the maximum difference between the current system time and the data written time is 86400 seconds (one day).
long maxTimeDeviation = 86400L;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions, maxTimeDeviation);
// Allow the UpdateRow operation on the data table.
tableOptions.setAllowUpdate(true);
// Enable the Stream feature and set the validity period of streams to 24 hours.
//StreamSpecification streamSpecification = new StreamSpecification(true, 24);
// Specify the name of the data table.
UpdateTableRequest request = new UpdateTableRequest("<TABLE_NAME>");
request.setTableOptionsForUpdate(tableOptions);
client.updateTable(request);
}
Update the reserved throughput of a data table in a high-performance instance
The following sample code provides an example on how to update the reserved throughput of a data table in a high-performance instance:
private static void updateTable(SyncClient client) {
// Set the new reserved read throughput to 1 and the new reserved write throughput to 1. You can set the reserved read and write throughput only to 0 for a data table in a capacity instance.
ReservedThroughput reservedThroughput = new ReservedThroughput(new CapacityUnit(1, 1));
// Specify the name of the data table.
UpdateTableRequest request = new UpdateTableRequest("<TABLE_NAME>");
request.setReservedThroughputForUpdate(reservedThroughput);
client.updateTable(request);
}
Query the information about a table
The following sample code provides an example on how to query the schema information, optional configurations, reserved read throughput, and reserved write throughput of a table:
private static void describeTable(SyncClient client) {
DescribeTableRequest request = new DescribeTableRequest(TABLE_NAME);
DescribeTableResponse response = client.describeTable(request);
TableMeta tableMeta = response.getTableMeta();
System.out.println("The name of the table:" + tableMeta.getTableName());
System.out.println("The primary key of the table:");
for (PrimaryKeySchema primaryKeySchema : tableMeta.getPrimaryKeyList()) {
System.out.println(primaryKeySchema);
}
TableOptions tableOptions = response.getTableOptions();
System.out.println("The TTL of the table:" + tableOptions.getTimeToLive());
System.out.println("MaxVersions of the table:" + tableOptions.getMaxVersions());
System.out.println("The encryption configuration of the table:" + response.getSseDetails());
ReservedThroughputDetails reservedThroughputDetails = response.getReservedThroughputDetails();
System.out.println("The reserved read throughput of the table:"
+ reservedThroughputDetails.getCapacityUnit().getReadCapacityUnit());
System.out.println("The reserved write throughput of the table:"
+ reservedThroughputDetails.getCapacityUnit().getWriteCapacityUnit());
}
Manage predefined columns
The following sample code provides an example on how to add predefined columns to a table. In this example, the following predefined columns are added: definedColumnName01 (type: STRING), definedColumnName02 (type: INTEGER), and definedColumnName03 (type: STRING).
public static void addDefinedColumnRequest(SyncClient client) {
AddDefinedColumnRequest addDefinedColumnRequest = new AddDefinedColumnRequest();
// Specify the name of the table.
addDefinedColumnRequest.setTableName("<TABLE_NAME>");
// Add predefined columns to the table.
addDefinedColumnRequest.addDefinedColumn("definedColumnName01",DefinedColumnType.STRING);
addDefinedColumnRequest.addDefinedColumn("definedColumnName02",DefinedColumnType.INTEGER);
addDefinedColumnRequest.addDefinedColumn("definedColumnName03",DefinedColumnType.STRING);
client.addDefinedColumn(addDefinedColumnRequest);
}
You can remove the predefined columns that you no longer require.
The following sample code provides an example on how to remove the definedColumnName01 and definedColumnName02 predefined columns from a table:
public static void deleteDefinedColumnRequest(SyncClient client){
DeleteDefinedColumnRequest deleteDefinedColumnRequest = new DeleteDefinedColumnRequest();
// Specify the name of the table.
deleteDefinedColumnRequest.setTableName("<TABLE_NAME>");
// Remove predefined columns from the table.
deleteDefinedColumnRequest.addDefinedColumn("definedColumnName01");
deleteDefinedColumnRequest.addDefinedColumn("definedColumnName02");
client.deleteDefinedColumn(deleteDefinedColumnRequest);
}
Delete a data table
The following sample code provides an example on how to delete the exampletable table:
private static void deleteTable(SyncClient client) {
// Specify the name of the data table.
DeleteTableRequest request = new DeleteTableRequest("<TABLE_NAME>");
client.deleteTable(request);
}