Use the UNLOAD command

Updated at:

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

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

  2. 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 UNLOAD command uses the same authorization mode as MaxCompute external tables. The authorization methods for OSS and Hologres are as follows:

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

  • Specifies the data to export by using the table name or the table name and partition name.

  • The format of pt_spec is as follows:

    (partition_col1 = partition_col_value1, partition_col2 = partition_col_value2, ...)

  • This export method generates no query statement and incurs no cost.

external_location

Yes

Specifies the destination OSS path for the exported data. The format is 'oss://<oss_endpoint>/<object>'.

StorageHandler

Yes

The name of a built-in StorageHandler. Valid values are:

  • com.aliyun.odps.CsvStorageHandler

  • com.aliyun.odps.TsvStorageHandler

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 .csv or .tsv extension.

'<property_name>'='<property_value>'

No

  • property_name is the property name.

  • property_value is the property value.

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

  • Specifies the data to export by using the table name.

  • This export method generates no query statement and incurs no cost.

external_location

Yes

  • Specifies the destination Hologres path where the exported data will be stored.

  • The format is 'jdbc:postgresql://<endpoint>:<port>/<database>?ApplicationName=MaxCompute&[currentSchema=<schema>&][useSSL={true|false}&]table=<holo_table_name>/'.

StorageHandler

Yes

  • The name of a built-in storage handler.

  • The value must be com.aliyun.odps.jdbc.JdbcStorageHandler.

'<property_name>'='<property_value>'

No

  • property_name is the property name.

  • property_value is the property value.

  • To export data to Hologres, you must set the following parameters:

    • 'odps.properties.rolearn'='<ram_arn>': The Alibaba Cloud Resource Name (ARN) of the RAM role used for Security Token Service (STS) authentication. You can find the ARN in the Basic Information section of the role's details page within the RAM console.

    • 'mcfed.mapreduce.jdbc.driver.class'='org.postgresql.Driver': The driver for connecting to the Hologres database. The value must be org.postgresql.Driver.

    • 'odps.federation.jdbc.target.db.type'='holo': The type of the database to connect to. The value must be holo.

For more information, see Hologres external table.

Examples

External storage: OSS

Export data from the sale_detail table in a MaxCompute project to OSS.

  1. Sample data

    The sale_detail table 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   |
    +------------+-------------+-------------+------------+------------+
  2. Log on to the OSS console. Create the oss-unload-test/data_location/ directory in an OSS bucket. In this example, the region is oss-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.

  3. Log on to the MaxCompute client and run the UNLOAD command to export data from the sale_detail table to OSS.

    1. 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 to UNLOAD FROM sale_detail.

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

  1. Sample data

    The data_test table contains the following data:

    +------------+----------+
    | id         | name     |
    +------------+----------+
    | 3          | rgege    |
    | 4          | Gegegegr |
    +------------+----------+
  2. In Hologres, create a destination table named mc_2_holo in the test database.

    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.

  3. Log on to the MaxCompute client and run the UNLOAD command to export data from the data_test table 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&currentSchema=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'
    );
  4. 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

  • Specifies the data to export by using the table name or the table name and partition name.

  • The format of pt_spec is as follows:

    (partition_col1 = partition_col_value1, partition_col2 = partition_col_value2, ...)

  • This export method generates no query statement and incurs no cost.

external_location

Yes

Specifies the destination OSS path for the exported data. The format is 'oss://<oss_endpoint>/<object>'.

serde_class

No

The Serializer/Deserializer (SerDe) class. For usage details, see Create an OSS external table.

'<property_name>'='<property_value>'

No

  • property_name is the property name.

  • property_value is the property value.

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

  • tbproperty_name is the name of an extended property of the external table.

  • tbproperty_value is the value of an extended property for the external table.

For example, to export data in SNAPPY or LZO compression format, set the compression property as follows:

  • 'mcfed.parquet.compression'='SNAPPY'

  • 'mcfed.parquet.compression'='LZO'

When you export data in SNAPPY or LZO compression format, the exported files do not have the .snappy or .lzo extension.

Examples

  1. 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   |
    +------------+-------------+-------------+------------+------------+
  2. Log on to the OSS console. Create the oss-unload-test/data_location/ directory in an OSS bucket. In this example, the region is oss-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.

  3. Log on to the MaxCompute client and run the UNLOAD command to export data from the sale_detail table to OSS.

    1. Example 1: Export data from the sale_detail table 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');
    2. Example 2: Export data from the partition where sale_date='2013' and region='china' in the sale_detail table 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');
    3. Example 3: Export data from the sale_detail table 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.

  • Can contain only letters (a-z, A-Z), digits (0-9), and underscores (_).

  • Must be 1 to 10 characters in length.

A valid string, such as 'mc_'.

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.

  • True

  • 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 '_hangzhou'.

odps.external.data.output.explicit.extension

Adds a custom file extension to the names of the output files.

  • Can contain only letters (a-z, A-Z), digits (0-9), and underscores (_).

  • Must be 1 to 10 characters in length.

  • This parameter takes precedence over the odps.external.data.enable.extension parameter.

A valid string, such as 'jsonl'.

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 text file, add the prefix mc_ and the suffix _hangzhou, and automatically generate the .txt file 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.

  1. Use the UNLOAD command to export the data to OSS.

  2. Download the data from OSS to your local environment.

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