All Products
Search
Document Center

ApsaraDB for ClickHouse:Access OSS data with an external table

Last Updated:Jul 17, 2026

You can use ApsaraDB for ClickHouse to query data in Alibaba Cloud Object Storage Service (OSS) and export ClickHouse data to OSS.

Prerequisites

Prerequisites

  1. Create an OSS bucket. For more information, see Create buckets in the console.

    Important

    The OSS bucket must be in the same region as your ApsaraDB for ClickHouse cluster.

    This example uses a bucket named ck-test-oss.

  2. Upload an object to OSS.

    This example uses the test.csv object. Upload this object to OSS. For more information, see Upload objects.

    If you use your own object, ensure its format is supported by ApsaraDB for ClickHouse. ApsaraDB for ClickHouse supports all formats except Protobuf and CapnProto. For more information, see File formats supported by ClickHouse.

Query OSS data

Method 1: OSS external table

Enterprise or Community-compatible (22.8 or later)

  1. Log on to the database.

    1. Log on to the ApsaraDB for ClickHouse console.

    2. On the Clusters page, click the Clusters of Community-compatible Edition or Enterprise Edition Clusters tab, and then click the ID of the target cluster.

    3. On the Cluster Information page, click Log On to Database in the upper-right corner.

    4. In the Connect to Instance dialog box, enter the database account and password, and then click Log On.

  2. Create an OSS external table.

    An OSS external table in ApsaraDB for ClickHouse allows you to query objects stored in OSS directly without importing data into the local storage, enabling storage-compute separation.

    The syntax is as follows:

    CREATE TABLE <table_name> [ON cluster default]
    (
    'col_name1' col_type1,
    'col_name2' col_type2,
    ...
    )
    ENGINE = OSS('https://<bucket-name>.<oss-endpoint>/<file-name>', '<access-key-id>', '<access-key-secret>', '<file-format-name>');

    The following table describes the parameters.

    Parameter

    Description

    Example

    table_name

    The name of the table.

    oss_test_tb

    col_name1,col_name2

    The names of the columns.

    user_name

    col_type1,col_type2

    The data types of the columns.

    Important

    The schema of the OSS external table must be consistent with the data in OSS.

    String

    bucket-name

    The name of the OSS bucket.

    ck-test-oss

    oss-endpoint

    The internal endpoint for accessing the OSS bucket from an ECS instance over a VPC. For more information, see Regions and endpoints.

    Important

    Ensure that the OSS bucket and your ApsaraDB for ClickHouse cluster are in the same region.

    oss-cn-hangzhou-internal.aliyuncs.com

    file-name

    The name of the object in the OSS bucket.

    test.csv

    access-key-id

    The AccessKey ID of the RAM user for accessing OSS data.

    yourAccessKeyID

    access-key-secret

    The AccessKey secret of the RAM user for accessing OSS data.

    You can use an existing AccessKey or create a new one. For more information, see Create an AccessKey.

    Important

    To reduce the risk of an AccessKey leak, the AccessKey secret is displayed only when you create it and cannot be retrieved later. Make sure that you store the AccessKey secret in a secure manner.

    yourAccessKeySecret

    file-format-name

    The format of the object.

    Important

    The value must be a case-sensitive file format supported by ClickHouse. For more information, see file formats supported by ClickHouse.

    CSV

    Example:

    CREATE TABLE oss_test_tb ON cluster default
    (
    id UInt8,
    user_name String,
    age UInt16,
    city String,
    access_url String
    )
    ENGINE = OSS('http://ck-test-oss.oss-cn-hangzhou-internal.aliyuncs.com/test.csv', 'LTAI****************', 'yourAccessKeySecret','CSV')
  3. Query OSS data.

    SELECT * FROM oss_test_tb;

    The following result is returned:

    +--------------+---------------------+---------------+----------------+----------------------+
    | id           | user_name           | age           | city           | access_url           |
    +--------------+---------------------+---------------+----------------+----------------------+
    | 1           | tick                | 32            | shanghai       | http://example.com   |
    +--------------+---------------------+---------------+----------------+----------------------+
    Returned rows: [1], Time elapsed: [183ms]

