Create a global or local secondary index for an existing data table by using the Tablestore SDK for Java.
Prerequisites
-
Install the Tablestore SDK for Java and initialize a client.
-
The
maxVersionsvalue of the data table is 1, and thetimeToLivevalue is -1 or data cannot be updated by callingUpdateRow. -
If you want to use non-primary key columns as index columns or attribute columns, configure the columns as predefined columns.
Description
A secondary index uses a different primary key schema composed of primary key columns and predefined columns from a data table to provide additional query dimensions. Secondary indexes are classified into global secondary indexes and local secondary indexes. The two index types have different data synchronization modes and primary key requirements. For more information, see Overview.
Call createIndex to create a secondary index for an existing data table. To create one or more secondary indexes together with a data table, configure the index schemas in the createTable request. For more information, see Create a data table.
public CreateIndexResponse createIndex(CreateIndexRequest createIndexRequest)
throws TableStoreException, ClientException
The following sample code creates the global secondary index example_global_index for example_table. The index includes existing data.
IndexMeta indexMeta = new IndexMeta("example_global_index");
indexMeta.addPrimaryKeyColumn("category");
indexMeta.addDefinedColumn("status");
CreateIndexRequest request =
new CreateIndexRequest("example_table", indexMeta, true);
client.createIndex(request);
Parameters
CreateIndexRequest contains the following parameters:
|
Name |
Type |
Description |
|
mainTableName (required) |
|
The name of the data table. |
|
indexMeta (required) |
|
The schema information about the index table. |
|
includeBaseData (required) |
|
Specifies whether the index table includes existing data of the data table. If you set this parameter to |
Index schema
indexMeta is of the IndexMeta type and contains the following parameters:
|
Name |
Type |
Description |
|
indexName (required) |
|
The name of the index table. The name must be different from the name of an existing data table or time series table. |
|
primaryKey (required) |
|
An ordered list of index column names. Specify one to four columns, which can be primary key columns or predefined columns of the data table. Tablestore uses the order in which columns are added and automatically appends unspecified primary key columns of the data table to the end of the index primary key. For a local secondary index, the first index primary key column must be the same as the first primary key column of the data table. |
|
definedColumns (optional) |
|
A list of attribute column names of the index table. Only predefined columns of the data table can be specified. |
|
indexType (optional) |
|
The index type. Valid values are |
|
indexUpdateMode (optional) |
|
The index update mode. A global secondary index must use |
Scenario examples
Create a local secondary index
To create a local secondary index, specify the first primary key column of the data table as the first index primary key column, and set the index type and update mode to IT_LOCAL_INDEX and IUM_SYNC_INDEX, respectively.
IndexMeta indexMeta = new IndexMeta("example_local_index");
indexMeta.addPrimaryKeyColumn("user_id");
indexMeta.addPrimaryKeyColumn("category");
indexMeta.addDefinedColumn("status");
indexMeta.setIndexType(IndexType.IT_LOCAL_INDEX);
indexMeta.setIndexUpdateMode(IndexUpdateMode.IUM_SYNC_INDEX);
CreateIndexRequest request =
new CreateIndexRequest("example_table", indexMeta, true);
client.createIndex(request);