AnalyticDB for MySQL lets you query Tablestore data directly through external tables or import it into internal tables for repeated analytics. Use direct query for ad hoc access. Use import for performance-sensitive or repeated analytics — querying an internal table is faster than querying an external table.
Prerequisites
Before you begin, ensure that you have:
An AnalyticDB for MySQL Enterprise, Basic or Data Lakehouse Edition cluster.
If your Tablestore instance is bound to a virtual private cloud (VPC), the AnalyticDB for MySQL cluster must be in the same VPC.
Background
A Tablestore instance manages tables and data in Tablestore and is equivalent to a database. Each Tablestore instance maps to an AnalyticDB for MySQL schema or database.
Network access: If no VPC is bound to the Tablestore instance, AnalyticDB for MySQL can access the data directly. If a VPC is bound, the AnalyticDB for MySQL cluster must be in the same VPC.
Sample data
The examples in this topic use a Tablestore table named person with the following data. Skip this section if you already have source data.
id (primary key) | name | age |
1 | james | 10 |
2 | bond | 20 |
3 | jack | 30 |
4 | lucy | 40 |
Open the SQL editor
Log on to the AnalyticDB for MySQL console. In the upper-left corner, select a region. In the left-side navigation pane, click Clusters. Find the cluster and click its cluster ID.
In the left-side navigation pane, choose Job Development > SQL Development.
Query Tablestore data
Step 1: Create an external database
CREATE EXTERNAL DATABASE adb_external_db;Step 2: Create an external table
The external table must match the Tablestore table in field names, count, and order. Field data types must also be compatible. For data type mappings, see Data type mappings.
The following example creates 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"
}';Parameter | Description |
| The storage engine for reading and writing Tablestore data. Always set this to |
| The name of the table in the Tablestore instance. To find the table name, go to the Tablestore console and open the All Instances page. |
| The VPC URL of the Tablestore instance. To find the URL, go to the Tablestore console and open the All Instances page. |
Step 3: Query the data
After the external table is created, AnalyticDB for MySQL automatically maps data from the Tablestore table. Run the following SELECT statement to query the data:
SELECT * FROM adb_external_db.person;Expected output:
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | james | 10 |
| 2 | bond | 20 |
| 3 | jack | 30 |
| 4 | lucy | 40 |
+------+-------+------+
4 rows in set (0.35 sec)Import Tablestore data to AnalyticDB for MySQL
Importing data from Tablestore into an AnalyticDB for MySQL internal table enables faster repeated queries. Three write methods are available — choose based on your deduplication and performance requirements.
Step 1: Create a database
CREATE DATABASE adb_demo;Step 2: Create a destination table
The destination table must have the same field count and order as the external table. Field data types must be compatible.
CREATE TABLE IF NOT EXISTS adb_demo.adb_import_test (
id int,
name string,
age int
)
DISTRIBUTED BY HASH(id);Step 3: Write data
Choose a write method based on how you want to handle duplicate primary keys:
Method | Duplicate handling | Syntax |
INSERT INTO | Skip duplicates (equivalent to INSERT IGNORE INTO) |
|
INSERT OVERWRITE | Overwrite duplicates |
|
SUBMIT JOB | Asynchronous overwrite |
|
Method 1: INSERT INTO (skip duplicates)
If a row with a duplicate primary key already exists, the row is skipped.
INSERT INTO adb_demo.adb_import_test
SELECT * FROM adb_external_db.person;Verify the result:
SELECT * FROM adb_demo.adb_import_test;Expected output:
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | james | 10 |
| 2 | bond | 20 |
| 3 | jack | 30 |
| 4 | lucy | 40 |
+------+-------+------+Method 2: INSERT OVERWRITE (replace duplicates)
If a row with a duplicate primary key already exists, the existing row is overwritten.
INSERT OVERWRITE adb_demo.adb_import_test
SELECT * FROM adb_external_db.person;Method 3: SUBMIT JOB (asynchronous import)
In most cases, use SUBMIT JOB to run the import asynchronously. You can add the /*+ direct_batch_load=true*/ hint to accelerate the job.
SUBMIT JOB
INSERT OVERWRITE adb_demo.adb_import_test
SELECT * FROM adb_external_db.person;The statement returns a job ID:
+---------------------------------------+
| job_id |
+---------------------------------------+
| 2020112122202917203100908203303****** |To monitor the asynchronous job, see Asynchronously submit an import job.
Data type mappings
The following table describes data type mappings between Tablestore and AnalyticDB for MySQL.
Tablestore | AnalyticDB for MySQL |
INTEGER (8 bytes) | BIGINT (8 bytes) |
STRING | VARCHAR |
BINARY | BINARY |
DOUBLE | DOUBLE |
BOOLEAN | BOOLEAN |
What's next
To learn how the INSERT INTO statement handles duplicates, see INSERT INTO.
To learn about asynchronous import options and the INSERT OVERWRITE SELECT syntax, see INSERT OVERWRITE SELECT.