All Products
Search
Document Center

AnalyticDB:Import OSS data using external tables

Last Updated:May 07, 2026

AnalyticDB for MySQL supports importing external data using external tables. This topic explains how to use them to import data from OSS into an AnalyticDB for MySQL cluster.

Prerequisites

  • The AnalyticDB for MySQL cluster and the OSS bucket are in the same region. For more information, see Activate OSS.

  • You have uploaded the data files to an OSS directory.

  • Elastic network interface (ENI) access is enabled for your AnalyticDB for MySQL Data Warehouse Edition cluster.

    Important
    • Log on to the AnalyticDB for MySQL console. On the Cluster Information page, under Network Information, turn on the ENI network switch.

    • Enabling or disabling the ENI network interrupts database connectivity for about 2 minutes, making read and write operations unavailable. Carefully evaluate the impact before you enable or disable the ENI network.

Data preparation

This example uploads the data file person.csv to the testBucketName/adb/dt=2023-06-15 directory in OSS. The file uses a line feed as the row delimiter and a comma (,) as the column delimiter. The person.csv file contains the following sample data:

1,james,10,2023-06-15
2,bond,20,2023-06-15
3,jack,30,2023-06-15
4,lucy,40,2023-06-15       

Procedure

Enterprise, basic, and data lakehouse editions

  1. Navigate to the SQL Development editor.

    1. 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.

    2. In the left-side navigation pane, choose Job Development > SQL Development.

  2. Import data.

    You can import data using either regular import (the default) or elastic import. In regular import mode, the system reads source data from compute nodes and creates indexes on storage nodes, consuming both compute and storage resources. The elastic import method is supported only for Enterprise Edition, Basic Edition, and Data Lakehouse Edition clusters that run kernel version 3.1.10.0 or later and have a job-type resource group. For more information, see Data import methods.

    Regular import

    1. Create an external database.

      CREATE EXTERNAL DATABASE adb_external_db;
    2. Create an external table. Use the CREATE EXTERNAL TABLE statement to create an OSS external table in the adb_external_db external database. This topic uses adb_external_db.person as an example.

      Note

      The AnalyticDB for MySQL external table must have the same field names, number of fields, field order, and data types as the source OSS file.

      Create a non-partitioned OSS external table

      CREATE EXTERNAL TABLE adb_external_db.person
      (
       id INT,
       name VARCHAR(1023),
       age INT,
       dt VARCHAR(1023)
      )
      ROW FORMAT DELIMITED FIELDS TERMINATED BY  ','
      STORED AS TEXTFILE
      LOCATION  'oss://testBucketName/adb/dt=2023-06-15/';

      Create a partitioned OSS external table

      Before you can query a partitioned OSS external table, you must create it and add the necessary partitions.

      1. Create a partitioned OSS external table.

        CREATE EXTERNAL TABLE adb_external_db.person
        (
         id INT,
         name VARCHAR(1023) ,
         age INT
        )
        PARTITIONED BY (dt STRING)
        ROW FORMAT DELIMITED FIELDS TERMINATED BY  ','
        STORED AS TEXTFILE
        LOCATION  'oss://testBucketName/adb/';
      2. You can use the ALTER TABLE ADD PARTITION statement to add a partition manually, or use the MSCK REPAIR TABLE statement to automatically find and add partitions.

        ALTER TABLE adb_external_db.person ADD PARTITION (dt='2023-06-15') LOCATION 'oss://testBucketName/adb/dt=2023-06-15/';
        Note

      For more information about the syntax for creating OSS external tables, see CREATE EXTERNAL TABLE.

    3. Query data.

      After creating the external table, you can run a SELECT statement in AnalyticDB for MySQL to query data from OSS.

      SELECT * FROM adb_external_db.person;

      The following result is returned:

      +------+-------+------+-----------+
      | id   | name  | age  | dt        |
      +------+-------+------+-----------+
      |    1 | james |   10 |2023-06-15 |
      |    2 | bond  |   20 |2023-06-15 |
      |    3 | jack  |   30 |2023-06-15 |
      |    4 | lucy  |   40 |2023-06-15 |
      +------+-------+------+-----------+
    4. Create a database in AnalyticDB for MySQL. If a database already exists, you can skip this step. The following is an example statement:

      CREATE DATABASE adb_demo; 
    5. Create a table in AnalyticDB for MySQL to store the imported OSS data. The following is an example statement:

      Note

      The internal table must match the external table from step b in field names, number of fields, field order, and data types.

      CREATE TABLE IF NOT EXISTS adb_demo.adb_import_test(
          id INT,
          name VARCHAR(1023),
          age INT,
          dt VARCHAR(1023)
      )
      DISTRIBUTED BY HASH(id);
    6. Import data into the table.

      • Method 1: Use the INSERT INTO statement. If a primary key is duplicated, the new data is ignored. This is equivalent to INSERT IGNORE INTO. For more information, see INSERT INTO. Example:

        INSERT INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.person;
      • Method 2: Use the INSERT OVERWRITE INTO statement to import data synchronously. This overwrites existing data in the table. Example:

        INSERT OVERWRITE INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.person;
      • Method 3: Use the INSERT OVERWRITE INTO statement to import data asynchronously. For more information, see asynchronous write. Example:

        SUBMIT JOB INSERT OVERWRITE adb_demo.adb_import_test SELECT * FROM adb_external_db.person;

    Elastic import

    1. Create a database. If a database already exists, skip this step. The following is an example statement:

      CREATE DATABASE adb_demo; 
    2. Create an external table.

      Note
      • The AnalyticDB for MySQL external table must have the same field names, number of fields, field order, and data types as the source OSS file.

      • Elastic import only supports creating external tables with the CREATE TABLE statement.

      CREATE TABLE oss_import_test_external_table
      (
        id INT(1023),
        name VARCHAR(1023),
        age INT,
        dt VARCHAR(1023)
      )
      ENGINE='OSS'
      TABLE_PROPERTIES='{
          "endpoint":"oss-cn-hangzhou-internal.aliyuncs.com",
          "url":"oss://testBucketName/adb/dt=2023-06-15/person.csv",
          "accessid":"accesskey_id",
          "accesskey":"accesskey_secret",
          "delimiter":","
      }';
      Important

      When you create an external table, the supported TABLE_PROPERTIES parameters vary based on the file format (CSV, Parquet, or ORC):

      • CSV format: Only the endpoint, url, accessid, accesskey, format, delimiter, null_value, and partition_column parameters are supported.

      • Parquet format: Only the endpoint, url, accessid, accesskey, format, and partition_column parameters are supported.

      • ORC format: Only the endpoint, url, accessid, accesskey, format, and partition_column parameters are supported.

      For more information about the parameters that you can set for external tables and their descriptions, see OSS non-partitioned external tables and OSS partitioned external tables.

    3. Query data.

      After the external table is created, you can run a SELECT statement in AnalyticDB for MySQL to query data from OSS.

      SELECT * FROM oss_import_test_external_table;

      The following result is returned:

      +------+-------+------+-----------+
      | id   | name  | age  | dt        |
      +------+-------+------+-----------+
      |    1 | james |   10 |2023-06-15 |
      |    2 | bond  |   20 |2023-06-15 |
      |    3 | jack  |   30 |2023-06-15 |
      |    4 | lucy  |   40 |2023-06-15 |
      +------+-------+------+-----------+
      4 rows in set (0.35 sec)
    4. Create a table in AnalyticDB for MySQL to store the imported OSS data. The following is an example statement:

      Note

      The internal table must match the external table from step b in field names, number of fields, field order, and data types.

      CREATE TABLE adb_import_test
      (
        id INT,
        name VARCHAR(1023),
        age INT,
        dt VARCHAR(1023)
      )
      DISTRIBUTED BY HASH(id);
    5. Import data.

      Important

      Elastic import supports importing data only by using the INSERT OVERWRITE INTO statement.

      • Method 1: Run the INSERT OVERWRITE INTO statement to elastically import data, overwriting the existing data in the table. The following is an example statement:

        /*+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.oss_import_test_external_table;
      • Method 2: Asynchronously run the INSERT OVERWRITE INTO statement to elastically import data. You can use the SUBMIT JOB statement to submit an asynchronous task that is scheduled in the background.

        /*+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.oss_import_test_external_table;
        Important

        You cannot set a priority queue when you asynchronously submit an elastic import task.

        The following result is returned:

        +---------------------------------------+
        | job_id                                |
        +---------------------------------------+
        | 202308151719510210170190**********    |

      After you submit an asynchronous task by using SUBMIT JOB, the returned result indicates only that the task was submitted successfully. You can use the job ID to terminate the asynchronous task or query its status to determine whether the task was successfully executed. For more information, see Submit an asynchronous import job.

      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.

    6. (Optional) Check whether the submitted import task is an elastic import task.

      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  |
      +---------------------------------------+------------------+
      | 20230815171951021017019072*********** |       1          |
      +---------------------------------------+------------------+

      If the value of is_elastic_load is 1, the submitted import task is an elastic import task. If the value is 0, the task is a regular import task.

Data warehouse edition

  1. Connect to a cluster and create a database.

    CREATE DATABASE adb_demo;
  2. Create an external table. Use the CREATE TABLE syntax to create an OSS external table in CSV, Parquet, or ORC format. For more information about the syntax, see OSS external table syntax.

    This topic uses a non-partitioned external table in CSV format as an example.

    CREATE TABLE IF NOT EXISTS oss_import_test_external_table
    (
        id INT,
        name VARCHAR(1023),
        age INT,
        dt VARCHAR(1023) 
    )
    ENGINE='OSS'
    TABLE_PROPERTIES='{
        "endpoint":"oss-cn-hangzhou-internal.aliyuncs.com",
        "url":"oss://testBucketname/adb/dt=2023-06-15/person.csv",
        "accessid":"accesskey_id",
        "accesskey":"accesskey_secret",
        "delimiter":",",
        "skip_header_line_count":0,
        "charset":"utf-8"
    }'; 
  3. Query data from the oss_import_test_external_table external table.

    Note

    For CSV, Parquet, or ORC data files, querying a large external table can cause significant performance overhead. To improve query efficiency, we recommend importing the data from the OSS external table into AnalyticDB for MySQL before running queries, as described in Steps 4 and 5.

    SELECT * FROM oss_import_test_external_table;
  4. Create a table in AnalyticDB for MySQL to store the data imported from the OSS external table.

    CREATE TABLE IF NOT EXISTS adb_oss_import_test
    (
       id INT,
       name VARCHAR(1023),
       age INT,
       dt VARCHAR(1023) 
    )
    DISTRIBUTED BY HASH(id);
  5. Run an INSERT statement to import data from the OSS external table into AnalyticDB for MySQL.

    Important

    By default, data import operations that use INSERT INTO or INSERT OVERWRITE SELECT run synchronously. For large datasets, such as those in the hundreds of gigabytes, the client must maintain a persistent connection to the AnalyticDB for MySQL server for a long time. During this time, network issues may interrupt the connection and cause the import to fail. Therefore, for large data volumes, we recommend that you use SUBMIT JOB INSERT OVERWRITE SELECT to perform the import asynchronously.

    • Method 1: Run the INSERT INTO statement to import data. If a primary key is duplicated, the current write operation is ignored and the data is not updated. This behavior is equivalent to INSERT IGNORE INTO. For more information, see INSERT INTO. The following is an example statement:

      INSERT INTO adb_oss_import_test
      SELECT * FROM oss_import_test_external_table;
    • Method 2: Run the INSERT OVERWRITE statement to import data, overwriting the existing data in the table. The following is an example statement:

      INSERT OVERWRITE adb_oss_import_test
      SELECT * FROM oss_import_test_external_table;
    • Method 3: Asynchronously run the INSERT OVERWRITE statement to import data. You can use SUBMIT JOB to submit an asynchronous task for background scheduling. You can add a hint (/*+ direct_batch_load=true*/) before the write task to accelerate the task. For more information, see Asynchronous write. The following is an example statement:

      SUBMIT JOB INSERT OVERWRITE adb_oss_import_test
      SELECT * FROM oss_import_test_external_table;

      The following result is returned:

      +---------------------------------------+
      | job_id                                |
      +---------------------------------------+
      | 2020112122202917203100908203303****** |

      For more information about how to submit an asynchronous task, see Submit an asynchronous import job.

