You need to enable JavaScript to run this app.
导航

S3 外表

最近更新时间2024.04.16 16:50:26

首次发布时间2024.04.16 16:50:26

ByteHouse 的 S3 引擎提供与 S3 兼容对象存储(包含火山引擎 ToS、Amazon S3 等)的生态系统的集成。

创建表
CREATE TABLE s3_engine_table (name String, value UInt32)
ENGINE = S3(path, [access_key_id, secret_access_key,] format, [compression])

引擎参数

path - 桶的 URL 和文件路径。支持以下通配符:*, ?, {abc,def} {N..M}
format - 文件的格式。
access_key_id, secret_access_key - 对象存储账户用户的长期凭证。
compression — 压缩类型(可选)。支持的值:none, gzip/gz, brotli/br, xz/LZMA, zstd/zst。默认情况下,它将通过文件扩展名自动检测压缩类型。

示例

设置 s3_engine_table 表:

CREATE TABLE s3_engine_table (name String, value UInt32) ENGINE=S3('https://storage.yandexcloud.net/my-test-bucket-768/test-data.csv.gz', 'CSV', 'gzip');

插入数据:

INSERT INTO s3_engine_table VALUES ('one', 1), ('two', 2), ('three', 3); 

查询数据:

SELECT * FROM s3_engine_table LIMIT 2;
 
┌─name─┬─value─┐
│ one │ 1 │
│ two │ 2 │
└──────┴───────┘

虚拟列

_path — 文件的路径。
_file — 文件的名称。

路径中的通配符

path 参数可以使用类 bash 的通配符(wildcard)指定多个文件。要被处理的文件必须存在并且匹配整个路径模式。文件的列表是在SELECT时(而不是在CREATE时刻)确定的。

  • * — 代替任何字符数量的任何字符,包括空字符串,但不包括 /
  • ? — 代替任何单一字符。
  • {some_string,another_string,yet_another_one} — 代替字符串 'some_string', 'another_string', 'yet_another_one' 中的任何一个。
  • {N..M} — 代替从N到M范围内的任何数字,包括两端。N和M可以有前导零,例如000..078。

带有{}的构造类似于远程表函数。

使用示例

假设我们在S3上有几个CSV格式的文件,其URI如下:

  • ‘https://storage.yandexcloud.net/my-test-bucket-768/some_prefix/some_file_1.csv’
  • ‘https://storage.yandexcloud.net/my-test-bucket-768/some_prefix/some_file_2.csv’
  • ‘https://storage.yandexcloud.net/my-test-bucket-768/some_prefix/some_file_3.csv’
  • ‘https://storage.yandexcloud.net/my-test-bucket-768/another_prefix/some_file_1.csv’
  • ‘https://storage.yandexcloud.net/my-test-bucket-768/another_prefix/some_file_2.csv’
  • ‘https://storage.yandexcloud.net/my-test-bucket-768/another_prefix/some_file_3.csv’

有几种方式可以创建一个包含所有六个文件的表:
第一种方式:

CREATE TABLE table_with_range (name String, value UInt32) ENGINE = S3('https://storage.yandexcloud.net/my-test-bucket-768/{some,another}_prefix/some_file_{1..3}', 'CSV');

另一种方式:

CREATE TABLE table_with_question_mark (name String, value UInt32) ENGINE = S3('https://storage.yandexcloud.net/my-test-bucket-768/{some,another}_prefix/some_file_?', 'CSV'); 

表包含两个目录中的所有文件(所有文件应满足查询中描述的格式和模式):

CREATE TABLE table_with_asterisk (name String, value UInt32) ENGINE = S3('https://storage.yandexcloud.net/my-test-bucket-768/{some,another}_prefix/*', 'CSV'); 

创建一个名为 file-000.csv, file-001.csv, … , file-999.csv 的文件表:

CREATE TABLE big_table (name String, value UInt32) ENGINE = S3('https://storage.yandexcloud.net/my-test-bucket-768/big_prefix/file-{000..999}.csv', 'CSV');