ClickHouse简介
ClickHouse最初是为Yandex.Metrica世界第二大Web分析平台而开发的。多年来一直作为该系统的核心组件被该系统持续使用着。目前为止,该系统在ClickHouse中有超过13万亿条记录,并且每天超过200多亿个事件被处理。它允许直接从原始数据中动态查询并生成报告。
ClickHouse的特征
ClickHouse 针对数据量和查询场景提供了不同的数据库和数据表引擎,此外它也可以使用多种多样的专用引擎或表函数(例如 HDFS,Kafka,S3 等)与许多外部系统进行通讯。
针对 S3 目前 ClickHouse 已经实现对象存储的两种用途。
l使用S3表引擎直接读取服从S3协议的外部数据
lClickHouse提供了使用S3作为MergeTree引擎的存储支持。ClickHouse存储卷允许从MergeTree表引擎中抽象物理磁盘,类型支持S3。
S3表引擎用法
为解决S3函数使用时一遍又一遍地指定存储桶 URL 和凭证,ClickHouse提供了S3表引擎。ClickHouse提供的S3函数允许对存储在 S3 中的数据执行即席查询,也提供了完善的函数使用手册。
S3表引擎语法:
CREATE TABLE s3_engine_table (name String, value UInt32)
ENGINE = S3(path, [aws_access_key_id, aws_secret_access_key,] format, [compression])
[SETTINGS ...]
参数说明:
-Path:包含文件路径的存储桶 URL。在只读模式下支持以下通配符:*、?、{abc,def} 和 {N,M} 其中N,M代表数字,'abc','def'代表字符串。
-Format:文件格式。
-aws_access_key_id、aws_secret_access_key:AWS 账户用户的长期凭证。您可以使用它们来验证您的请求。该参数是可选的。如果未指定凭据,则使用配置文件值。
-compression:压缩类型。支持的值:none, gzip/gz, brotli/br, xz/LZMA,zstd/zst.该参数是可选的。默认情况下,它将按文件扩展名自动检测压缩。
示例一:
1、创建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');
2、插入数据
INSERT INTO s3_engine_table VALUES ('one', 1), ('two', 2), ('three', 3);
3、查询数据
SELECT * FROM s3_engine_table LIMIT 2;
┌─name─┬─value─┐
│ one │ 1 │
│ two │ 2 │
└──────┴───────┘
示例二:
假设我们在 S3 上有几个 CSV 格式的文件,CSV的URL如下:
‘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');
如果文件列表中包含有从零开头的数字范围,请对每个数字分别使用带括号的结构,或者使用’?’。
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');
更多详细用法请参考:https://ClickHouse.com/docs/en/engines/table-engines/integrations/s3