了解如何在 S3 上按年和月进行分区写入
我想导出数据,并让 S3 存储桶 中的路径采用如下结构:
- 2022
- 1
- 2
- …
- 12
- 2021
- 1
- 2
- …
- 12
依此类推 …
答案
给定以下 ClickHouse 表:
CREATE TABLE sample_data (
`name` String,
`age` Int,
`time` DateTime
) ENGINE = MergeTree
ORDER BY
name添加 10000 条记录:
INSERT INTO
sample_data
SELECT
*
FROM
generateRandom(
'name String, age Int, time DateTime',
10,
10,
10
)
LIMIT
10000;运行此命令,在 S3 存储桶 my_bucket 中创建所需的结构 (注意:此示例会以 Parquet 格式写入文件) :
INSERT INTO
FUNCTION s3(
'https://s3-host:4321/my_bucket/{_partition_id}/file.parquet.gz',
's3-access-key',
's3-secret-access-key',
Parquet,
'name String, age Int, time DateTime'
) PARTITION BY concat(
formatDateTime(time, '%Y'),
'/',
formatDateTime(time, '%m')
)
SELECT
name,
age,
time
FROM
sample_data
Query id: 55adcf22-f6af-491e-b697