Secondary indexes

更新时间:
复制 MD 格式

Create and manage secondary indexes with the Tablestore CLI.

Prerequisites

  • The Tablestore CLI installed and configured with access credentials. For more information, see Tablestore CLI.

  • A data table with predefined columns. The maximum number of versions (max versions) must be 1. For more information, see Manage data tables.

Create a secondary index

Create a global or local secondary index for a data table.

create_index -t <tableName> -n <indexName> -i <indexType> --pk <primaryKeyName> --attr <definedColumn>

Parameter

Required

Description

-t, --table

No

The data table name. Not required if a data table is already selected with use.

-n, --name

Yes

The secondary index name.

-i, --index_type

No

The secondary index type. Valid values:

  • global (default): global secondary index. The indexed columns and primary key columns from the data table are asynchronously replicated to the index table. Synchronization latency is typically in milliseconds.

  • local: local secondary index. Data is synchronously written to the index table. Data is available for queries immediately after it is written to the data table.

-k, --pk

Yes

The indexed columns of the index table, consisting of primary key columns and predefined columns from the data table. Separate multiple columns with commas. For a local secondary index, the first primary key column of the index table must be the same as the first primary key column of the data table.

-a, --attr

Yes

The attribute columns of the index table, which are predefined columns from the data table. Separate multiple columns with commas.

-b, --without_base_data

No

Excludes existing data from the data table when the secondary index is created. By default, existing data is included.

Examples

  • Create a global secondary index named index0 for the data table mytable, including existing data.

    create_index -t mytable -n index0 -i global --pk uid,pid -a name,col0
  • Create a global secondary index named index1 for the data table mytable, excluding existing data.

    create_index -t mytable -n index1 -i global --pk uid,pid -a name,col0 -b
  • Create a local secondary index named index2 for the data table mytable.

    create_index -t mytable -n index2 -i local --pk uid,name -a col0,col1

Use an index table

Select an index table for subsequent query and export operations.

use --wc -t <indexName>

Parameter

Required

Description

--wc

No

Indicates that the specified table is a data table or index table rather than a search index.

-t, --table

Yes

The index table name.

Examples

Use the index table index0.

use -t index0

View index information

Run the desc command to view information about a data table and its secondary indexes.

desc -t <tableName>

Parameter

Required

Description

-t, --table

No

The data table or index table name. Not required if a table is already selected with use.

-f, --print_format

No

The output format. Valid values: json (default) and table.

-o, --output

No

Outputs table information to a local JSON file.

Examples

  • View information about the current table.

    desc
  • Save table information to a local file.

    desc -o /tmp/describe_table_meta.json

Read data

Read a single row of data from an index table by primary key.

Note

If the specified row does not exist, an empty result is returned.

get --pk '<pk_values>'

Parameter

Required

Description

-k, --pk

Yes

The primary key values of the index table, expressed as a JSON array. The number and types of primary key columns must match those of the index table.

-c, --columns

No

The columns to read. Specify primary key columns or attribute columns. Separate multiple columns with commas. If not specified, the entire row is returned.

--max_version

No

The maximum number of versions to read.

--time_range_start

No

Reads data within a version number range. time_range_start and time_range_end specify the start and end timestamps respectively. The range is a left-closed, right-open interval: [start, end).

--time_range_end

No

--time_range_specific

No

Reads data of a specific version number.

-o, --output

No

Outputs query results to a local JSON file.

Examples

Read a row from the index table where the primary key values are "86" and 6771.

get --pk '["86", 6771]'

Scan data

Scan an index table to retrieve all data or a specified number of rows.

scan --limit <n>

Parameter

Required

Description

--limit

No

The maximum number of rows to return. If not specified, all rows in the table are scanned.

Examples

Scan up to 10 rows from the index table.

scan --limit 10

Export data

Export data from an index table to a local JSON file.

scan -o /localpath/filename.json

Parameter

Required

Description

-c, --columns

No

The columns to export. Specify primary key columns or attribute columns. Separate multiple columns with commas. If not specified, the entire row is exported.

--max_version

No

The maximum number of versions to export.

--time_range_start

No

Exports data within a version number range. time_range_start and time_range_end specify the start and end timestamps respectively. The range is a left-closed, right-open interval: [start, end).

--time_range_end

No

--time_range_specific

No

Exports data of a specific version number.

--backward

No

Exports data in descending order of primary keys.

-o, --output

Yes

The local file path to export data to.

-l, --limit

No

The maximum number of rows to export.

-b, --begin

No

The start and end points of the export range. The primary key range is a left-closed, right-open interval: [start, end).

Note
  • If --begin is set to [null,null], the start point is [INF_MIN,INF_MIN], which means each primary key column starts from the minimum value.

  • If --end is set to [null,null], the end point is [INF_MAX,INF_MAX], which means each primary key column ends at the maximum value.

-e, --end

No

Examples

  • Export all data from the index table to a local file.

    scan -o /tmp/mydata.json
  • Export only the uid and name columns.

    scan -o /tmp/mydata.json -c uid,name
  • Export data within a specified primary key range.

    scan -o /tmp/mydata.json -b '["86", 6771]' -e '["86", 6775]'

Delete a secondary index

Delete an index table that is no longer needed.

drop_index -t <tableName> -i <indexName> -y

Parameter

Required

Description

-t, --table

No

The data table name. Not required if a data table is already selected with use.

-i, --index

Yes

The secondary index name.

-y, --yes

Yes

Confirms the deletion without prompting.

Examples

  • Delete the index table index0 from the current data table.

    drop_index -i index0 -y
  • Delete the index table index0 from the data table mytable.

    drop_index -t mytable -i index0 -y