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
-
Navigate to the SQL Development editor.
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.
-
In the left-side navigation pane, choose .
-
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
-
Create an external database.
CREATE EXTERNAL DATABASE adb_external_db; -
Create an external table. Use the CREATE EXTERNAL TABLE statement to create an OSS external table in the
adb_external_dbexternal database. This topic uses adb_external_db.person as an example.NoteThe AnalyticDB for MySQL external table must have the same field names, number of fields, field order, and data types as the source OSS file.
For more information about the syntax for creating OSS external tables, see CREATE EXTERNAL TABLE.
-
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 | +------+-------+------+-----------+ -
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; -
Create a table in AnalyticDB for MySQL to store the imported OSS data. The following is an example statement:
NoteThe 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); -
Import data into the table.
-
Method 1: Use the
INSERT INTOstatement. If a primary key is duplicated, the new data is ignored. This is equivalent toINSERT 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 INTOstatement 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 INTOstatement 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
-
Create a database. If a database already exists, skip this step. The following is an example statement:
CREATE DATABASE adb_demo; -
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 TABLEstatement.
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":"," }';ImportantWhen 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, andpartition_columnparameters are supported. -
Parquet format: Only the
endpoint,url,accessid,accesskey,format, andpartition_columnparameters are supported. -
ORC format: Only the
endpoint,url,accessid,accesskey,format, andpartition_columnparameters 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.
-
-
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) -
Create a table in AnalyticDB for MySQL to store the imported OSS data. The following is an example statement:
NoteThe 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); -
Import data.
ImportantElastic import supports importing data only by using the
INSERT OVERWRITE INTOstatement.-
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 JOBstatement 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;ImportantYou 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.
-
-
(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_loadis 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
-
Connect to a cluster and create a database.
CREATE DATABASE adb_demo; -
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" }'; -
Query data from the
oss_import_test_external_tableexternal table.NoteFor 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; -
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); -
Run an INSERT statement to import data from the OSS external table into AnalyticDB for MySQL.
ImportantBy default, data import operations that use
INSERT INTOorINSERT OVERWRITE SELECTrun 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 useSUBMIT JOB INSERT OVERWRITE SELECTto perform the import asynchronously.-
Method 1: Run the
INSERT INTOstatement 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 toINSERT 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 OVERWRITEstatement to import data. You can useSUBMIT JOBto 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.
|
||
|
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.
|
|
|
External tables in CSV and Text formats |
delimiter|field_delimiter |
Yes |
The column delimiter of the data file.
|
|
External tables in CSV format |
null_value |
No |
Defines what represents a Important
This parameter requires a cluster with kernel version 3.1.4.2 or later. |
|
ossnull |
Defines the rule for interpreting
Note
The preceding examples assume that |
||
|
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:
Important
This parameter requires a cluster with kernel version 3.1.10.4 or later. |
-
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 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 thedelimiterparameter to the escaped value\\1. -
The default
NULLvalue for Hive TEXT files is\N. When using a CSV-formatted OSS external table to read or write these files, you must set thenull_valueparameter to the escaped value\\\\N. -
Other basic Hive data types, such as
BOOLEAN, map directly to AnalyticDB for MySQL data types. However, theBINARY,CHAR(n), andVARCHAR(n)types all map to the AnalyticDB for MySQLVARCHARtype.
Appendix: Data type mappings
-
The data types you specify when creating a table must match the mappings in the following tables. For the
DECIMALtype, the precision must also match. -
Parquet external tables do not support the
STRUCTtype. If you use this type, the table creation fails. -
ORC external tables do not support complex types such as
LIST,STRUCT, andUNION. If you use these types, the table creation fails. You can create an ORC external table that contains a column of theMAPtype, 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 |
|
DECIMAL |
DECIMAL |
|
BINARY |
UTF-8 |
|
|
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 |
|
|
|
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 |