When you create a data table, you can specify non-primary key columns as predefined columns for the data table and specify the types of the predefined columns. This way, when you create a secondary index, you can use the predefined columns as the index columns or attribute columns of the index table. You can remove a predefined column that you no longer require.
Usage notes
To create a secondary index for a data table, you must specify predefined columns for the data table. You do not need to specify predefined columns for a data table to create a search index for the data table.
You can specify up to 32 predefined columns for a data table. To specify more predefined columns for a data table, submit a ticket.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created. For more information, see Create a data table.
Add predefined columns
If no predefined columns are specified or the existing predefined columns do not meet your business requirements when you create or use a secondary index, you can add predefined columns to the data table.
You can also add predefined columns when you create a data table. For more information, see Create a data table.
Parameters
Parameter | Required | Description |
TableName | Yes | The name of the data table. |
DefinedColumns | Yes | The names and data types of predefined columns. The predefined columns cannot be primary key columns. You can use the predefined columns as the index columns or attribute columns of a secondary index. This parameter includes the following configuration items:
|
Examples
The following sample code provides an example on how to add the defcol1, defcol2, and defcol3 predefined columns to a data table. The data types of the defcol1, defcol2, and defcol3 columns are String, Integer, and String, respectively.
func AddDefinedColumn(client *tablestore.TableStoreClient, tableName string) {
addDefinedColumnRequest := new(tablestore.AddDefinedColumnRequest)
addDefinedColumnRequest.AddDefinedColumn("defcol1",tablestore.DefinedColumn_STRING)
addDefinedColumnRequest.AddDefinedColumn("defcol2",tablestore.DefinedColumn_INTEGER)
addDefinedColumnRequest.AddDefinedColumn("defcol3",tablestore.DefinedColumn_STRING)
addDefinedColumnRequest.TableName = tableName
_, err := client.AddDefinedColumn(addDefinedColumnRequest)
if (err != nil) {
fmt.Println("Failed to Add DefinedColumn with error:", err)
} else {
fmt.Println("Add DefinedColumn finished")
}
}
Remove predefined columns
You can remove predefined columns that you no longer require from a data table. You can remove multiple predefined columns at the same time.
Parameters
Parameter | Required | Description |
TableName | Yes | The name of the data table. |
DefinedColumns | Yes | The name of the predefined column. |
Examples
The following sample code provides an example on how to remove the defcol1 and defcol2 predefined columns from a data table:
func DeleteDefinedColumn(client *tablestore.TableStoreClient, tableName string){
deleteDefinedColumnRequest := new(tablestore.DeleteDefinedColumnRequest)
deleteDefinedColumnRequest.DefinedColumns = []string{"defcol1","defcol2"}
deleteDefinedColumnRequest.TableName = tableName
_, err := client.DeleteDefinedColumn(deleteDefinedColumnRequest)
if (err != nil) {
fmt.Println("Failed to delete DefinedColumn with error:", err)
} else {
fmt.Println("Delete DefinedColumn finished")
}
}
References
For information about the API operations that you can call to add and remove predefined columns, see AddDefinedColumn and DeleteDefinedColumn.
If an error occurs when you call the operations, find the error cause in Error codes based on the error message and handle the error.
After you specify predefined columns for a data table, you can use the predefined columns to create a secondary index for the data table based on your business requirements to query data. For more information, see Create a secondary index.