All Products
Search
Document Center

AnalyticDB:CREATE EXTERNAL TABLE

Last Updated:Jul 09, 2026

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

Usage notes

You can create only OSS external tables across Alibaba Cloud accounts.

OSS external tables

Important
  • 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 TABLE statement 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

table_name (column_name column_type[, …])

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.

PARTITIONED BY (column_name column_type[, …])

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.

ROW FORMAT DELIMITED FIELDS TERMINATED BY ','

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 STORED AS TEXTFILE or STORED AS JSON is specified.

STORED AS {TEXTFILE|ORC|PARQUET|JSON|RCFILE|HUDI|ICEBERG|PAIMON}

Yes

Specifies the file format.

If the file format is .txt or .csv, set this parameter to STORED AS TEXTFILE.

Files in PARQUET format support the STRUCT data type, including nested types.

Important

Only clusters with a minor version of 3.1.8.0 or later support PARQUET files that use the STRUCT data type.

LOCATION

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:

  • The directory path must end with a forward slash (/).

  • All files in the directory must have the same file format.

  • All files in the directory must have the same number of fields, field order, and field data types.

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 oss://testBucketname/testfolder/p1=2023-06-13/data.csv, you must specify LOCATION 'oss://testBucketname/testfolder/' to create a partitioned external table where p1 is the partition key column.

Important
  • When you create a Hudi external table, make sure that the Hudi metadata file, named .hoodies, exists in the specified path.

  • If you set auto.create.location=true and the LOCATION path does not exist when you create a partitioned external table, an OSS directory is automatically created.

type

No

The type of the Hudi external table. Valid values:

  • COW (default): Copy on Write. This type is suitable for read-heavy workloads.

  • MOR: Merge on Read. This type is suitable for write-heavy workloads.

Important

This parameter is required only when STORED AS HUDI is specified.

auto.create.location

No

Specifies whether to automatically create the OSS file or directory path. Valid values:

  • true: Automatically creates the path.

  • false (default): Does not automatically create the path.

Important

This parameter takes effect only when you create a partitioned external table.

metadata_location

No

Specifies the path of the metadata file for the Iceberg external table.

Important
  • This parameter is required only when STORED AS ICEBERG is specified.

  • Use the latest metadata file to ensure that you can query the most up-to-date data.

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

Important
  • 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

table_name (column_name column_type[, …])

Yes

Defines the table name and schema.

For information about the rules for naming tables and columns, see Naming conventions.

ENGINE='MYSQL'

Yes

The storage engine of the external table. To read data from and write data to RDS for MySQL, set this value to MYSQL.

TABLE_PROPERTIES

Yes

The properties of the external table.

url

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.

tablename

Yes

The name of the table in the RDS for MySQL database.

username

Yes

The account for the RDS for MySQL database.

password

Yes

The password for the RDS for MySQL database account.

charset

No

The character set of the MySQL external table. Valid values:

  • gbk

  • UTF8 (default)

  • utf8mb4

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

Important
  • 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

table_name (column_name column_type[, …])

Yes

Defines the table name and schema.

For information about the rules for naming tables and columns, see Naming conventions.

ENGINE='MONGODB'

Yes

The storage engine of the external table. To read data from and write data to ApsaraDB for MongoDB, set this value to MONGODB.

TABLE_PROPERTIES

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

Important

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

table_name (column_name column_type[, …])

Yes

Defines the table name and schema. For information about the rules for naming tables and columns, see Naming conventions.

ENGINE='OTS'

Yes

The storage engine of the external table. To read data from and write data to Tablestore, set this value to OTS.

mapped_name

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.

location

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

Important
  • 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

table_name (column_name column_type[, …])

Yes

Defines the table name and schema. The table schema must include the partition key column.

table_name and column_name specify the table name and column name. For information about the rules for naming tables and columns, see Naming conventions.

column_type supports basic and complex MaxCompute data types, such as ARRAY, MAP, and STRUCT.

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.

ENGINE='ODPS'

Yes

The storage engine of the external table. To read data from and write data to MaxCompute, set this value to ODPS.

endpoint

Yes

The endpoint of the MaxCompute service.

Note

You can access MaxCompute only through a VPC endpoint. To view the MaxCompute endpoint, see Endpoints.

accessid

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.

accesskey

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.

partition_column

No

The partition key column. This parameter is required if the MaxCompute table is a partitioned table.

project_name

Yes

The name of the MaxCompute project.

table_name

Yes

The name of the table in the MaxCompute project.

auto_refresh

No

Specifies whether to enable automatic schema refresh for the current MaxCompute external table. Valid values:

  • true: enables the feature.

  • false (default): disables the feature.

Important
  • Supported only for clusters of minor version 3.2.7.0 or later.

  • Before you enable this feature, you must configure the cluster-level switch external_table_schema_refresh_enabled. For more information, see Cluster-level configuration in this topic.

auto_refresh_mode

No

The schema refresh mode. This parameter takes effect only when auto_refresh=true. Valid values:

  • add_only (default): automatically synchronizes only the columns that are added to the MaxCompute source table. If columns are dropped or column types are changed in the source table, only a warning is logged and the external table metadata is not automatically modified.

  • full: automatically synchronizes added columns, dropped columns, and column type changes from the MaxCompute source table to keep the external table schema consistent with the source table.

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