The CnchS3 engine developed by ByteHouse integrates object storage and supports managing data stored in S3-compatible object storage via ByteHouse. This article will introduce how to use S3 external tables in ByteHouse cloud data warehouse edition.
ByteHouse, using the CnchS3 engine, enables integration with S3-compatible object storage without needing to migrate data into ByteHouse, allowing queries, writes, and other operations on the original data in object storage. ByteHouse provides two usage methods: CnchS3 table engine (persistent external table) and CnchS3 table function, which you can flexibly choose according to business scenarios.
The comparison between CnchS3 table engine and CnchS3 table function is as follows:
Method | Use cases |
|---|---|
CnchS3 table engine |
|
CnchS3 table function |
|
The purpose of external tables is to provide processing capabilities for data other than ByteHouse internal tables. With a simple DDL statement, you can create an external table in ByteHouse, establishing an association between ByteHouse tables and external data sources.
Before starting, please ensure you have access privileges to the object storage bucket you use:
https://tos-{REGION}.volces.com/xxx copied from the BytePlus TOS console cannot be used directly. The domain name must be changed to the S3 endpoint domain name and modified to the format https://tos-s3-{REGION}.volces.com/xxx. For details, see BytePlus TOS supported regions and access domain names.Syntax
CREATE TABLE [db_name.]table_name ( column1 type1, column2 type2, ... ) ENGINE=CnchS3( 'http://....' or 'https://' or 's3://', '[FORMAT]', '[COMPRESSION]', 'access_key_id', 'secret_access_key' );
Parameter name | Required | Description |
|---|---|---|
| No | Specify the name of the database where the table is located. If you run |
| Yes | Specify the table name of the S3 external table. Example: |
| Yes | Specify the structure of the S3 external table, including column names and column types. Example: |
| Yes | The table engine is specified as CnchS3. |
| Yes | Specify the path to the object storage bucket corresponding to the S3 external table. The path description is as follows:
|
| Yes | Specify the file format of the S3 external table. Please fill in according to your file format. For formats currently supported by ByteHouse, see Related reference: File formats supported by the CnchS3 table engine. |
| Yes | Specify the compression format for the S3 external table. Currently, gzip, zstd, lz4, snappy, bzip2, xz, and brotli compression formats are supported. You can also set it to none. If not specified, the system will infer the compression format from the file suffix. |
| Yes | Specify the Access Key ID of the object storage service used by the S3 external table. You can refer to the official documentation of your object storage service to obtain the Access Key ID. If you use BytePlus TOS, refer to Access Key (key) management. |
| Yes | Specify the Secret Access Key of the object storage service used by the S3 external table. You can refer to the official documentation of your object storage service to obtain the Secret Access Key. If you use BytePlus TOS, refer to Access Key (key) management. |
Example
Create single file table
CREATE TABLE s3_engine_table(name String, value UInt32) ENGINE = CnchS3( 'https://bucket.endpoint/file_path/fileSample.csv', 'CSV', 'none', 'access_key_id', 'secret_access_key' );
Use wildcard character to create multi-file table
CREATE TABLE s3_engine_table_glob_uri(name String, value UInt32) ENGINE = CnchS3( 'https://bucket.endpoint/file_path/fileSample*.csv', 'CSV', 'none', 'access_key_id', 'secret_access_key' );
You can use the INSERT INTO statement to write data to S3 external tables. The usage is the same as writing to normal tables.
Examples are as follows:
INSERT INTO s3_engine_table VALUES ('one', 1), ('two', 2), ('three', 3);
You can use the SELECT statement to query data in S3 external tables. The usage is the same as querying normal tables. ByteHouse also supports pruning queries by the _path and _file columns. Examples are as follows:
Query data
SELECT * FROM s3_engine_table;
Use the _file column for pruning queries
SELECT * FROM s3_engine_table_glob_uri WHERE _file = 'fileSample.csv';
By default, the resources_assign_type **** value of external tables is server_push. In this mode, data files are sent to workers for querying. To query locally on the server, change resources_assign_type to server_local.
ALTER TABLE s3_engine_table MODIFY SETTING resources_assign_type='server_local'
You can update AK/SK by using the SELECT statement.
SELECT * FROM s3_engine_table SETTINGS s3_access_key_id = 'your_new_ak', s3_access_key_secret = 'your_new_sk'
Syntax
INSERT INTO FUNCTION CnchS3( 'http://....' or 'https://', 'column1 type1, column2 type2, ...', 'FORMAT', 'COMPRESSION', 'access_key_id', 'secret_access_key' ) VALUES (value1_1, value1_2, ...), (value2_1, value2_2, ...), (...);
Parameter name | Required | Description |
|---|---|---|
FUNCTION | Yes | The table function is specified as CnchS3. |
| Yes | Specify the path of the source table in object storage.
|
| Yes | Specify the target columns and column types for writing data. Example: |
| Yes | Specify the file format of the source table data. Please fill in according to your file format. For the file formats currently supported by ByteHouse, see Related reference: File formats supported by the CnchS3 table engine. |
| Yes | Specify the compression format for the S3 external table. Currently, gzip, zstd, lz4, snappy, bzip2, xz, and brotli compression formats are supported, and you can also set it to none. If not specified, the system will infer the format from the file suffix. |
| Yes | The Access Key ID of the object storage service. You can refer to the official documentation of your object storage service to obtain the Access Key ID. If you are using BytePlus TOS, refer to Access Key (key) management. |
| Yes | The Secret Access Key of the object storage service. You can refer to the official documentation of your object storage service to obtain the Secret Access Key. If you are using BytePlus TOS, refer to Access Key (key) management. |
| Yes | Specify the data to be written. |
Example
INSERT INTO FUNCTION CnchS3( 'http://bucket_endpoint/bucket/normal_write.csv', 'name String, id String', 'CSV', 'none', 'your_access_key_id', 'your_secret_access_key' ) VALUES ('one', '1'), ('two', '2'), ('three', '3');
The syntax for partition-level data writing is the same as normal data writing, but the object storage path must contain the '{_partition_id}' field. For other parameters, please refer to the parameter description above.
Examples are as follows:
INSERT INTO FUNCTION CnchS3( 'http://bucket_endpoint/bucket/{_partition_id}_partition_write.csv', 'name String, id String', 'CSV', 'none', 'your_access_key_id', 'your_secret_access_key' ) PARTITION BY name VALUES ('one', '1'), ('one', '2'), ('two', '1'), ('two', '2');
Use the CnchS3 function to directly query S3 external tables.
Syntax
SELECT * FROM CnchS3( ''http://....' or 'https://', 'column1 type1, column2 type2, ...', '[FORMAT]', '[COMPRESSION]', 'access_key_id', 'secret_access_key' ) [WHERE where_clause];
Parameter | Required | Configuration description |
|---|---|---|
| Yes | Specify the table function as CnchS3. |
| Yes | Specify the path of the source table in object storage.
|
| Yes | Specify the table structure of the S3 external table. It must be consistent with the source table, including column names and column types. Example: |
| No | Optional configuration. Specify the file format of the S3 external table. Please fill in according to your file format. For the file formats currently supported by CnchS3, see Related reference: File formats supported by the CnchS3 table engine. |
| No | Optional configuration. Currently, gzip, zstd, lz4, snappy, bzip2, xz, and brotli compression formats are supported, and you can also set it to none. If not specified, the system will infer the format from the file suffix. |
| No | Specify filter conditions. ByteHouse S3 external tables support pruning queries by |
Example
Query a single file
SELECT * FROM CnchS3( 'http://bucket_endpoint/bucket/fileSample.csv', 'city String, name String', 'CSV', 'none', 'your_access_key_id', 'your_secret_access_key' );
Use wildcard character to query multiple files and perform pruning queries
SELECT * FROM CnchS3( 'http://bucket_endpoint/bucket/fileSample.csv', 'city String, name String', 'CSV', 'none', 'your_access_key_id', 'your_secret_access_key' ) WHERE _path LIKE '%test%';
File format | Read | Write |
|---|---|---|
✅ | ✅ | |
✅ | ❌ | |
✅ | ✅ | |
✅ | ❌ | |
✅ | ✅ | |
✅ | ✅ | |
✅ | ❌ | |
❌ | ✅ | |
❌ | ✅ | |
✅ | ✅ | |
❌ | ✅ | |
✅ | ✅ | |
❌ | ✅ | |
❌ | ✅ | |
❌ | ✅ | |
❌ | ✅ | |
❌ | ✅ | |
❌ | ✅ | |
✅ | ✅ | |
✅ | ✅ | |
✅ | ✅ | |
✅ | ✅ | |
✅ | ✅ | |
❌ | ✅ | |
✅ | ❌ |