Use the UNLOAD command
The UNLOAD command exports data from MaxCompute tables to Object Storage Service (OSS) or Hologres, making it available to other compute engines such as Amazon Redshift and BigQuery.
Learn how to use the UNLOAD command with syntax references and examples.
You can run the commands in this topic using the following tools or methods:
Features
The unload command exports data from MaxCompute to external storage such as OSS and Hologres. OSS supports storing data in CSV or other open-source formats.
To export MaxCompute data to other compute engines such as Amazon Redshift and BigQuery, you must first export the data to OSS.
The export operation appends new files instead of overwriting existing ones.
Limitations
You cannot use the dual-signature authorization mode when exporting data from MaxCompute to Hologres.
You cannot export data from MaxCompute to a Hologres partitioned table.
You cannot add a file extension to open-source format files during export.
Authorization
The user account has the required MaxCompute permissions.
The user account must have at least the read (Select) permission on the MaxCompute table to be exported. To grant permissions, see MaxCompute permissions.
The user account has the required permissions on the data source in external storage.
You must authorize MaxCompute to access the external storage (OSS or Hologres) before exporting data. The
UNLOADcommand uses the same authorization mode as MaxCompute external tables. The authorization methods for OSS and Hologres are as follows:OSS: Uses one-click authorization for enhanced security. The role name is AliyunODPSDefaultRole. For details, see Authorization scheme for accessing external data sources.
Hologres: Create a RAM role, grant it the required Hologres permissions, and add the role to the Hologres instance. For detailed instructions, see Create a Hologres external table (STS mode).
Data export
Using built-in extractors (StorageHandler)
Syntax
UNLOAD FROM {<select_statement>|<table_name> [PARTITION (<pt_spec>)]}
INTO | OVERWRITE
LOCATION <external_location>
STORED BY <StorageHandler>
[WITH SERDEPROPERTIES ('<property_name>'='<property_value>',...)];Exporting to Hologres does not support the OVERWRITE mode.
Parameters
External storage: OSS
Parameter | Required | Description |
select_statement | No | A SELECT query statement that specifies the data to export from the source table to the destination OSS path. |
table_name, pt_spec | No |
|
external_location | Yes | Specifies the destination OSS path for the exported data. The format is |
StorageHandler | Yes | The name of a built-in StorageHandler. Valid values are:
These built-in text data parsers define how MaxCompute reads data from and writes data to CSV and TSV files. By default, exported files are given the |
'<property_name>'='<property_value>' | No |
|
For more information, see OSS external table.
External storage: Hologres
Parameter | Required | Description |
select_statement | No | A SELECT query statement that specifies the data to export from the source table to the destination Hologres table. |
table_name | No |
|
external_location | Yes |
|
StorageHandler | Yes |
|
'<property_name>'='<property_value>' | No |
|
For more information, see Hologres external table.
Examples
External storage: OSS
Export data from the sale_detail table in a MaxCompute project to OSS.
Sample data
The
sale_detailtable contains the following data:+------------+-------------+-------------+------------+------------+ | shop_name | customer_id | total_price | sale_date | region | +------------+-------------+-------------+------------+------------+ | s1 | c1 | 100.1 | 2013 | china | | s2 | c2 | 100.2 | 2013 | china | | s3 | c3 | 100.3 | 2013 | china | | null | c5 | NULL | 2014 | shanghai | | s6 | c6 | 100.4 | 2014 | shanghai | | s7 | c7 | 100.5 | 2014 | shanghai | +------------+-------------+-------------+------------+------------+Log on to the OSS console. Create the
oss-unload-test/data_location/directory in an OSS bucket. In this example, the region isoss-cn-hangzhou. After you create the directory, confirm that it exists.The OSS path is:
oss://oss-cn-hangzhou-internal.aliyuncs.com/oss-unload-test/data_location.Log on to the MaxCompute client and run the UNLOAD command to export data from the
sale_detailtable to OSS.Example 1: Export data from the sale_detail table in CSV format and use GZIP compression. Run the following command:
-- Control the number of exported files: set the amount of MaxCompute table data read by a single worker. Unit: MB. -- Data in MaxCompute tables is compressed. The exported data typically expands by about four times when stored in OSS. SET odps.stage.mapper.split.size=256; -- Export data UNLOAD FROM (SELECT * FROM sale_detail) INTO LOCATION 'oss://oss-cn-hangzhou-internal.aliyuncs.com/oss-unload-test/data_location' STORED BY 'com.aliyun.odps.CsvStorageHandler' WITH SERDEPROPERTIES ( 'odps.properties.rolearn'='acs:ram::<uid>:role/AliyunODPSDefaultRole', 'odps.text.option.gzip.output.enabled'='true');UNLOAD FROM (SELECT * FROM sale_detail)is equivalent toUNLOAD FROM sale_detail.Example 2: Export data from the partition where sale_date='2013' and region='china' in the sale_detail table to a TSV file and use GZIP compression. Run the following command:
-- Control the number of exported files: set the amount of MaxCompute table data read by a single worker. Unit: MB. -- Data in MaxCompute tables is compressed. The exported data typically expands by about four times when stored in OSS. SET odps.stage.mapper.split.size=256; -- Export data. UNLOAD FROM sale_detail PARTITION (sale_date='2013',region='china') INTO LOCATION 'oss://oss-cn-hangzhou-internal.aliyuncs.com/oss-unload-test/data_location' STORED BY 'com.aliyun.odps.TsvStorageHandler' WITH SERDEPROPERTIES ( 'odps.properties.rolearn'='acs:ram::<uid>:role/AliyunODPSDefaultRole', 'odps.text.option.gzip.output.enabled'='true');
In both examples, the
'odps.text.option.gzip.output.enabled'='true'parameter enables GZIP compression for the exported files. Only GZIP is supported. For more information about parameters, see OSS external table.
External storage: Hologres
Export data from the data_test table in a MaxCompute project to Hologres.
Sample data
The
data_testtable contains the following data:+------------+----------+ | id | name | +------------+----------+ | 3 | rgege | | 4 | Gegegegr | +------------+----------+In Hologres, create a destination table named
mc_2_holoin thetestdatabase.You can run the CREATE TABLE statement in the HoloWeb SQL editor. For more information, see Connect to HoloWeb and run queries. The statement is as follows:
CREATE TABLE mc_2_holo (id INT, name TEXT);The data type of each column in the destination table must match the corresponding data type in the MaxCompute source table. For details, see Data type mapping between MaxCompute and Hologres.
Log on to the MaxCompute client and run the UNLOAD command to export data from the
data_testtable to Hologres. The following is a sample command:UNLOAD FROM (SELECT * FROM data_test) INTO LOCATION 'jdbc:postgresql://hgprecn-cn-5y**********-cn-hangzhou-internal.hologres.aliyuncs.com:80/test?ApplicationName=MaxCompute¤tSchema=public&useSSL=false&table=mc_2_holo/' STORED BY 'com.aliyun.odps.jdbc.JdbcStorageHandler' WITH SERDEPROPERTIES ( 'odps.properties.rolearn'='acs:ram::13**************:role/aliyunodpsholorole', 'mcfed.mapreduce.jdbc.driver.class'='org.postgresql.Driver', 'odps.federation.jdbc.target.db.type'='holo' );Query the exported data in Hologres:
SELECT * FROM mc_2_holo;Example result:
id name 4 Gegegegr 3 rgege
Export data in other open-source formats
Syntax
UNLOAD FROM {<select_statement>|<table_name> [PARTITION (<pt_spec>)]}
INTO | OVERWRITE
LOCATION <external_location>
[ROW FORMAT SERDE '<serde_class>'
[WITH SERDEPROPERTIES ('<property_name>'='<property_value>',...)]
]
STORED AS <file_format>
[PROPERTIES ('<tbproperty_name>'='<tbproperty_value>')];Parameters
Parameter | Required | Description |
select_statement | No | A SELECT query statement that specifies the data to export from the source table to the destination OSS path. |
table_name, pt_spec | No |
|
external_location | Yes | Specifies the destination OSS path for the exported data. The format is |
serde_class | No | The Serializer/Deserializer (SerDe) class. For usage details, see Create an OSS external table. |
'<property_name>'='<property_value>' | No |
The supported properties are the same as those for MaxCompute external tables. For more information, see Create an OSS external table. |
file_format | Yes | The file format for the exported data. Valid values include ORC, PARQUET, RCFILE, SEQUENCEFILE, and TEXTFILE. The usage is the same as for MaxCompute external tables. For more information, see Create an OSS external table. |
'<tbproperty_name>'='<tbproperty_value>' | No |
For example, to export data in SNAPPY or LZO compression format, set the compression property as follows:
When you export data in SNAPPY or LZO compression format, the exported files do not have the |
Examples
Sample data
-- Data in the sale_detail table. +------------+-------------+-------------+------------+------------+ | shop_name | customer_id | total_price | sale_date | region | +------------+-------------+-------------+------------+------------+ | s1 | c1 | 100.1 | 2013 | china | | s2 | c2 | 100.2 | 2013 | china | | s3 | c3 | 100.3 | 2013 | china | | null | c5 | NULL | 2014 | shanghai | | s6 | c6 | 100.4 | 2014 | shanghai | | s7 | c7 | 100.5 | 2014 | shanghai | +------------+-------------+-------------+------------+------------+Log on to the OSS console. Create the
oss-unload-test/data_location/directory in an OSS bucket. In this example, the region isoss-cn-hangzhou. After you create the directory, confirm that it exists.The OSS path is:
oss://oss-cn-hangzhou-internal.aliyuncs.com/oss-unload-test/data_location.Log on to the MaxCompute client and run the UNLOAD command to export data from the
sale_detailtable to OSS.Example 1: Export data from the
sale_detailtable in PARQUET format and use SNAPPY compression.-- Control the number of exported files: set the amount of MaxCompute table data read by a single worker. Unit: MB. -- Data in MaxCompute tables is compressed. The exported data typically expands by about four times when stored in OSS. SET odps.stage.mapper.split.size=256; -- Export data UNLOAD FROM (SELECT * FROM sale_detail) INTO LOCATION 'oss://oss-cn-hangzhou-internal.aliyuncs.com/oss-unload-test/data_location' ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' WITH SERDEPROPERTIES ('odps.properties.rolearn'='acs:ram::<uid>:role/AliyunODPSDefaultRole') STORED AS PARQUET PROPERTIES('mcfed.parquet.compression'='SNAPPY');Example 2: Export data from the partition where sale_date='2013' and region='china' in the
sale_detailtable in PARQUET format and use SNAPPY compression.-- Control the number of exported files: set the amount of MaxCompute table data read by a single worker. Unit: MB. -- Data in MaxCompute tables is compressed. The exported data typically expands by about four times when stored in OSS. SET odps.stage.mapper.split.size=256; --Export data. UNLOAD FROM sale_detail PARTITION (sale_date='2013',region='china') INTO LOCATION 'oss://oss-cn-hangzhou-internal.aliyuncs.com/oss-unload-test/data_location' ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' WITH SERDEPROPERTIES ('odps.properties.rolearn'='acs:ram::<uid>:role/AliyunODPSDefaultRole') STORED AS PARQUET PROPERTIES('mcfed.parquet.compression'='SNAPPY');Example 3: Export data from the
sale_detailtable to a text file and use a comma (,) as the delimiter.SET odps.sql.allow.fullscan=true; UNLOAD FROM (SELECT * FROM sale_detail) INTO LOCATION 'oss://oss-cn-hangzhou-internal.aliyuncs.com/oss-unload-test/data_location' ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ('field.delim'=',') STORED AS TEXTFILE PROPERTIES('odps.external.data.enable.extension'='true');
Set file prefixes, suffixes, and extensions
When exporting a MaxCompute table to files with the UNLOAD command, you can customize the file prefix and suffix. A file extension is automatically generated based on the file format.
Syntax
To export files in formats like CSV and TSV using a built-in parser:
UNLOAD FROM {<select_statement>|<table_name> [PARTITION (<pt_spec>)]} INTO | OVERWRITE LOCATION <external_location> [STORED BY <StorageHandler>] [WITH SERDEPROPERTIES ('<property_name>'='<property_value>',...)];To export files in open-source file formats, like ORC or Parquet:
UNLOAD FROM {<select_statement>|<table_name> [PARTITION (<pt_spec>)]} INTO | OVERWRITE LOCATION <external_location> [ROW FORMAT SERDE '<serde_class>' [WITH SERDEPROPERTIES ('<property_name>'='<property_value>',...)] ] STORED AS <file_format> [PROPERTIES('<tbproperty_name>'='<tbproperty_value>')];
Parameters
Parameter tbproperty_name | Use case | Description | Value |
odps.external.data.output.prefix (Compatible with odps.external.data.prefix) | Adds a custom prefix to the names of the output files. |
| A valid string, such as |
odps.external.data.enable.extension | Specifies whether to show the file extensions of the output files. | Set the value to True to show file extensions or False to hide them. The default is False. |
|
odps.external.data.output.suffix | Adds a custom suffix to the names of the output files. | Can contain only letters (a-z, A-Z), digits (0-9), and underscores (_). | A valid string, such as |
odps.external.data.output.explicit.extension | Adds a custom file extension to the names of the output files. |
| A valid string, such as |
For more information about other parameters, see Parameters for built-in extractors and Parameters for other open-source data formats.
File extensions
The following table lists the file extensions automatically generated for each external table format when odps.external.data.enable.extension=true.
File format | Serde | File extension |
SEQUENCEFILE | org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe | .sequencefile |
TEXTFILE | org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe | .txt |
RCFILE | org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe | .rcfile |
ORC | org.apache.hadoop.hive.ql.io.orc.OrcSerde | .orc |
PARQUET | org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe | .parquet |
AVRO | org.apache.hadoop.hive.serde2.avro.AvroSerDe | .avro |
JSON | org.apache.hive.hcatalog.data.JsonSerDe | .json |
Examples
Export data to a
textfile, add the prefix mc_ and the suffix _hangzhou, and automatically generate the.txtfile extension.UNLOAD FROM (SELECT * FROM sale_detail) INTO LOCATION 'oss://oss-cn-hangzhou-internal.aliyuncs.com/oss-unload-test/data_location' ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' STORED AS TEXTFILE PROPERTIES ( 'odps.external.data.output.prefix'='mc_', 'odps.external.data.output.suffix'='_hangzhou', 'odps.external.data.enable.extension'='true');Check the result in the specified OSS destination path:
An example exported filename is
mc_20251120162614634grey9oeujm16_M1_1_0_0-0_TableSink1_hangzhou.txt.
Export data to downstream systems using OSS
MaxCompute can export data through OSS to databases that are not directly supported, such as the time-series database InfluxDB.
Use the
UNLOADcommand to export the data to OSS.Download the data from OSS to your local environment.
Use the target database's import tools or commands to load the data.
Billing
Compute costs: The UNLOAD command itself does not incur a charge. The query clause within UNLOAD scans data and uses compute resources, and is billed as a standard SQL job.
Storage costs: Exporting structured data to OSS can reduce storage costs in some scenarios, but you should estimate costs in advance.
The storage cost for MaxCompute is CNY 0.12 per GB per month. For more information, see Storage costs. Data imported into MaxCompute has a compression ratio of about 5:1, and we bill you based on the compressed data size.
The price for standard storage in OSS is CNY 0.12 per GB per month. OSS also provides other storage classes, such as infrequent access, archive, and cold archive. For more details, see Storage costs.
If you export data solely to save on storage costs, estimate the compression ratio based on your data characteristics, calculate the UNLOAD operation cost based on your query, and evaluate your future data access patterns. This helps avoid extra charges from unnecessary data migration.
Related documentation
To import data in CSV or other open-source formats from external storage into MaxCompute, see LOAD.
To learn the syntax for creating, reading, and writing an OSS external table, see OSS external table.
To learn more about Hologres external tables, see Hologres external table.
FAQ
Q: Can I enclose each field in double quotes when exporting data from a MaxCompute table to external storage (OSS or Hologres)? For example: "cloud-native big data computing service", "MaxCompute", "Hello".
A: Yes. Use one of the following methods:
Modify the table fields: Manually concatenate strings with CONCAT.
Adjust the table format: When you create an external table with the open-source data parser
org.apache.hadoop.hive.serde2.OpenCSVSerde, double quotes are added to each field by default.If you create an OSS external table with a built-in text data parser such as
com.aliyun.odps.CsvStorageHandler/TsvStorageHandler, these parsers do not support this output format, and no parameter exists to enable it.
For more information about data parsers and parameters, see OSS external table.