OSS external table syntax

Enterprise, Basic, and Data Lakehouse

For information about the syntax for creating OSS external tables in Enterprise Edition, Basic Edition, and Data Lakehouse Edition, see OSS external tables.

Data Warehouse

OSS non-partitioned external table

CREATE TABLE [IF NOT EXISTS] table_name
(column_name column_type[, …])
ENGINE='OSS'
TABLE_PROPERTIES='{
    "endpoint":"endpoint",
    "url":"OSS_LOCATION",
    "accessid":"accesskey_id",
    "accesskey":"accesskey_secret",
    "format":"csv|orc|parquet|text
    "delimiter|field_delimiter":";",
    "skip_header_line_count":1,
    "charset":"utf-8"
}';

External table type

Parameter

Required

Description

External tables in CSV, Parquet, and ORC formats

ENGINE='OSS'

Yes

The table engine. Set the value to OSS.

endpoint

The Endpoint of the OSS bucket. Currently, AnalyticDB for MySQL can access OSS only over a VPC network.

Note

Log on to the OSS console, click the target bucket, and view the Endpoint on the Overview page of the bucket.

url

The path to the OSS file or directory.

  • For an OSS file, specify the absolute path. Example: oss://testBucketname/adb/oss_import_test_data.csv.

  • Directory paths must end with a forward slash (/). Example: oss://testBucketname/adb/.

    Note

    The external table will include all data from the specified directory.

  • Use the wildcard character (*) at the end of the path to match all files or folders with a specific prefix. Example: oss://testBucketname/adb/list_file_with_prefix/test*

    Note

    The wildcard in this example matches all files and folders that match the prefix, such as oss://testBucketname/adb/list_file_with_prefix/testfile1 and

    oss://testBucketname/adb/list_file_with_prefix/test1/file2.

