All Products
Search
Document Center

E-MapReduce:Import and export data between OSS and ClickHouse

Last Updated:Aug 20, 2026

You can read from and write to OSS on an EMR ClickHouse cluster using the S3 table engine or an S3 table function. This topic explains how to import data from OSS to a ClickHouse cluster and export data from the cluster to OSS.

Prerequisites

Import data from OSS to ClickHouse

Step 1: Create a business table

  1. Log on to the ClickHouse cluster by using SSH. For more information, see Log on to a cluster.

  2. Run the following command to start the ClickHouse client.

    clickhouse-client -h core-1-1 -m
    Note

    This example logs on to the core-1-1 node. If your cluster has multiple core nodes, you can log on to any of them.

  3. Run the following commands to create the product database and the orders business table in the product database.

    CREATE DATABASE IF NOT EXISTS product ON CLUSTER cluster_emr;
    CREATE TABLE IF NOT EXISTS product.orders ON CLUSTER cluster_emr
    (
        `uid` UInt32,
        `date` DateTime,
        `skuId` UInt32,
        `order_revenue` UInt32
    )
    Engine = ReplicatedMergeTree('/cluster_emr/product/orders/{shard}', '{replica}')
    PARTITION BY toYYYYMMDD(date)
    ORDER BY toYYYYMMDD(date);
    CREATE TABLE IF NOT EXISTS product.orders_all ON CLUSTER cluster_emr
    (
        `uid` UInt32,
        `date` DateTime,
        `skuId` UInt32,
        `order_revenue` UInt32
    )
    Engine = Distributed(cluster_emr, product, orders, rand());
    Note

    E-MapReduce automatically generates the {shard} and {replica} macros for the ClickHouse cluster, which you can use directly.

Step 2: Import data

Import with the S3 table engine

The ClickHouse S3 table engine reads data in a specific format from a specified OSS path. The syntax is as follows:

CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
  name1 [type1] [NULL|NOT NULL] [DEFAULT|MATERIALIZED|ALIAS expr1] [compression_codec] [TTL expr1],
  name2 [type2] [NULL|NOT NULL] [DEFAULT|MATERIALIZED|ALIAS expr2] [compression_codec] [TTL expr2],
  ...
)
ENGINE = S3(path, [access_key_id, access_key_secret,] format, [compression]);

Parameter

Description

db

The database name.

table_name

The table name.

name1/name2

The column name.

type1/type2

The data type of the column.

path

The OSS path.

To find the internal endpoint for accessing OSS from your ClickHouse cluster, see Access OSS resources from an ECS instance by using an internal endpoint.

The path parameter supports two styles: virtual-hosted style and path-style. We recommend that you use the virtual-hosted style.

The path parameter supports the following wildcards:

  • *: Represents any number of characters except for the forward slash (/), including an empty string.

  • ?: Represents a single character.

  • {str1,str2,...,strn} represents str1/str2/.../strn, meaning any one of the strings.

  • {N..M}: Represents any number in the range from N to M. N and M can include leading zeros, such as {001..099}.

    Important

    Wildcards determine which files are used during a SELECT operation, not during table creation. Therefore, do not use wildcards when writing data to OSS.

AccessKey ID

Your Alibaba Cloud AccessKey ID.

AccessKey Secret

Your Alibaba Cloud AccessKey Secret.

format

The format of the object (file) specified by the path parameter. Examples include CSV and XML. For more information, see Formats for Input and Output Data.

compression

The compression type.

This parameter is optional. By default, ClickHouse automatically detects the compression type from the file extension.

Set the compression type based on the EMR version of your cluster:

  • EMR-3.x versions support none, gzip/gz, brotli/b, deflate, or auto.

  • EMR 5.x series: supports none, gzip/gz, brotli/b, lzma(xz), auto, or zstd/zst.

  1. Create a table to read data from OSS.

    1. Download the sample data file orders.csv and upload it to the root directory of an OSS bucket named test.

    2. Run the following command to create an OSS table that uses the S3 table engine.

      CREATE DATABASE IF NOT EXISTS oss ON CLUSTER cluster_emr;
      CREATE TABLE oss.orders_oss
      (
        uid UInt32,
        date DateTime,
        skuId UInt32,
        order_revenue UInt32
      ) ENGINE = S3('http://test.oss-cn-beijing-internal.aliyuncs.com/orders.csv', '<access_key_id>', '<access_key_secret>', 'CSV');
      Note

      The example data path http://test.oss-cn-beijing-internal.aliyuncs.com/orders.csv represents the orders.csv file in a bucket named test in the cn-beijing region.

  2. Run the following command to import data into the product.orders_all table.

    INSERT INTO product.orders_all
    SELECT
      uid,
      date,
      skuId,
      order_revenue
    FROM
      oss.orders_oss;
  3. Run the following commands to view the data in the tables and verify data consistency:

    • View the data in the orders_all table.

      SELECT count(1) FROM product.orders_all;
    • View the data in the orders_oss table.

      SELECT count(1) FROM oss.orders_oss;

