AnalyticDB for MySQL lets you create several types of external tables, including OSS external tables, RDS for MySQL external tables, ApsaraDB for MongoDB external tables, Tablestore external tables, and MaxCompute external tables.
Prerequisites
An AnalyticDB for MySQL Enterprise Edition, Basic Edition, or Data Lakehouse Edition cluster is created.
The cluster minor version must be 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 first create an external database. For more information, see CREATE EXTERNAL DATABASE.
Usage notes
You can create only OSS external tables across Alibaba Cloud accounts.
OSS external tables
The OSS bucket must be in the same region as the AnalyticDB for MySQL cluster.
To create a Hudi, Iceberg, or Paimon external table, your cluster must meet the following minor version requirements:
Hudi external table: The cluster minor version must be 3.1.9.2 or later.
Iceberg external table: The cluster minor version must be 3.2.3.0 or later.
Paimon external table: The cluster minor version must be 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 Alibaba Cloud accounts, you must specify 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 table name and schema. For information about the rules for naming tables and columns, see Naming conventions. Important When you create a Paimon external table, the table name, column names, and column data types must match those in the Paimon files. If the table schema (such as column names or types) does not match, the Paimon schema takes precedence. |
| No | Specifies the partition key column. This parameter is required if you want to 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 used in the data file. This topic uses a comma (,) as an example. Important This parameter is supported only when |
| Yes | Specifies the file format. If the file format is .txt or .csv, set this parameter to Files in Important Only clusters with a minor version of 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 to prevent query failures or unexpected results:
When you create a partitioned external table, set LOCATION to the parent directory of the partitions. For example, if the path of an OSS file is Important
|
| No | The type of the Hudi external table. 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
Create an external table stored 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';Create an external table stored 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');Create an external table stored 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';Create an external table stored 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');Create an external table stored as PAIMON.
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/';RDS for MySQL external tables
To create an RDS for MySQL external table, you must first enable ENI on the Cluster Information page in the AnalyticDB for MySQL console. Enabling or disabling ENI causes a database connection interruption for about two minutes, during which read and write operations fail. Carefully evaluate the potential impact before you enable or disable ENI.
The RDS for MySQL instance must be in the same VPC as the AnalyticDB for MySQL cluster.
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 schema. For information about the rules for naming tables and columns, see Naming conventions. |
| Yes | The storage engine of the external table. To read data from and write data to RDS for MySQL, set this value to MYSQL. |
| Yes | The properties of the external table. |
| Yes | The VPC endpoint, port number, and database name of the RDS for MySQL instance. For information about how to obtain the VPC endpoint, see View and change the endpoints and port numbers of an ApsaraDB RDS for MySQL instance. |
| Yes | The name of the table in the RDS for MySQL database. |
| Yes | The account for the RDS for MySQL database. |
| Yes | The password for the 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
To create an ApsaraDB for MongoDB external table, you must first enable ENI on the Cluster Information page in the AnalyticDB for MySQL console. Enabling or disabling ENI causes a database connection interruption for about two minutes, during which read and write operations fail. Carefully evaluate the potential impact before you enable or disable ENI.
The ApsaraDB for MongoDB instance must be in the same 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 schema. For information about the rules for naming tables and columns, see Naming conventions. |
| Yes | The storage engine of the external table. To read data from and write data to ApsaraDB for MongoDB, set this value to MONGODB. |
| Yes | The properties of the external table. |
mapped_name | Yes | The name of the MongoDB collection. |
location | Yes | The vpc endpoint of the ApsaraDB for MongoDB instance. |
username | Yes | The ApsaraDB for MongoDB database account. Note
ApsaraDB for MongoDB verifies the account and password against the destination database. Use the account of the database specified in the vpc endpoint. If you encounter issues, contact technical support. |
password | Yes | The password for the ApsaraDB for MongoDB database account. |
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 the Tablestore instance uses a VPC, it must be the same VPC as your AnalyticDB for MySQL cluster.
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 schema. For information about the rules for naming tables and columns, see Naming conventions. |
| Yes | The storage engine of the external table. To read data from and write data to Tablestore, set this value to OTS. |
| 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 must be in the same region as the AnalyticDB for MySQL cluster.
To create MaxCompute external tables in batches, 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",
["auto_refresh":"true|false"],
["auto_refresh_mode":"add_only|full"]
}';Parameters
Parameter | Required | Description |
| Yes | Defines the table name and schema. The table schema must include the partition key column.
Note Minor version 3.2.1.0 and later supports complex MaxCompute data types. 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 of the external table. To read data from and write data to MaxCompute, set this value to ODPS. |
| Yes | The endpoint of the MaxCompute service. Note You can access MaxCompute only through a VPC endpoint. To view the MaxCompute endpoint, see Endpoints. |
| Yes | The AccessKey ID of an Alibaba Cloud account or a RAM user with permissions to access MaxCompute. For information about how to obtain an AccessKey ID and an AccessKey secret, see Obtain an AccessKey pair. |
| Yes | The AccessKey secret of the Alibaba Cloud account or RAM user. For information about how to obtain an AccessKey ID and an AccessKey secret, see Obtain an AccessKey pair. |
| 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 table in the MaxCompute project. |
| No | Specifies whether to enable automatic schema refresh for the current MaxCompute external table. Valid values:
Important
|
| No | The schema refresh mode. This parameter takes effect only when
Important Supported only for clusters of minor version 3.2.7.0 or later. |
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"
}';Automatic schema refresh (auto refresh)
MaxCompute external tables support automatic schema refresh. After it is enabled, schema changes in the MaxCompute source table are automatically synchronized to the external table on each refresh cycle, without recreating the external table or refreshing it manually. Whether it is enabled and the refresh scope are controlled by the auto_refresh and auto_refresh_mode parameters that you specify when you create the table. For more information, see Parameters above.
Cluster-level configuration
Before you use the table-level auto_refresh=true setting to enable automatic schema refresh, you must run the following statements to enable the cluster-level automatic refresh switch.
-- Enable the automatic schema refresh feature.
SET ADB_CONFIG external_table_schema_refresh_enabled=true;
-- The polling interval for automatic schema refresh, in milliseconds. Default value: 300000 (5 minutes). This configuration takes effect after you restart the cluster.
SET ADB_CONFIG external_table_schema_refresh_poll_interval_ms=300000;Examples
The following example creates a MaxCompute external table with automatic schema refresh enabled in add_only mode:
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.mctest_add_only (
id BIGINT,
name VARCHAR(1024),
amount DOUBLE
) ENGINE='ODPS'
TABLE_PROPERTIES='{
"accessid":"LTAI****************",
"accesskey":"yourAccessKeySecret",
"endpoint":"https://service.cn-hangzhou-vpc.maxcompute.aliyun-inc.com/api",
"project_name":"test_adb",
"table_name":"person",
"auto_refresh":"true",
"auto_refresh_mode":"add_only"
}';After a column is added to the MaxCompute source table, the external table automatically adds the corresponding column after the next refresh. Dropped columns or column type changes in the source table are not automatically applied.
The following example creates a MaxCompute external table with automatic schema refresh enabled in full mode:
CREATE EXTERNAL TABLE IF NOT EXISTS adb_external_demo.mctest_full (
id BIGINT,
name VARCHAR(1024),
amount DOUBLE
) ENGINE='ODPS'
TABLE_PROPERTIES='{
"accessid":"LTAI****************",
"accesskey":"yourAccessKeySecret",
"endpoint":"https://service.cn-hangzhou-vpc.maxcompute.aliyun-inc.com/api",
"project_name":"test_adb",
"table_name":"person",
"auto_refresh":"true",
"auto_refresh_mode":"full"
}';After columns are added, dropped, or their types are changed in the MaxCompute source table, the external table synchronizes the metadata after the next refresh.
Usage notes
Automatic schema refresh is supported only for MaxCompute external tables that use
ENGINE='ODPS'.add_only is the default mode and is suitable for production environments in which only column additions are allowed.
full mode automatically synchronizes dropped columns and column type changes, which may affect the SQL statements, applications, reports, or views that depend on the old columns. Use this mode with caution.
If you create a view that uses
*on the MaxCompute external table (for example,CREATE VIEW view_name AS SELECT * FROM external_table), the*is expanded and saved as a fixed list of columns when the view is created. After full mode synchronizes a dropped or renamed column from the source table and the change involves a column saved in the view, querying the view returns an error indicating that the view is stale and must be re-created. This is expected behavior. We recommend that you avoid using*in views. For more information, see CREATE VIEW.
Related documentation
OSS external tables: Import data from OSS using external tables.
RDS for MySQL external tables: Import data from RDS for MySQL using external tables.
ApsaraDB for MongoDB external tables: Import data from ApsaraDB for MongoDB using external tables.
Tablestore external tables: Query and import Tablestore data.
MaxCompute external tables: Import data from MaxCompute using external tables.