AnalyticDB for MySQL supports importing data from MaxCompute by using external tables. This method maximizes the use of cluster resources and achieves high-performance data import. This topic describes how to import MaxCompute data into AnalyticDB for MySQL by using an external table.
Overview
|
AnalyticDB for MySQLEdition |
Access method |
AnalyticDB for MySQLKernel version |
Throughput |
|
Enterprise Edition, Basic Edition, and Data Lakehouse Edition |
Tunnel Record API |
All versions |
Suitable for small-scale data access. Data access and import speeds are low. |
|
Tunnel Arrow API |
3.2.2.3 or later |
Reads data in a columnar format to reduce data access and import time, providing faster data transfer speeds. |
|
|
Data Warehouse Edition |
Tunnel Record API |
All versions |
Uses a public data transfer service resource group shared by all projects in the region, resulting in low throughput. |
Prerequisites
-
The MaxCompute project and the AnalyticDB for MySQL cluster are in the same region. For more information, see Create a cluster.
-
The MaxCompute project's whitelist must include the CIDR block of the VPC for your AnalyticDB for MySQL cluster.
NoteLog on to the AnalyticDB for MySQL console to find the VPC ID on the Cluster Information page. Then, in the VPC console, use the VPC ID to find the CIDR block on the VPC page. For information about how to configure a MaxCompute whitelist, see Manage IP Whitelists.
-
For AnalyticDB for MySQL Enterprise, Basic, and Data Lakehouse Edition clusters:
-
Elastic network interface (ENI) access is enabled.
Important-
Log on to the AnalyticDB for MySQL console. On the page, turn on the ENI network switch in the Network Information section.
-
Enabling or disabling the ENI network interrupts database connections for about two minutes, causing read and write operations to fail. Carefully evaluate this impact before proceeding.
-
-
To access and import MaxCompute data using the Tunnel Arrow API, your cluster must have a kernel version of 3.2.2.3 or later.
NoteTo view and update the minor version, go to the Configuration Information section on the Cluster Information page in the AnalyticDB for MySQL console.
-
Data preparation
In this example, the MaxCompute project is odps_project and the sample table is odps_nopart_import_test. The example is as follows:
CREATE TABLE IF NOT EXISTS odps_nopart_import_test (
id int,
name string,
age int)
partitioned by (dt string);
Add a partition to the odps_nopart_import_test table. The following is an example:
ALTER TABLE odps_nopart_import_test
ADD
PARTITION (dt='202207');
Insert data into the partition:
INSERT INTO odps_project.odps_nopart_import_test
PARTITION (dt='202207')
VALUES (1,'james',10),(2,'bond',20),(3,'jack',30),(4,'lucy',40);
Procedure
Enterprise, Basic, and Data Lakehouse editions
By default, an AnalyticDB for MySQL cluster uses the Tunnel Record API to access and import data from MaxCompute. To use the Tunnel Arrow API, you must first enable the Arrow API feature. After it is enabled, the AnalyticDB for MySQL cluster uses the Tunnel Arrow API for data import.
Tunnel record API
You can import data using either regular import (default) or elastic import. In regular import mode, source data is read on compute nodes and indexes are built on storage nodes, which consumes both compute and storage resources. In elastic import mode, source data is read and indexes are built in a Serverless Spark job, which consumes resources from a job resource group. Elastic import is supported only on clusters with a kernel version of 3.1.10.0 or later that have an existing job resource group. Compared with regular import, elastic import significantly reduces resource consumption, minimizes the impact of the import process on online read/write workloads, and improves resource isolation and data import efficiency. For more information, see Data import methods.
Regular import
-
Go to the SQL editor.
Log on to the AnalyticDB for MySQL console. In the upper-left corner of the console, select a region. In the left-side navigation pane, click Clusters. Find the cluster that you want to manage and click the cluster ID.
-
In the left-side navigation pane, choose .
-
Create an external database. Example:
CREATE EXTERNAL DATABASE adb_external_db; -
Create an external table. This topic uses
test_adbas an example.CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_db.test_adb ( id int, name varchar(1023), age int, dt string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "accessid":"yourAccessKeyID", "endpoint":"http://service.cn-hangzhou.maxcompute.aliyun.com/api", "accesskey":"yourAccessKeySecret", "partition_column":"dt", "project_name":"odps_project", "table_name":"odps_nopart_import_test" }';Note-
For an external table in AnalyticDB for MySQL and a table in MaxCompute, the field names, number of fields, and order of fields must be consistent, and the field types must be compatible.
-
For more information about the parameters for creating an external table, see CREATE EXTERNAL TABLE.
-
-
Query the data.
SELECT * FROM adb_external_db.test_adb;The following result is returned:
+------+-------+------+---------+ | id | name | age | dt | +------+-------+------+---------+ | 1 | james | 10 | 202207 | | 2 | bond | 20 | 202207 | | 3 | jack | 30 | 202207 | | 4 | lucy | 40 | 202207 | +------+-------+------+---------+ -
Perform the following steps to import the MaxCompute data into AnalyticDB for MySQL:
-
Create a database in AnalyticDB for MySQL. Example:
CREATE DATABASE adb_demo; -
In AnalyticDB for MySQL, create a table to store the data imported from MaxCompute. Example:
NoteThe field order and number of fields in the new table must match those in the external table created in Step 3. The field data types must be compatible.
CREATE TABLE IF NOT EXISTS adb_demo.adb_import_test( id int, name string, age int, dt string, PRIMARY KEY(id,dt) ) DISTRIBUTED BY HASH(id) PARTITION BY VALUE('dt'); -
Write data to the table. Examples:
-
Method 1: Import data by executing an
INSERT INTOstatement. If a duplicate primary key exists, the new data is automatically ignored and no update is performed. This operation is equivalent toINSERT IGNORE INTO. For more information, see INSERT INTO. The following is an example:INSERT INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.test_adb;If you need to import data from a specific partition into
adb_demo.adb_import_test, you can execute the following:INSERT INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.test_adb WHERE dt = '202207'; -
Method 2: Run the INSERT OVERWRITE INTO statement to import data. This overwrites the existing data in the table. Example:
INSERT OVERWRITE INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.test_adb; -
Method 3: Asynchronously import data by using INSERT OVERWRITE INTO. You can use
SUBMIT JOBto submit an asynchronous job that is scheduled in the background. To accelerate the write job, add a Hint (/*+ direct_batch_load=true*/) before the job. For more information, see Asynchronous writes. The following is an example:SUBMIT job INSERT OVERWRITE INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.test_adb;The following result is returned:
+---------------------------------------+ | job_id | +---------------------------------------+ | 2020112122202917203100908203303****** | +---------------------------------------+For more information about submitting asynchronous jobs, see Submit an asynchronous import job.
-
-
Elastic import
-
Go to the SQL editor.
Log on to the AnalyticDB for MySQL console. In the upper-left corner of the console, select a region. In the left-side navigation pane, click Clusters. Find the cluster that you want to manage and click the cluster ID.
-
In the left-side navigation pane, choose .
-
Create a database if one does not already exist. Example:
CREATE DATABASE adb_demo; -
Create an external table.
Note-
The name of the AnalyticDB for MySQL external table must be the same as the name of the MaxCompute project. Otherwise, table creation will fail.
-
The field names, number of fields, and order of fields in the AnalyticDB for MySQL external table must match those in the MaxCompute table. The field data types must also be compatible.
-
Elastic import only supports the
CREATE TABLEstatement to create external tables.
CREATE TABLE IF NOT EXISTS test_adb ( id int, name string, age int, dt string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "endpoint":"http://service.cn-hangzhou.maxcompute.aliyun-inc.com/api", "accessid":"yourAccessKeyID", "accesskey":"yourAccessKeySecret", "partition_column":"dt", "project_name":"odps_project", "table_name":"odps_nopart_import_test" }';For more information about the supported parameters and their descriptions, see Parameters.
-
-
Query the data.
SELECT * FROM adb_demo.test_adb;The following result is returned:
+------+-------+------+---------+ | id | name | age | dt | +------+-------+------+---------+ | 1 | james | 10 | 202207 | | 2 | bond | 20 | 202207 | | 3 | jack | 30 | 202207 | | 4 | lucy | 40 | 202207 | +------+-------+------+---------+ -
In AnalyticDB for MySQL, create a table to store the data imported from MaxCompute. Example:
NoteThe internal table must have the same field names, number of fields, field order, and data types as the external table created in Step 3.
CREATE TABLE IF NOT EXISTS adb_import_test ( id int, name string, age int, dt string, PRIMARY KEY(id,dt) ) DISTRIBUTED BY HASH(id) PARTITION BY VALUE('dt') LIFECYCLE 30; -
Import the data.
ImportantElastic import only supports importing data by using the
INSERT OVERWRITE INTOstatement.-
Method 1: Run the INSERT OVERWRITE INTO statement to elastically import data. This overwrites the existing data in the table. Example:
/*+ elastic_load=true, elastic_load_configs=[adb.load.resource.group.name=resource_group]*/ INSERT OVERWRITE INTO adb_demo.adb_import_test SELECT * FROM adb_demo.test_adb;
-
Method 2: Asynchronously perform an elastic import of data by running the INSERT OVERWRITE INTO command. You can typically use
SUBMIT JOBto submit an asynchronous job, which is then scheduled by the backend./*+ elastic_load=true, elastic_load_configs=[adb.load.resource.group.name=resource_group]*/ SUBMIT JOB INSERT OVERWRITE INTO adb_demo.adb_import_test SELECT * FROM adb_demo.test_adb;ImportantPriority queues are not supported for asynchronous elastic import jobs.
The following result is returned:
+---------------------------------------+ | job_id | +---------------------------------------+ | 2023081517192220291720310090151****** | +---------------------------------------+
After you use
SUBMIT JOBto submit an asynchronous job, the returned result indicates only that the job was submitted successfully. You can use the job_id to terminate the asynchronous job or query its status to determine whether the job was executed successfully. For more information, see Submit an import job asynchronously.Hint parameters:
-
elastic_load: specifies whether to use elastic import. Valid values: true and false. Default value: false.
-
elastic_load_configs: the configuration parameters of the elastic import feature. You must enclose the parameters within brackets ([ ]) and separate multiple parameters with vertical bars (|). The following table describes the parameters.
Parameter
Required
Description
adb.load.resource.group.name
Yes
The name of the job resource group that runs the elastic import job.
adb.load.job.max.acu
No
The maximum amount of resources for an elastic import job. Unit: AnalyticDB compute units (ACUs). Minimum value: 5 ACUs. Default value: number of shards plus 1.
Execute the following statement to query the number of shards in the cluster:
SELECT count(1) FROM information_schema.kepler_meta_shards;spark.driver.resourceSpec
No
The resource type of the Spark driver. Default value: small. For information about the valid values, see the Type column in the "Spark application configuration parameters" table of the Conf configuration parameters topic.
spark.executor.resourceSpec
No
The resource type of the Spark executor. Default value: large. For information about the valid values, see the Type column in the "Spark application configuration parameters" table of the Conf configuration parameters topic.
spark.adb.executorDiskSize
No
The disk capacity of the Spark executor. Valid values: (0,100]. Unit: GiB. Default value: 10 GiB. For more information, see the "Specify driver and executor resources" section of the Conf configuration parameters topic.
-
-
(Optional) Check whether the submitted import job is an elastic import job.
SELECT job_name, (job_type = 3) AS is_elastic_load FROM INFORMATION_SCHEMA.kepler_meta_async_jobs WHERE job_name = "2023081818010602101701907303151******";The following result is returned:
+---------------------------------------+------------------+ | job_name | is_elastic_load | +---------------------------------------+------------------+ | 2023081517195203101701907203151****** | 1 | +---------------------------------------+------------------+If
is_elastic_loadreturns 1, the submitted import job is an elastic import job. If it returns 0, the submitted import job is a regular import job.
Tunnel arrow API
Step 1: Enable Arrow API
Methods
You can use the SET statement or a hint to enable the Arrow API at the cluster or query level:
-
At the cluster level:
SET ADB_CONFIG <config_name>= <value>; -
At the query level:
/*<config_name>= <value>*/ SELECT * FROM table;
Arrow API parameters
|
Parameter (config_name) |
Description |
|
ODPS_TUNNEL_ARROW_ENABLED |
Specifies whether to enable the Arrow API. Valid values:
|
|
ODPS_TUNNEL_SPLIT_BY_SIZE_ENABLED |
Specifies whether to enable dynamic splits. Valid values:
|
Step 2: Access and import data
-
Go to the SQL editor.
Log on to the AnalyticDB for MySQL console. In the upper-left corner of the console, select a region. In the left-side navigation pane, click Clusters. Find the cluster that you want to manage and click the cluster ID.
-
In the left-side navigation pane, choose .
-
Create an external database. Example:
CREATE EXTERNAL DATABASE adb_external_db; -
Create an external table. This topic uses
test_adbas an example.CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_db.test_adb ( id int, name varchar(1023), age int, dt string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "accessid":"yourAccessKeyID", "endpoint":"http://service.cn-hangzhou.maxcompute.aliyun.com/api", "accesskey":"yourAccessKeySecret", "partition_column":"dt", "project_name":"odps_project", "table_name":"odps_nopart_import_test" }';Note-
The external table in AnalyticDB for MySQL and the table in MaxCompute must have consistent field names, number of fields, and field order, and compatible field types.
-
For more information about the parameters for creating an external table, see CREATE EXTERNAL TABLE.
-
-
Query the data.
SELECT * FROM adb_external_db.test_adb;The following result is returned:
+------+-------+------+---------+ | id | name | age | dt | +------+-------+------+---------+ | 1 | james | 10 | 202207 | | 2 | bond | 20 | 202207 | | 3 | jack | 30 | 202207 | | 4 | lucy | 40 | 202207 | +------+-------+------+---------+ -
Perform the following steps to import the MaxCompute data into AnalyticDB for MySQL:
-
Create a database in AnalyticDB for MySQL. Example:
CREATE DATABASE adb_demo; -
In AnalyticDB for MySQL, create a table to store the data imported from MaxCompute. Example:
NoteThe field order and number of fields in the new table must match those in the external table created in Step 3. The field data types must be compatible.
CREATE TABLE IF NOT EXISTS adb_demo.adb_import_test( id int, name string, age int, dt string, PRIMARY KEY(id,dt) ) DISTRIBUTED BY HASH(id) PARTITION BY VALUE('dt'); -
Write data to the table. Examples:
-
Method 1: Use an INSERT INTO statement to import data. When a duplicate primary key is encountered, the new record is ignored and the existing record is not updated. This operation is equivalent to
INSERT IGNORE INTO. For more information, see INSERT INTO. The following is an example:INSERT INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.test_adb;If you need to import data from a specific partition into
adb_demo.adb_import_test, you can run the following:INSERT INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.test_adb WHERE dt = '202207'; -
Method 2: Run the INSERT OVERWRITE INTO statement to import data. This overwrites the existing data in the table. Example:
INSERT OVERWRITE INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.test_adb; -
Method 3: Asynchronously import data by running an
INSERT OVERWRITE INTOstatement. You can useSUBMIT JOBto submit an asynchronous job, which is scheduled by the backend. To accelerate the write job, add a hint (/*+ direct_batch_load=true*/). For more information, see Asynchronous write. The following is an example:SUBMIT job INSERT OVERWRITE INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.test_adb;The following result is returned:
+---------------------------------------+ | job_id | +---------------------------------------+ | 2020112122202917203100908203303****** | +---------------------------------------+For more information about submitting asynchronous jobs, see Submit an asynchronous import job.
-
-
Data warehouse edition
-
Connect to the AnalyticDB for MySQL cluster. For more information, see Connect to an AnalyticDB for MySQL cluster.
-
Create a destination database.
CREATE database test_adb; -
Create a MaxCompute external table. This topic uses
odps_nopart_import_test_external_tableas an example.CREATE TABLE IF NOT EXISTS odps_nopart_import_test_external_table ( id int, name string, age int, dt string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "endpoint":"http://service.cn.maxcompute.aliyun-inc.com/api", "accessid":"yourAccessKeyID", "accesskey":"yourAccessKeySecret", "partition_column":"dt", "project_name":"odps_project1", "table_name":"odps_nopart_import_test" }';Parameter
Description
ENGINE=’ODPS’The storage engine of the external table. To read data from and write data to MaxCompute, set this parameter to ODPS.
endpointThe endpoint of MaxCompute.
NoteAnalyticDB for MySQL can access MaxCompute only through a MaxCompute VPC endpoint.
To find the VPC endpoint for each region, see VPC Endpoint.
accessidThe AccessKey ID of an Alibaba Cloud account or a RAM user that has permissions to access MaxCompute.
For information about how to obtain an AccessKey ID and an AccessKey Secret, see Obtain an AccessKey pair.
accesskeyThe AccessKey Secret of an Alibaba Cloud account or a RAM user that has permissions to access MaxCompute.
For information about how to obtain an AccessKey ID and an AccessKey Secret, see Obtain an AccessKey pair.
partition_columnThe example in this topic is for creating a partitioned table, so you need to configure
partition_column. If the MaxCompute table is a non-partitioned table, you must also create a non-partitioned table in AnalyticDB for MySQL. In this case, you do not need to configurepartition_column.project_nameThe name of the workspace in MaxCompute.
table_nameThe name of the source table in MaxCompute.
-
In the
test_adbdatabase, create theadb_nopart_import_testtable to store data imported from MaxCompute.CREATE TABLE IF NOT EXISTS adb_nopart_import_test ( id int, name string, age int, dt string, PRIMARY KEY(id,dt) ) DISTRIBUTED BY HASH(id) PARTITION BY VALUE('dt') LIFECYCLE 30; -
Import the data.
-
Method 1: Run
INSERT INTOto import data. If a primary key is duplicated, the new data is automatically ignored and no update is performed. This is equivalent toINSERT IGNORE INTO. For more information, see INSERT INTO. The following is an example:INSERT INTO adb_nopart_import_test SELECT * FROM odps_nopart_import_test_external_table;Query the data written to the table:
SELECT * FROM adb_nopart_import_test;The following result is returned:
+------+-------+------+---------+ | id | name | age | dt | +------+-------+------+---------+ | 1 | james | 10 | 202207 | | 2 | bond | 20 | 202207 | | 3 | jack | 30 | 202207 | | 4 | lucy | 40 | 202207 | +------+-------+------+---------+If you need to import data from a specific partition into
adb_nopart_import_test, run the following command:INSERT INTO adb_nopart_import_test SELECT * FROM odps_nopart_import_test_external_table WHERE dt = '202207'; -
Method 2: Run the
INSERT OVERWRITEcommand to import data. This command overwrites the existing data in the table. The following is an example:INSERT OVERWRITE adb_nopart_import_test SELECT * FROM odps_nopart_import_test_external_table; -
Method 3: Asynchronously import data by using
INSERT OVERWRITE. Typically, you can useSUBMIT JOBto submit an asynchronous job. The job is then scheduled by the backend. You can add a hint before the write job to accelerate it. For more information, see Asynchronous writes. The following is an example:SUBMIT JOB INSERT OVERWRITE adb_nopart_import_test SELECT * FROM odps_nopart_import_test_external_table;The following result is returned:
+---------------------------------------+ | job_id | +---------------------------------------+ | 2020112122202917203100908203303****** |For more information about submitting asynchronous jobs, see Submit an asynchronous import job.
-