All Products
Search
Document Center

Tablestore:Operations on a data table

Last Updated:Aug 20, 2024

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:

  1. Log on to the Tablestore console.

  2. In the top navigation bar, select a resource group and a region.

  3. 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.

  4. On the Instance Details tab, click Create Table.

  5. In the Create Table dialog box, configure the Table Name and Primary Key parameters. Configure other parameters based on your business requirements.

    Note

    For more information, see Step 3: Create a data table.

    • 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.

  6. 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

  1. On the Tables tab, click the name of the table that you want to manage.

  2. In the Description section of the Basic Information tab, click Modify Attributes.

  3. 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.

  4. Click OK.

Query the information about a table

  1. On the Tables tab, click the name of the table that you want to manage.

  2. 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

  1. On the Tables tab, click the name of the table that you want to manage.

  2. In the Advanced Features section of the Basic Information tab, click Add Pre-Defined Column.

  3. In the Add Pre-Defined Column dialog box, click Add and specify the name and data type of a predefined column.

  4. Click OK.

After you add a predefined column, click the image.png 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

  1. On the Tables tab, find the data table that you want to delete, click the image.png icon in the Actions column, and then click Delete.

  2. 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 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 table and a global secondary index

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);
}

Create a table and a local secondary index

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);
}

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

The following sample code provides an example on how to update the value of the maxVersions parameter for the example table to 5:

private static void updateTable(SyncClient client) {
    // Specify the time to live (TTL) of data. A value of -1 specifies that the data never expires. Unit: seconds. 
    int timeToLive = -1;
    // Update the max versions of the table to five. 
    int maxVersions = 5; 
    TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
    // Specify the name of the table. 
    UpdateTableRequest request = new UpdateTableRequest("<TABLE_NAME>");
    request.setTableOptionsForUpdate(tableOptions);
    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);
}