This topic describes how to use external tables to import data from Tablestore to AnalyticDB for MySQL Data Lakehouse Edition (V3.0).

Prerequisites

  • A Tablestore instance is created. For more information, see Use Tablestore.
  • An AnalyticDB for MySQL Data Lakehouse Edition (V3.0) cluster is created. For more information, see Create a Data Lakehouse Edition (V3.0) cluster.
    Note If a virtual private cloud (VPC) is bound to the created Tablestore instance, make sure that the AnalyticDB for MySQL cluster is created in the same VPC.

Background information

Tablestore is a NoSQL database service developed by Alibaba Cloud. It uses a variety of data models and supports search index queries. You can call Tablestore API operations in AnalyticDB for MySQL to query data, such as point query and range query. In addition, Tablestore can intelligently select a primary table, secondary index table, and search index table. This significantly improves query performance and reduces user costs.

A Tablestore instance is an entity used to manage tables and data in Tablestore. Each instance is equivalent to a database. A Tablestore instance corresponds to an AnalyticDB for MySQL schema or database. If no VPC is bound to the Tablestore instance, you can directly access the Tablestore data from AnalyticDB for MySQL. If a VPC is bound to the Tablestore instance, make sure that the AnalyticDB for MySQL cluster is created in the same VPC.

Description of sample data

In this example, a table named person is created in Tablestore, and four entries are inserted into the table. If you have created source data, skip this step.
id (primary key)nameage
1james10
2bond20
3jack30
4lucy40

Procedure

  1. Go to the SQL editor.
    1. Log on to the AnalyticDB for MySQL console.
    2. In the upper-left corner of the page, select the region where the cluster resides.
    3. In the left-side navigation pane, click Clusters.
    4. On the Data Lakehouse Edition (V3.0) tab, find the cluster and click the Cluster ID.
    5. In the left-side navigation pane, choose Job Development > SQL Development.
  2. Create an external database.
    Create a database named adb_external_db.
    create external database adb_external_db;
  3. Create an external table.
    Note The AnalyticDB for MySQL external table must have the same names, quantity, and order of fields as the Tablestore table. The data types of fields must be compatible between the two tables. For more information about data type mappings, see the "Data type mappings" section of this topic.
    Create a table named person in the adb_external_db database.
    CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_db.person (
        id int,
        name string,
        age int
    ) ENGINE = 'OTS'  
    TABLE_PROPERTIES = '{
    "mapped_name":"person",
    "location":"https://w0****la.cn-hangzhou.vpc.tablestore.aliyuncs.com"
    }'
    ParameterDescription
    ENGINE='OTS'The storage engine of the external table. To read and write Tablestore data, set the storage engine to OTS.
    mapped_nameThe name of the table in the Tablestore instance. To view the table name, log on to the Tablestore console and go to the Instances page.
    locationThe VPC URL of the Tablestore instance. To view the VPC URL, log on to the Tablestore console and go to the Instances page.
  4. After an external table is created, AnalyticDB for MySQL automatically maps data from the Tablestore table to the AnalyticDB for MySQL table. Execute the following SELECT statement in AnalyticDB for MySQL to query data of the person table in the Tablestore instance:
    select * from adb_external_db.person;
    The following information is returned:
    +------+-------+------+
    | id   | name  | age  |
    +------+-------+------+
    |    1 | james |   10 |
    |    2 | bond  |   20 |
    |    3 | jack  |   30 |
    |    4 | lucy  |   40 |
    +------+-------+------+
    4 rows in set (0.35 sec)
  5. Perform the following steps to import data from Tablestore to AnalyticDB for MySQL:
    1. Create a database in AnalyticDB for MySQL.
      CREATE DATABASE adb_demo; 
    2. Create a table in AnalyticDB for MySQL to store data imported from Tablestore.
      Note The created table must have the same quantity and order of fields as the external table created in Step 3. The data types of fields must be compatible between the two tables.
      CREATE TABLE IF NOT EXISTS adb_demo.adb_import_test(
          id int,
          name string,
          age int
      )
      DISTRIBUTE BY HASH(id);                   
    3. Write data to the table.
      • Method 1: Execute the INSERT INTO statement to import data. If the primary key has duplicate values, data is not repeatedly inserted and the INSERT INTO statement is equivalent to INSERT IGNORE INTO. For more information, see INSERT INTO.
        INSERT INTO adb_demo.adb_import_test
        SELECT * FROM adb_external_db.person;
        Execute the SELECT statement to query the data written to the table.
        select * from adb_demo.adb_import_test;
        The following information is returned:
        +------+-------+------+
        | id   | name  | age  |
        +------+-------+------+
        |    1 | james |   10 |
        |    2 | bond  |   20 |
        |    3 | jack  |   30 |
        |    4 | lucy  |   40 |
        +------+-------+------+
      • Method 2: Execute the INSERT OVERWRITE INTO statement to import data. If the primary key has duplicate values, the original value is overwritten by the new value.
        INSERT OVERWRITE INTO adb_demo.adb_import_test
        SELECT * FROM adb_external_db.person;
      • Method 3: Asynchronously execute the INSERT OVERWRITE INTO statement to import data. Typically, SUBMIT JOB is used to submit an asynchronous task. You can add a hint (/* direct_batch_load=true*/) before the data import statement to accelerate the task. For more information, see Asynchronous writing.
        SUBMIT job 
        INSERT OVERWRITE INTO adb_demo.adb_import_test
        SELECT * FROM adb_external_db.person;
        The following information is returned:
        +---------------------------------------+
        | job_id                                |
        +---------------------------------------+
        | 2020112122202917203100908203303****** |

        For more information about how to submit asynchronous tasks, see Asynchronously submit an import task.

Data type mappings

The following table describes the data type mappings between Tablestore and AnalyticDB for MySQL.
Data type supported by TablestoreData type supported by AnalyticDB for MySQL
INTEGER (eight bytes)BIGINT (eight bytes)
STRINGVARCHAR
BINARYBINARY
DOUBLEDOUBLE
BOOLEANBOOLEAN