Community-compatible (21.8 or earlier)

  1. Log on to the database.

    1. Log on to the ApsaraDB for ClickHouse console.

    2. On the Clusters page, click the Clusters of Community-compatible Edition tab, and then click the ID of the target cluster.

    3. On the Cluster Information page, click Log On to Database in the upper-right corner.

    4. In the Connect to Instance dialog box, enter the database account and password, and then click Log On.

  2. Create an OSS external table.

    The syntax is as follows:

    CREATE TABLE <table_name> [ON cluster default]
    (
    'col_name1' col_type1,
    'col_name2' col_type2,
    ...
    )
    ENGINE = OSS('<oss-endpoint>', '<access-key-id>', '<access-key-secret>', '<oss-file-path>', '<file-format-name>');

    The following table describes the parameters.

    Parameter

    Description

    Example

    table_name

    The name of the table.

    oss_test_tb

    col_name1,col_name2

    The names of the columns.

    user_name

    col_type1,col_type2

    The data types of the columns.

    Important

    The schema of the OSS external table must be consistent with the data in OSS.

    String

    oss-endpoint

    The internal endpoint for accessing the OSS bucket from an ECS instance over a VPC. For more information, see Regions and endpoints.

    Important

    Ensure that the OSS bucket and your ApsaraDB for ClickHouse cluster are in the same region.

    oss-cn-hangzhou-internal.aliyuncs.com

    access-key-id

    The AccessKey ID of the RAM user for accessing OSS data.

    yourAccessKeyID

    access-key-secret

    The AccessKey secret of the RAM user for accessing OSS data.

    You can use an existing AccessKey or create a new one. For more information, see Create an AccessKey.

    Important

    To reduce the risk of an AccessKey leak, the AccessKey secret is displayed only when you create it and cannot be retrieved later. Make sure that you store the AccessKey secret in a secure manner.

    yourAccessKeySecret

    oss-file-path

    The storage path of the object, which is typically in the oss://<bucket-name>/<path-to-file> format.

    Note

    The oss-file-path parameter supports fuzzy matching with wildcards. For more information, see Use wildcards for fuzzy matching of storage paths in OSS.

    oss://ck-test-oss/test.csv

    file-format-name

    The format of the object.

    Important

    The value must be a case-sensitive file format supported by ClickHouse. For more information, see file formats supported by ClickHouse.

    CSV

    Example:

    CREATE TABLE oss_test_tb ON cluster default
    (
    id UInt8,
    user_name String,
    age UInt16,
    city String,
    access_url String
    )
    ENGINE = OSS('oss-cn-hangzhou-internal.aliyuncs.com', 'LTAI****************', 'yourAccessKeySecret', 'oss://ck-test-oss/test.csv', 'CSV');
  3. Query OSS data.

    SELECT * FROM oss_test_tb;

    The following result is returned:

    +--------------+---------------------+---------------+----------------+----------------------+
    | id           | user_name           | age           | city           | access_url           |
    +--------------+---------------------+---------------+----------------+----------------------+
    | 1           | tick                | 32            | shanghai       | http://example.com   |
    +--------------+---------------------+---------------+----------------+----------------------+
    Returned rows: [1], Time elapsed: [183ms]

Method 2: Table function