Import with an S3 table function

The ClickHouse S3 table function reads data from a specified OSS path and returns a table with a defined structure. The syntax is as follows:

s3(path, [access_key_id, access_key_secret,] format, structure, [compression])

Parameter

Description

path

The OSS path.

To find the internal endpoint for accessing OSS from your ClickHouse cluster, see Access OSS resources from an ECS instance by using an internal endpoint.

The path parameter supports two styles: virtual hosted style and path style. We recommend that you use the virtual hosted style.

path supports the following wildcards:

  • *: Represents any number of characters except for the forward slash (/), including an empty string.

  • ?: Represents a single character.

  • {str1,str2,...,strn} represents any one of the strings in str1/str2/.../strn.

  • {N..M}: Represents any number in the range from N to M. N and M can include leading zeros, such as {001..099}.

    Important

    Wildcards determine which files are used during a SELECT operation, not during table creation. Therefore, do not use wildcards when writing data to OSS.

AccessKey ID

Your Alibaba Cloud AccessKey ID.

AccessKey Secret

Your Alibaba Cloud AccessKey Secret.

format

The format of the object (file) that the path parameter specifies, such as CSV and XML. For more information, see Formats for Input and Output Data.

structure

The table structure, which defines the column names and data types. Example: column1 UInt32, column2 String.

compression

The compression type.

This parameter is optional. By default, ClickHouse automatically detects the compression type from the file extension.

Set the compression type based on the EMR version of your cluster:

  • EMR-3.x versions support none, gzip/gz, brotli/b, deflate, or auto.

  • EMR 5.x versions: Support none, gzip/gz, brotli/b, lzma(xz), auto, or zstd/zst.

  1. Use the S3 table function to import data into the ClickHouse cluster.

    INSERT INTO product.orders_all
    SELECT
      uid,
      date,
      skuId,
      order_revenue
    FROM
      s3('http://test.oss-cn-beijing-internal.aliyuncs.com/orders.csv',
          '<your-access-key>',
          '<your-access-secret>',
          'CSV',
          'uid UInt32, date DateTime, skuId UInt32, order_revenue UInt32');
  2. Run the following commands to view the data in the tables and verify data consistency:

    • View the data in the orders_all table.

      SELECT count(1) FROM product.orders_all;
    • View the data in the orders_oss table.

      SELECT count(1) FROM oss.orders_oss;

Export data from ClickHouse to OSS

Step 1: Create a business table

The business table for exporting data uses the same schema as the one for importing. To create the table, see Step 1: Create a business table.

Step 2: Prepare data

  1. To prepare data for the export, run the following command to insert it into the product.orders_all business table.

    INSERT INTO product.orders_all VALUES 
      (60333391,'2021-08-04 11:26:01',49358700,89) 
      (38826285,'2021-08-03 10:47:29',25166907,27) 
      (10793515,'2021-07-31 02:10:31',95584454,68) 
      (70246093,'2021-08-01 00:00:08',82355887,97) 
      (70149691,'2021-08-02 12:35:45',68748652,1)  
      (87307646,'2021-08-03 19:45:23',16898681,71) 
      (61694574,'2021-08-04 23:23:32',79494853,35) 
      (61337789,'2021-08-02 07:10:42',23792355,55) 
      (66879038,'2021-08-01 16:13:19',95820038,89);
  2. (Optional) Configure the export mode. For EMR-5.8.0 and later or EMR-3.45.0 and later, you can configure the write mode to handle cases where a file already exists at the specified path.

    Incremental export

    If the file already exists, ClickHouse creates a new file in the same directory and writes the data to it.

    set s3_create_new_file_on_insert=1

    Overwrite export

    If the file already exists, ClickHouse overwrites the existing data. Use this setting with caution.

    set s3_truncate_on_insert=1

Step 3: Export data

