You can access Tablestore in the Tablestore console, in the Tablestore CLI, or by using Tablestore SDKs. You can get started with Tablestore in the Tablestore CLI. This topic describes how to manage the Wide Column model in the Tablestore CLI.

Step 1: Download and start the Tablestore CLI

  1. Download the Tablestore CLI package based on your operating system.
    Operating system Download link
    Windows Windows 10
    Linux
    macOS macOS
  2. Decompress the Tablestore CLI package that you downloaded. Go to the root directory of the Tablestore CLI and select a method that is used to start the Tablestore CLI based on your operating system.
    • For Windows, double-click the ts.exe file.
    • For Linux or macOS, run the ./ts command.
      Note If you do not have execute permissions to run the command in Linux or macOS, run the chmod 755 ts command to obtain permissions and start the Tablestore CLI.
    The following code shows a sample Tablestore startup interface:
    # Welcome to use Command Line Tool for Aliyun Tablestore. Current Version is '2021-11-11'.
    #    _______      _      _              _
    #   |__   __|    | |    | |            | |
    #      | |  __ _ | |__  | |  ___   ___ | |_   ___  _ __    ___
    #      | | / _' || '_ \ | | / _ \ / __|| __| / _ \ | '__| / _ \
    #      | || (_| || |_) || ||  __/ \__ \| |_ | (_) || |   |  __/
    #      |_| \__,_||_.__/ |_| \___| |___/ \__| \___/ |_|    \___|
    #
    # Please visit our product website: https://www.aliyun.com/product/ots
    # You can also join our DingTalk Chat Group (ID: 11789671 or 23307953) to discuss and ask Tablestore related questions.
    #
    tablestore>

Step 2: Activate Tablestore

If Tablestore is activated, skip this operation. You must activate Tablestore only once. You are not charged when you activate Tablestore.

  1. Run the config command to configure the AccessKey pair information.
    Notice An AccessKey pair, which consists of the AccessKey ID and the AccessKey secret of an Alibaba Cloud account, is required. For information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.
    config --id NTSVLeBHzgX2iZfcaXXPJ**** --key 7NR2DiotscDbauohSq9kSHX8BDp99bjs7eNpCR7o****
  2. Run the enable_service command to activate Tablestore.

Step 3: Create an instance

Run the create_instance command to create a high-performance instance.

The following sample code shows how to create a high-performance instance named myinstance in the China (Hangzhou) region:
create_instance -d "First instance created by CLI." -n myinstance -r cn-hangzhou

Step 4: Configure access information

Run the config command to configure access information.

The following sample code shows how to configure access information for the instance named myinstance:

config --endpoint https://myinstance.cn-hangzhou.ots.aliyuncs.com --instance myinstance --id NTSVLeBHzgX2iZfcaXXPJ**** --key 7NR2DiotscDbauohSq9kSHX8BDp99bjs7eNpCR7o****

Step 5: Create and use a data table

After a data table is created, you can perform operations on the table or data in the table.

  1. Run the following command to create a data table named mytable:
    create -t mytable --pk '[{"c":"uid", "t":"string"}, {"c":"pid", "t":"integer"}]'
  2. Run the use -t mytable command to use the data table named mytable.

Step 6: Perform data operations

You can insert, update, read, and delete a row of data based on your business requirements.

  • Insert a row of data
    Insert a row of data into a data table. The value of the first primary key column in the row is "86". The value of the second primary key column in the row is 6771. The row contains the following two attribute columns: name and country. The name and country columns are of the string type.
    put --pk '["86", 6771]' --attr '[{"c":"name", "v":"redchen"}, {"c":"country", "v":"china"}]'
  • Update a row of data
    Update a row of data in which the value of the first primary key column is "86" and the value of the second primary key column is 6771. Data is inserted regardless of whether the row exists. If the row exists, the inserted data overwrites the existing data.
    update --pk '["86", 6771]' --attr '[{"c":"name", "v":"redchen"}, {"c":"country", "v":"china"}]'  --condition ignore
  • Read a row of data
    Read a row of data in which the value of the first primary key column is "86" and the value of the second primary key column is 6771.
    get --pk '["86",6771]'
  • Scan data
    The following sample code shows how to scan data in a data table to return up to 10 rows of data:
    scan --limit 10
  • Delete a row of data
    Delete a row of data in which the value of the first primary key column is "86" and the value of the second primary key column is 6771.
    delete --pk '["86", 6771]'

Step 7: Use SQL to query data

The SQL query feature is compatible with MySQL query syntax and supports table creation by using Data Definition Language (DDL) statements. For existing data tables, you can execute the CREATE TABLE statement to create mapping tables for the existing data tables. Then, you can use SQL statements to access the data in the existing data tables.

To use SQL statements to quickly query data in a table, perform the following steps:

  1. Run the sql command to enter the SQL mode.
  2. Run the following command to create a mapping table for the data table named mytable:
    Note For more information about the field type mappings between data tables and mapping tables, see Data type mappings.
    CREATE TABLE `mytable` (
         `uid` VARCHAR(1024),
         `pid` BIGINT(20),
         `b` DOUBLE,
         `c` BOOL,
         `d` MEDIUMTEXT,
         PRIMARY KEY(`uid`,`pid`)
    );
  3. Run the following command to query all data in the table named mytable:
    SELECT * FROM mytable;

If you want to exit the SQL mode, run the exit; command.