Enterprise or Community-compatible (22.8 or later)

  1. Log on to the database.

    1. Log on to the ApsaraDB for ClickHouse console.

    2. On the Clusters page, click the Clusters of Community-compatible Edition or Enterprise Edition Clusters tab, and then click the ID of the target cluster.

    3. On the Cluster Information page, click Log On to Database in the upper-right corner.

    4. In the Connect to Instance dialog box, enter the database account and password, and then click Log On.

  2. Use a table function to query data.

    The syntax is as follows:

    SELECT * FROM oss('https://<bucket-name>.<oss-endpoint>/<file-name>','<access-key-id>', '<access-key-secret>', '<file-format-name>', '<col_name> <col_type>(,...)');

    The parameters are the same as those for creating an OSS external table. For details, see Parameter descriptions.

    Example:

    SELECT * FROM oss('http://testBucketName.oss-cn-shanghai-internal.aliyuncs.com/test.csv', 'LTAI****************', 'yourAccessKeySecret', 'CSV', 'id UInt8, user_name String, age UInt16, city String, access_url String')

    The following result is returned:

    +--------------+---------------------+---------------+----------------+----------------------+
    | id           | user_name           | age           | city           | access_url           |
    +--------------+---------------------+---------------+----------------+----------------------+
    | 1           | tick                | 32            | shanghai       | http://example.com   |
    +--------------+---------------------+---------------+----------------+----------------------+
    Returned rows: [1], Time elapsed: [183ms]

Community-compatible (21.8 or earlier)

  1. Log on to the database.

    1. Log on to the ApsaraDB for ClickHouse console.

    2. On the Clusters page, click the Clusters of Community-compatible Edition tab, and then click the ID of the target cluster.

    3. On the Cluster Information page, click Log On to Database in the upper-right corner.

    4. In the Connect to Instance dialog box, enter the database account and password, and then click Log On.

  2. Use a table function to query data.

    The syntax is as follows:

    SELECT * FROM oss('<oss-endpoint>', '<access-key-id>', '<access-key-secret>', '<oss-file-path>', '<file-format-name>', '<col_name> <col_type>(,...)');

    The parameters are the same as those for creating an OSS external table. For details, see Parameter descriptions.

    Example:

    SELECT * FROM oss('oss-cn-hangzhou-internal.aliyuncs.com', 'LTAI****************', 'yourAccessKeySecret', 'oss://ck-test-oss/test.csv', 'CSV', 'id UInt8, user_name String, age UInt16, city String, access_url String');

    The following result is returned:

    +--------------+---------------------+---------------+----------------+----------------------+
    | id           | user_name           | age           | city           | access_url           |
    +--------------+---------------------+---------------+----------------+----------------------+
    | 1           | tick                | 32            | shanghai       | http://example.com   |
    +--------------+---------------------+---------------+----------------+----------------------+
    Returned rows: [1], Time elapsed: [183ms]

Export ClickHouse data or write data to OSS

Usage notes

Enterprise Edition or Community-compatible Edition 22.8 or later

When you export or write data to OSS, you must set s3_truncate_on_insert=1 or s3_create_new_file_on_insert=1. If neither parameter is set or both are set to 0, the operation fails when a file already exists in the destination path.

  • s3_truncate_on_insert=1: ApsaraDB for ClickHouse checks if a file exists in the destination path.

    • If a file exists, ClickHouse overwrites the existing file with new data.

    • This method is suitable for full overwrite exports or writes. However, proceed with caution because this operation deletes the original data.

  • s3_create_new_file_on_insert=1: ClickHouse checks if a file exists in the destination path.

    • If a file exists, ClickHouse creates a new file in the directory and writes data to it. The new file is named using the following pattern: target file name + a sequence number (starting from 0 and incrementing by 1) + file format name.

      For example, if the target file is test.csv, the first operation creates test0.csv and the second operation creates test1.csv.

    • This method is suitable for incremental exports or writes and avoids overwriting existing data.

Community-compatible Edition 21.8 or earlier

When exporting data or writing data, only a single OSS file is used. The new data overwrites the content of the original file. Proceed with caution.

Step 1: Log on to the database

  1. Log on to the ApsaraDB for ClickHouse console.

  2. On the Clusters page, click the Clusters of Community-compatible Edition or Enterprise Edition Clusters tab, and then click the ID of the target cluster.

  3. In the upper-right corner of the Cluster Information page, click Log On to Database.

  4. In the Connect to Instance dialog box, enter the database account and password, and click Log On.