Export with the S3 table engine

  1. Run the following command to create an S3 table.

    CREATE TABLE oss.orders_oss
    (
      uid UInt32,
      date DateTime,
      skuId UInt32,
      order_revenue UInt32
    ) ENGINE = S3('http://test.oss-cn-beijing-internal.aliyuncs.com/orders.csv', '<access_key_id>', '<access_key_secret>', 'CSV');
  2. Run the following command to write data to the table.

    -- Assume the business table is product.orders_all
    INSERT INTO oss.orders_oss
    SELECT
      uid,
      date,
      skuId,
      order_revenue
    FROM
      product.orders_all;
    Note

    When ClickHouse exports data, it creates a file at the specified path and writes data to it. By default, the export fails if the file already exists. On EMR-5.8.0, EMR-3.45.0, and later, you can configure parameters to change this behavior.

  3. View the data in the OSS console.

Export with an S3 table function

  1. Run the following command to export data.

    INSERT INTO FUNCTION
    s3('http://test.oss-cn-beijing-internal.aliyuncs.com/orders.csv',
          '<your-access-key>',
          '<your-access-secret>',
          'CSV',
          'uid UInt32, date DateTime, skuId UInt32, order_revenue UInt32')
    SELECT
      uid,
      date,
      skuId,
      order_revenue
    FROM
      product.orders_all;
    Note

    When ClickHouse exports data, it creates a file at the specified path and writes data to it. By default, the export fails if the file already exists. On EMR-5.8.0, EMR-3.45.0, and later, you can configure parameters to change this behavior.

  2. View the data in the OSS console.

OSS-related configurations

Profile

  1. Supported profiles

    When using multipart upload to OSS, the s3_min_upload_part_size parameter sets the minimum size for each part, with a default of 512 MB. The value must be a UInt64 integer.

  2. Configuration methods

    • For a single SQL statement, configure the parameter as follows:

      INSERT INTO OSS_TABLE
      SELECT
        ...
      FROM
        ...
      SETTINGS
        s3_min_upload_part_size=1073741824;
    • For a single session, configure the parameter as follows:

      SET s3_min_upload_part_size=1073741824;
      INSERT INTO OSS_TABLE
      SELECT
        ...
      FROM
        ...
      ;
    • For a specific table, configure the parameter as follows:

      CREATE TABLE OSS_TABLE
      (
        ...
      ) ENGINE = s3(...)
      SETTINGS
        s3_min_upload_part_size=1073741824;
    • For a specific user, configure the parameter as follows:

      On the Configure page of the ClickHouse service in the EMR console, click the server-users tab. Add the users.<YourUserName>.s3_min_upload_part_size parameter and set its value to 1073741824.

Configuration

ClickHouse on EMR supports the following OSS configuration parameters. For example:

<s3>
    <endpoint-name>
        <endpoint>https://oss-cn-beijing-internal.aliyuncs.com/bucket</endpoint>
        <access_key_id>ACCESS_KEY_ID</access_key_id>
        <secret_access_key>ACCESS_KEY_SECRET</secret_access_key>
    </endpoint-name>
</s3>

The following table describes the parameters.

Parameter

Description

endpoint-name

The name of the endpoint.

endpoint

The domain name used to access OSS. For more information, see OSS domain names.

access_key_id

Your Alibaba Cloud AccessKey ID.

secret_access_key

Your Alibaba Cloud AccessKey Secret.

On the Configure page of the ClickHouse service in the EMR console, you can also click the server-config tab to add custom configurations in the following two ways.

Method

Actions

Method 1

Add the parameters oss.<endpoint-name>.endpoint, oss.<endpoint-name>.access_key_id, and oss.<endpoint-name>.secret_access_key and their corresponding values.

Note

Replace <endpoint-name> with the name of the endpoint.

Method 2

Add a parameter named oss and set its value to the following configuration.

<endpoint-name>
  <endpoint>https://oss-cn-beijing-internal.aliyuncs.com/bucket</endpoint>
  <access_key_id>ACCESS_KEY_ID</access_key_id>
  <secret_access_key>ACCESS_KEY_SECRET</secret_access_key>
</endpoint-name>
Note

Replace the placeholder values with your own.

After completing the configuration, you can create an OSS table or use an S3 table function with the following simplified syntax:

  • OSS table

    CREATE TABLE OSS_TABLE
    (
      column1 UInt32,
      column2 String
      ...
    )
    ENGINE = S3(path, format, [compression]);
  • S3 table function

    s3(path, format, structure, [compression]);