All Products
Search
Document Center

Tablestore:Add or remove predefined columns

Last Updated:Nov 10, 2023

This topic describes how to add predefined columns to or remove predefined columns from a table. This way, when you create a secondary index for the table, you can set the predefined columns of the table as the index columns or attribute columns of the secondary index.

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialization.

  • A table is created.

Add predefined columns

When you create a secondary index for a table, if the table does not contain predefined columns or the existing predefined columns are not suitable for creating a secondary index, you can add predefined columns to the table.

Parameters

Parameter

Description

tableName

The name of the table.

definedColumns

The names and data types of predefined columns. The predefined columns cannot be primary key columns. You can set the predefined columns as the index columns or attribute columns of a secondary index. This parameter includes the following configurations:

  • name: the name of the predefined column.

  • type: the data type of the predefined column.

Example

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

Remove predefined columns

You can remove predefined columns that you no longer use from a table.

Parameters

Parameter

Description

tableName

The name of the table.

name

The name of the predefined column.

Example

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