Step 2: (Optional) Prepare data for export

If you need to prepare sample data to export from ApsaraDB for ClickHouse to OSS, follow these steps.

If you already have data to export, skip this step.

  1. Create a table based on your cluster edition.

    This example creates a table in a single-replica Community-compatible Edition cluster. If your cluster is a double-replica cluster that runs Enterprise Edition or Community-compatible Edition, see Create a table.

     CREATE TABLE test_tb_local ON cluster default
    (
    id UInt8,
    user_name String,
    age UInt16,
    city String,
    access_url String
    )
    ENGINE = MergeTree()
    ORDER BY id;
  2. Insert sample data.

    INSERT INTO test_tb_local VALUES (13, 'tick', 25, 'shanghai', 'http://example.com');
  3. (Optional) View the data.

    Run the following statement to verify that the data was inserted.

    SELECT * FROM test_tb_local;

Step 3: Create an OSS external table

Enterprise Edition or Community-compatible Edition 22.8 or later

The syntax is as follows:

CREATE TABLE <table_name> [ON cluster default]
(
'col_name1' col_type1,
'col_name2' col_type2,
...
)
ENGINE = OSS('https://<bucket-name>.<oss-endpoint>/<file-name>', '<access-key-id>', '<access-key-secret>', '<file-format-name>');

The following table describes the parameters.

Parameter

Description

Example

table_name

The name of the table.

oss_test_tb

col_name1,col_name2

The names of the columns.

user_name

col_type1,col_type2

The data types of the columns.

Important

The schema of the OSS external table must be consistent with the data in OSS.

String

bucket-name

The name of the OSS bucket.

ck-test-oss

oss-endpoint

The internal endpoint for accessing the OSS bucket from an ECS instance over a VPC. For more information, see Regions and endpoints.

Important

Ensure that the OSS bucket and your ApsaraDB for ClickHouse cluster are in the same region.

oss-cn-hangzhou-internal.aliyuncs.com

file-name

The name of the object in the OSS bucket.

test.csv

access-key-id

The AccessKey ID of the RAM user for accessing OSS data.

yourAccessKeyID

access-key-secret

The AccessKey secret of the RAM user for accessing OSS data.

You can use an existing AccessKey or create a new one. For more information, see Create an AccessKey.

Important

To reduce the risk of an AccessKey leak, the AccessKey secret is displayed only when you create it and cannot be retrieved later. Make sure that you store the AccessKey secret in a secure manner.

yourAccessKeySecret

file-format-name

The format of the object.

Important

The value must be a case-sensitive file format supported by ClickHouse. For more information, see file formats supported by ClickHouse.

CSV

Example:

CREATE TABLE oss_test_tb ON cluster default
(
id UInt8,
user_name String,
age UInt16,
city String,
access_url String
)
ENGINE = OSS('http://ck-test-oss.oss-cn-hangzhou-internal.aliyuncs.com/test.csv', 'LTAI****************', 'yourAccessKeySecret','CSV')

Community-compatible Edition 21.8 or earlier

The syntax is as follows:

CREATE TABLE <table_name> [ON cluster default]
(
'col_name1' col_type1,
'col_name2' col_type2,
...
)
ENGINE = OSS('<oss-endpoint>', '<access-key-id>', '<access-key-secret>', '<oss-file-path>', '<file-format-name>');

The following table describes the parameters.

Parameter

Description

Example

table_name

The name of the table.

oss_test_tb

col_name1,col_name2

The names of the columns.

user_name

col_type1,col_type2

The data types of the columns.

Important

The schema of the OSS external table must be consistent with the data in OSS.

String

oss-endpoint

The internal endpoint for accessing the OSS bucket from an ECS instance over a VPC. For more information, see Regions and endpoints.

Important

Ensure that the OSS bucket and your ApsaraDB for ClickHouse cluster are in the same region.

