AnalyticDB for MySQL supports creating external tables for OSS, ApsaraDB RDS for MySQL, ApsaraDB for MongoDB, Tablestore, and MaxCompute.
Prerequisites
An AnalyticDB for MySQL Enterprise Edition, Basic Edition, or Data Lakehouse Edition cluster is created.
The minor version of the cluster is 3.1.8.0 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.
You must create an external database. For more information, see CREATE EXTERNAL DATABASE.
Usage notes
Only OSS external tables support cross-account creation.
OSS external tables
The OSS bucket must be in the same region as the AnalyticDB for MySQL cluster.
To create Hudi, Iceberg, or Paimon external tables, your cluster must meet the following minor version requirements:
Hudi external tables: cluster minor version 3.1.9.2 or later.
Iceberg external tables: cluster minor version 3.2.3.0 or later.
Paimon external tables: cluster minor version 3.2.6.1 or later.
To view and update the minor version, go to the Configuration Information section on the Cluster Information page in the AnalyticDB for MySQL console.
After you create a partitioned OSS external table, run the
MSCK REPAIR TABLEstatement to synchronize partitions. Otherwise, you cannot query data from the external table.To create an OSS external table across accounts, you must add the required parameters when you create the external database. For more information, see CREATE EXTERNAL DATABASE.
Syntax
CREATE EXTERNAL TABLE [IF NOT EXISTS] table_name
(column_name column_type[, ...])
[PARTITIONED BY (column_name column_type[, ...])]
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS {TEXTFILE|ORC|PARQUET|JSON|RCFILE|HUDI|ICEBERG|PAIMON}
LOCATION 'OSS_LOCATION'
[TBLPROPERTIES (
'type' = 'cow|mor',
'auto.create.location' = 'true|false',
'metadata_location' = 'METADATA_LOCATION'
)]Parameters
Parameter | Required | Description |
| Yes | Defines the name and schema of the table. For information about the naming conventions for tables and columns, see Naming conventions. Important When you create a Paimon external table, the table name, column names, and column data types must be the same as those in the Paimon file. If the table schema (field names or types) is inconsistent with the Paimon schema, the Paimon schema prevails. |
| No | Specifies the partition key column. This parameter is required when you create a partitioned external table. To create a multi-level partitioned table, specify multiple partition key columns. |
| Yes | Specifies the column delimiter. You can specify any character, but it must match the delimiter in the source file. This topic uses a comma (,) as an example. Important This parameter is supported only when you specify |
| Yes | Specifies the file format. If the file is in .txt or .csv format, set this parameter to Files in Important Only clusters with minor version 3.1.8.0 or later support |
| Yes | Specifies the path of the OSS file or directory. When you specify an OSS directory path, follow these rules. Otherwise, query failures or unexpected results may occur.
When you create a partitioned external table, set LOCATION to the parent directory of the partitions. For example, if the OSS file path is Important
|
| No | The Hudi external table type. Valid values:
Important This parameter is required only when |
| No | Specifies whether to automatically create the OSS file or directory path. Valid values:
Important This parameter takes effect only when you create a partitioned external table. |
| No | Specifies the path of the metadata file for the Iceberg external table. Important
|
Examples
Example 1: Create a non-partitioned external table
Specify the file storage format as TEXTFILE.
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.osstest1 (id INT, name STRING, age INT, city STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION 'oss://testBucketName/osstest/p1=hangzhou/p2=2023-06-13/data.csv';Specify the file storage format as HUDI.
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.osstest2 (id INT, name STRING, age INT, city STRING) STORED AS HUDI LOCATION 'oss://testBucketName/osstest/test' TBLPROPERTIES ('type' = 'cow');ImportantWhen you create a Hudi external table, five fixed columns are automatically created:
_hoodie_commit_time,_hoodie_commit_seqno,_hoodie_record_key,_hoodie_partition_path, and_hoodie_file_name.Specify the file storage format as PARQUET.
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.osstest3 ( A STRUCT < var1:STRING, var2:INT >) STORED AS PARQUET LOCATION 'oss://testBucketName/osstest/Parquet';Specify the file storage format as ICEBERG.
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.osstest4 ( user_id BIGINT) STORED AS ICEBERG LOCATION 'oss://testBucketName/osstest/no_partition_table/' TBLPROPERTIES (metadata_location='oss://testBucketName/osstest/no_partition_table/metadata/00000-a32d6136-8490-4ad2-ada3-fe2f7204199f.metadata.json');Specify the file storage format as P*****.
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.osstest5 ( a INT, b BIGINT, aCa STRING, d VARCHAR(1)) STORED AS PAIMON LOCATION 'oss://testBucketName/osstest/default.db/t1/';
Example 2: Create a partitioned external table
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.osstest6
(id int,
name string,
age int,
city string)
PARTITIONED BY (p2 string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 'oss://testBucketName/osstest/p1=hangzhou/';Example 3: Create a multi-level partitioned external table
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.osstest7
(id int,
name string,
age int,
city string)
PARTITIONED BY (p1 string,p2 string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 'oss://testBucketName/osstest/';ApsaraDB RDS for MySQL external tables
Before you create an ApsaraDB RDS for MySQL external table, go to the AnalyticDB for MySQL console and enable the elastic network interface (ENI) switch on the Cluster Information page. Enabling or disabling the ENI network interrupts database connections for about 2 minutes. During this time, you cannot read data from or write data to the database. Carefully evaluate the impact before you enable or disable the ENI network.
The ApsaraDB RDS for MySQL instance and the AnalyticDB for MySQL cluster must be in the same virtual private cloud (VPC).
Syntax
CREATE EXTERNAL TABLE [IF NOT EXISTS] table_name
(column_name column_type[, …])
ENGINE='MYSQL'
TABLE_PROPERTIES='{
"url":"mysql_vpc_address",
"tablename":"mysql_table_name",
"username":"mysql_user_name",
"password":"mysql_user_password"
[,"charset":"{gbk|utf8|utf8mb4}"]
}';Parameters
Parameter | Required | Description |
| Yes | Defines the table name and structure. For more information about the naming conventions for tables and columns, see Naming conventions. |
| Yes | The storage engine of the external table. To read data from or write data to an ApsaraDB RDS for MySQL database, set this parameter to MYSQL. |
| Yes | The properties of the external table. |
| Yes | The VPC endpoint, port number, and database name of the ApsaraDB RDS for MySQL instance. For more information about how to obtain the VPC endpoint, see View or modify the internal and public endpoints and port numbers. |
| Yes | The name of the table in the ApsaraDB RDS for MySQL database. |
| Yes | The username for the ApsaraDB RDS for MySQL database. |
| Yes | The password for the ApsaraDB RDS for MySQL database account. |
| No | The character set of the MySQL external table. Valid values:
|
Example
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.mysqltest (
id int,
name varchar(1023),
age int
) ENGINE = 'MYSQL'
TABLE_PROPERTIES = '{
"url":"jdbc:mysql://rm-bp1gx6********.mysql.rds.aliyuncs.com:3306/test_adb",
"tablename":"person",
"username":"testUserName",
"password":"testUserPassword",
"charset":"utf8"
}';ApsaraDB for MongoDB external tables
Before creating an ApsaraDB for MongoDB external table, enable the elastic network interface (ENI) switch on the Cluster Information page of the AnalyticDB for MySQL console. Enabling or disabling the ENI network interrupts database connections for about 2 minutes. During this time, you cannot read from or write to the database. Carefully assess the impact before you enable or disable the ENI network.
The ApsaraDB for MongoDB external table instance must be in the same virtual private cloud (VPC) as the AnalyticDB for MySQL cluster.
Syntax
CREATE EXTERNAL TABLE [IF NOT EXISTS] table_name
(column_name column_type[, …])
ENGINE='MONGODB'
TABLE_PROPERTIES = '{
"mapped_name":"table",
"location":"location",
"username":"user",
"password":"password",
}';Parameters
Parameter | Required | Description |
| Yes | Defines the table name and structure. For more information about the naming conventions for tables and columns, see Naming conventions. |
| Yes | The storage engine for the external table. To read data from and write data to ApsaraDB for MongoDB, set this parameter to MONGODB. |
| Yes | The properties of the external table. |
mapped_name | Yes | The name of the ApsaraDB for MongoDB collection. |
location | Yes | |
username | Yes | ApsaraDB for MongoDB database account. Note ApsaraDB for MongoDB needs to verify the account and password for the target database. Use the database account specified in the ApsaraDB for MongoDB VPC endpoint. If you encounter any problems, contact technical support. |
password | Yes | The password for the ApsaraDB for MongoDB database username. |
Example
CREATE EXTERNAL TABLE adb_external_demo.mongodbtest (
id int,
name string,
age int
) ENGINE = 'MONGODB' TABLE_PROPERTIES ='{
"mapped_name":"person",
"location":"mongodb://testuser:****@dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717,dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717/test_mongodb",
"username":"testuser",
"password":"password",
}';Tablestore external tables
If a Tablestore instance is associated with a virtual private cloud (VPC), the associated VPC must be the same as the VPC where the AnalyticDB for MySQL cluster resides.
Syntax
CREATE EXTERNAL TABLE [IF NOT EXISTS] table_name
(column_name column_type[, …])
ENGINE='OTS'
TABLE_PROPERTIES = '{
"mapped_name":"table_name",
"location":"tablestore_vpc_address"
}';Parameters
Parameter | Required | Description |
| Yes | Defines the table name and structure. For the naming conventions for table and column names, see Naming conventions. |
| Yes | The storage engine for the external table. Set this parameter to |
| Yes | The name of the table in the Tablestore instance. Log on to the Tablestore console and find the table name on the Instance Management page. |
| Yes | The VPC endpoint of the Tablestore instance. Log on to the Tablestore console and find the VPC endpoint of the instance on the Instance Management page. |
Example
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.otstest (
id int,
name string,
age int
) ENGINE = 'OTS'
TABLE_PROPERTIES = '{
"mapped_name":"person",
"location":"https://w0****la.cn-hangzhou.vpc.tablestore.aliyuncs.com"
}';MaxCompute external tables
The MaxCompute project and the AnalyticDB for MySQL cluster must be in the same region.
To create MaxCompute external tables in bulk, see IMPORT FOREIGN SCHEMA.
Syntax
CREATE EXTERNAL TABLE [IF NOT EXISTS] table_name
(column_name column_type[, …])
ENGINE='ODPS'
TABLE_PROPERTIES='{
"endpoint":"endpoint",
"accessid":"accesskey_id",
"accesskey":"accesskey_secret",
["partition_column":"partition_column"],
"project_name":"project_name",
"table_name":"table_name"
}'; Parameters
Parameter | 是否必填 | Description |
| Yes | Defines the table name and schema. The schema must include the partition key column.
Note MaxCompute complex data types are supported in minor version 3.2.1.0 and later. For more information about complex data types, see Complex data types. To view and update the minor version of an AnalyticDB for MySQL cluster, log on to the AnalyticDB for MySQL console and go to the Configuration Information section of the Cluster Information page. |
| Yes | The storage engine for the external table. Set this to |
| Yes | The endpoint of MaxCompute. Note You can access MaxCompute only through a virtual private cloud (VPC) endpoint. For more information about how to view the MaxCompute endpoint, see Endpoints. |
| Yes | The AccessKey ID of your Alibaba Cloud account or a RAM user that has permissions to access MaxCompute. For more information about how to obtain an AccessKey ID and AccessKey Secret, see Account and permissions. |
| Yes | The AccessKey Secret of your Alibaba Cloud account or a RAM user that has permissions to access MaxCompute. For more information about how to obtain an AccessKey ID and AccessKey Secret, see Account and permissions. |
| No | The partition key column. This parameter is required if the MaxCompute table is a partitioned table. |
| Yes | The name of the MaxCompute project. |
| Yes | The name of the MaxCompute table. |
Example
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.mctest (
id int,
name varchar(1023),
age int,
dt string
) ENGINE='ODPS'
TABLE_PROPERTIES='{
"accessid":"LTAI****************",
"endpoint":"http://service.cn-hangzhou.maxcompute.aliyun.com/api",
"accesskey":"yourAccessKeySecret",
"partition_column":"dt",
"project_name":"test_adb",
"table_name":"person"
}';Related documents
OSS external table: Import OSS data to the data lakehouse edition.
ApsaraDB RDS for MySQL external table: Import ApsaraDB RDS for MySQL data.
ApsaraDB for MongoDB external table: Import ApsaraDB for MongoDB data.
Tablestore external table: Query and import Tablestore data.
MaxCompute external table: Import MaxCompute data.