accessid

The AccessKey ID of an Alibaba Cloud account or a RAM user that has OSS management permissions.

For information about how to obtain an AccessKey ID, see Accounts and permissions.

accesskey

The AccessKey Secret of an Alibaba Cloud account or a RAM user that has OSS management permissions.

To obtain an AccessKey Secret, see Accounts and permissions.

format

Conditionally required

The file format.

  • To create a Parquet external table, you must set this parameter to parquet.

  • To create an ORC external table, you must set this parameter to orc.

  • To create a Text external table, you must set this parameter to text.

  • If you do not specify this parameter, the default file format is csv.

    Note

    The parameter value is case-sensitive.

External tables in CSV and Text formats

delimiter|field_delimiter

Yes

The column delimiter of the data file.

  • If the file type is csv, the parameter name is delimiter.

  • If the file type is text, the parameter name is field_delimiter.

External tables in CSV format

null_value

No

Defines what represents a NULL value in the CSV data file. By default, an empty value is treated as NULL. Example: "null_value": "".

Important

This parameter requires a cluster with kernel version 3.1.4.2 or later.

ossnull

Defines the rule for interpreting NULL values in the CSV data file. Valid values:

  • 1 (default): EMPTY_SEPARATORS. Treats only empty values as NULL.

    Example: a,"",,c --> "a","",NULL,"c"

  • 2: EMPTY_QUOTES. Treats only "" as NULL.

    Example: a,"",,c --> "a",NULL,"","c"

  • 3: BOTH. Treats both empty values and "" as NULL.

    Example: a,"",,c --> "a",NULL,NULL,"c"

  • 4: NEITHER. Treats neither empty values nor "" as NULL.

    Example: a,"",,c --> "a","","","c"