oss-cn-hangzhou-internal.aliyuncs.com

access-key-id

The AccessKey ID of the RAM user for accessing OSS data.

yourAccessKeyID

access-key-secret

The AccessKey secret of the RAM user for accessing OSS data.

You can use an existing AccessKey or create a new one. For more information, see Create an AccessKey.

Important

To reduce the risk of an AccessKey leak, the AccessKey secret is displayed only when you create it and cannot be retrieved later. Make sure that you store the AccessKey secret in a secure manner.

yourAccessKeySecret

oss-file-path

The storage path of the object, which is typically in the oss://<bucket-name>/<path-to-file> format.

Note

The oss-file-path parameter supports fuzzy matching with wildcards. For more information, see Use wildcards for fuzzy matching of storage paths in OSS.

oss://ck-test-oss/test.csv

file-format-name

The format of the object.

Important

The value must be a case-sensitive file format supported by ClickHouse. For more information, see file formats supported by ClickHouse.

CSV

Example:

CREATE TABLE oss_test_tb ON cluster default
(
id UInt8,
user_name String,
age UInt16,
city String,
access_url String
)
ENGINE = OSS('oss-cn-hangzhou-internal.aliyuncs.com', 'LTAI****************', 'yourAccessKeySecret', 'oss://ck-test-oss/test.csv', 'CSV');

Step 4: Export or write data to OSS

Use an INSERT INTO statement on the OSS external table to export or write data to OSS. For syntax details, see INSERT INTO.

Export ClickHouse data

Enterprise Edition or Community-compatible Edition 22.8 or later

  • Full overwrite export (export data to OSS and overwrite existing data).

    INSERT INTO oss_test_tb SETTINGS s3_truncate_on_insert = 1 SELECT * FROM test_tb_local;
  • Incremental export (export data to OSS without overwriting existing data).

    INSERT INTO oss_test_tb SETTINGS s3_create_new_file_on_insert = 1 SELECT * FROM test_tb_local;

    This operation creates a new file in the OSS bucket. The new file is named using the following pattern: target file name + a sequence number (starting from 0 and incrementing by 1) + file format name.

    For example, if the target file name that you specify when you create the OSS external table is test.csv, the new file name is test0.csv. If you run the operation again, the new file name is test1.csv. You can use the OSS console to view the new file.

Community-compatible Edition 21.8 or earlier

Important

Exporting ApsaraDB for ClickHouse data to OSS overwrites existing data. Proceed with caution.

INSERT INTO oss_test_tb  SELECT * FROM test_tb_local;

Write data

Enterprise Edition or Community-compatible Edition 22.8 or later

  • Write new data to OSS and overwrite existing data.

    INSERT INTO oss_test_tb SETTINGS s3_truncate_on_insert = 1 VALUES (14, 'tick', 25, 'shanghai', 'http://example.com');
  • Write new data to OSS without overwriting existing data.

    INSERT INTO oss_test_tb SETTINGS s3_create_new_file_on_insert = 1 VALUES (14, 'tick', 25, 'shanghai', 'http://example.com');

    This operation creates a new file in the OSS bucket. The new file is named using the following pattern: target file name + a sequence number (starting from 0 and incrementing by 1) + file format name.

    For example, if the target file name that you specify when you create the OSS external table is test.csv, the new file name is test0.csv. If you run the operation again, the new file name is test1.csv. You can use the OSS console to view the new file.

Community-compatible Edition 21.8 or earlier

Important

Writing data from ApsaraDB for ClickHouse to OSS overwrites existing data. Proceed with caution.

INSERT INTO oss_test_tb SETTINGS s3_truncate_on_insert = 1 VALUES (14, 'tick', 25, 'shanghai', 'http://example.com');

More operations: Import OSS data to ClickHouse

If you use OSS as intermediate storage for data migration, you must also import the data from OSS to the destination cluster. For more information, see Import data from OSS.