AnalyticDB for MySQL uses external tables to import and export data. This topic shows how to use an external table in an AnalyticDB for MySQL Data Warehouse Edition cluster to export data to an ApsaraDB RDS for MySQL instance.
Prerequisites
-
The ApsaraDB RDS for MySQL instance and the AnalyticDB for MySQL cluster must be in the same VPC and have the same VPC ID.
Note-
To find the VPC ID of the ApsaraDB RDS for MySQL instance, log on to the ApsaraDB RDS console, click the instance ID, and on the Database Connection page, view the ID in the Network Type section.
-
Log on to the AnalyticDB for MySQL console. On the Data Warehouse Edition tab, click the cluster ID. On the Cluster Information page, view the VPC ID of the AnalyticDB for MySQL cluster in the Network Information section.
-
-
Create a database in the ApsaraDB RDS for MySQL instance, establish a connection to the instance, and prepare the required test data. For more information, see Create accounts and databases and Connect to an ApsaraDB RDS for MySQL instance.
In this example, the destination database in the ApsaraDB RDS for MySQL instance is named
test_adb. A destination table namedcoursesis required in this database to store the data exported from the AnalyticDB for MySQL cluster. Use the following statement to create the table:CREATE TABLE courses ( id bigint NOT NULL, name varchar(32) NOT NULL, grade varchar(32) NOT NULL, submission_date timestamp NOT NULL, PRIMARY KEY (id) ); -
If the AnalyticDB for MySQL cluster is in elastic mode, you must turn on ENI in the Network Information section of the Cluster Information page.
ImportantWhen you enable or disable ENI, database connections may be interrupted for approximately 2 minutes. During this period, you cannot perform read or write operations. Proceed with caution when you enable or disable ENI.
Procedure
-
Connect to the AnalyticDB for MySQL cluster. For more information, see Connect to an AnalyticDB for MySQL cluster.
-
Create a source database. For more information, see Create a database.
In this example, the source database in the AnalyticDB for MySQL cluster is named
adb_demo. -
Create the source table and insert data.
Run the following statement to create a source table named
coursesin theadb_demosource database. The data from this table will be exported to thecoursestable in thetest_adbdestination database.CREATE TABLE courses ( id bigint AUTO_INCREMENT, name varchar NOT NULL, grade varchar DEFAULT '1st Grade', submission_date timestamp ) DISTRIBUTED BY HASH(id);Run the following statement to insert a row of data into the source table
courses:INSERT INTO courses (name,submission_date) VALUES("Jams",NOW()); -
Create an external table.
Run the following statement to create an external table named
courses_external_tablein theadb_demosource database.CREATE TABLE IF NOT EXISTS courses_external_table( id bigint NOT NULL, name varchar(32) NOT NULL, grade varchar(32) NOT NULL, submission_date timestamp NOT NULL, PRIMARY KEY (id) ) ENGINE='mysql' TABLE_PROPERTIES='{ "url":"jdbc:mysql://mysql-vpc-address:3306/test_adb", "tablename":"courses", "username":"mysql-user-name", "password":"mysql-user-password", "charset":"utf8" }';Parameter
Description
ENGINE='mysql'The storage engine for the external table. This example uses MySQL.
TABLE_PROPERTIESAnalyticDB for MySQL access methods for RDS MySQL data.
urlThe VPC endpoint of the ApsaraDB RDS for MySQL instance and the name of the destination database. In this example, the destination database is
test_adb. For information about how to view the endpoint of the RDS instance, see View and manage instance endpoints and ports.Format:
"jdbc:mysql://mysql-vpc-address:3306/rds-database-name".Example:
jdbc:mysql://rm-bp1hem632****.mysql.rds.aliyuncs.com:3306/test_adb.tablenameThe name of the destination table in the ApsaraDB RDS for MySQL instance. This example uses
courses.usernameThe username for the destination ApsaraDB RDS for MySQL database.
passwordThe password for the specified username.
charsetThe MySQL character set. Valid values:
-
gbk
-
UTF8 (Default)
-
utf8mb4
-
-
Export data from the source AnalyticDB for MySQL cluster to the destination ApsaraDB RDS for MySQL instance.
Run the following statement:
REPLACE INTO courses_external_table SELECT * FROM courses;
Next steps
After the export is complete, you can log on to the test_adb destination database in your ApsaraDB RDS for MySQL instance and run the following statement to verify that the data was exported to the destination courses table:
SELECT * FROM courses LIMIT 100;