Note

The preceding examples assume that "null_value": "".

skip_header_line_count

The number of rows to skip from the beginning of the data file. For example, if a CSV file has a header row, set this parameter to 1 to skip it.

The default value is 0, which indicates that no rows are skipped.

oss_ignore_quote_and_escape

If set to true, quotation marks and escape characters in field values are ignored. The default is false.

Important

This parameter requires a cluster with kernel version 3.1.4.2 or later.

charset

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

  • utf-8 (default)

  • gbk

Important

This parameter requires a cluster with kernel version 3.1.10.4 or later.

Note
  • The column names and their order in the CREATE EXTERNAL TABLE statement must match those in the source Parquet or ORC file. Column names are case-insensitive.

  • You can create an external table using a subset of columns from the source file. Columns not specified in the CREATE EXTERNAL TABLE statement are ignored.

  • If the CREATE EXTERNAL TABLE statement includes a column that does not exist in the Parquet or ORC file, queries on that column return NULL.

AnalyticDB for MySQL can read from and write to Hive TEXT files using an OSS external table in CSV format. Use the following statement to create the table:

CREATE TABLE adb_csv_hive_format_oss (
  a tinyint,
  b smallint,
  c int,
  d bigint,
  e boolean,
  f float,
  g double,
  h varchar,
  i varchar, -- binary
  j timestamp,
  k DECIMAL(10, 4),
  l varchar, -- char(10)
  m varchar, -- varchar(100)
  n date
) ENGINE = 'OSS' TABLE_PROPERTIES='{
    "format": "csv",
    "endpoint":"oss-cn-hangzhou-internal.aliyuncs.com",
    "accessid":"accesskey_id",
    "accesskey":"accesskey_secret",
    "url":"oss://testBucketname/adb_data/",
    "delimiter": "\\1",
    "null_value": "\\\\N",
    "oss_ignore_quote_and_escape": "true",
    "ossnull": 2
}';
Note

Note the following points when you create an OSS external table in CSV format to read Hive TEXT files:

  • The default column delimiter for Hive TEXT files is \1. When using a CSV-formatted OSS external table to read or write these files, you must set the delimiter parameter to the escaped value \\1.

  • The default NULL value for Hive TEXT files is \N. When using a CSV-formatted OSS external table to read or write these files, you must set the null_value parameter to the escaped value \\\\N.

  • Other basic Hive data types, such as BOOLEAN, map directly to AnalyticDB for MySQL data types. However, the BINARY, CHAR(n), and VARCHAR(n) types all map to the AnalyticDB for MySQL VARCHAR type.

Appendix: Data type mappings

Important
  • The data types you specify when creating a table must match the mappings in the following tables. For the DECIMAL type, the precision must also match.

  • Parquet external tables do not support the STRUCT type. If you use this type, the table creation fails.

  • ORC external tables do not support complex types such as LIST, STRUCT, and UNION. If you use these types, the table creation fails. You can create an ORC external table that contains a column of the MAP type, but queries against the table will fail.

Parquet file and AnalyticDB for MySQL data type mapping

Parquet primitive type

Parquet logical type

AnalyticDB for MySQL type

BOOLEAN

None

BOOLEAN

INT32

INT_8

TINYINT

INT32

INT_16

SMALLINT

INT32

None

INT or INTEGER

INT64

None

BIGINT

FLOAT

None

FLOAT

DOUBLE

None

DOUBLE

  • FIXED_LEN_BYTE_ARRAY

  • BINARY

  • INT64

  • INT32

DECIMAL

DECIMAL

BINARY

UTF-8

  • VARCHAR

  • STRING

  • JSON (if the Parquet column contains JSON)

INT32

DATE

DATE

INT64

TIMESTAMP_MILLIS

TIMESTAMP or DATETIME

INT96

None

TIMESTAMP or DATETIME

ORC file to AnalyticDB for MySQL data type mapping

ORC type

AnalyticDB for MySQL type

BOOLEAN

BOOLEAN

BYTE

TINYINT

SHORT

SMALLINT

INT

INT or INTEGER

LONG

BIGINT

DECIMAL

DECIMAL

FLOAT

FLOAT

DOUBLE

DOUBLE

  • BINARY

  • STRING

  • VARCHAR

  • VARCHAR

  • STRING

  • JSON (if the ORC column contains JSON)

TIMESTAMP

TIMESTAMP or DATETIME

DATE

DATE

Paimon files and AnalyticDB for MySQL data type mapping

Paimon type

AnalyticDB for MySQL type

CHAR

VARCHAR

VARCHAR

VARCHAR

BOOLEAN

BOOLEAN

BINARY

VARBINARY

VARBINARY

VARBINARY

DECIMAL

DECIMAL

TINYINT

TINYINT

SMALLINT

SMALLINT

INT

INTEGER

BIGINT

BIGINT

FLOAT

REAL

DOUBLE

DOUBLE

DATE

DATE

TIME

Not supported

TIMESTAMP

TIMESTAMP

LocalZonedTIMESTAMP

TIMESTAMP (ignores local time zone information)

ARRAY

ARRAY

MAP

MAP

ROW

ROW