问题描述
我目前正在开始为我们的内部分析系统使用 clickhouse,但似乎没有自动方法来配置数据保留策略。我唯一看到的是 ALTER ... MOVE PARTITION
(https://clickhouse.tech/docs/en/sql-reference/statements/alter/partition/#alter_move-partition),但看起来这个过程必须在我们的应用层手动/实现。
我的目标是出于存档和价格原因,将超过 3 个月的数据直接移动到 S3 集群,同时仍然能够对其进行查询。
是否有任何本地方法可以直接在带有存储策略的 Clickhouse 中执行此操作?
提前致谢。
解决方法
这个答案是基于@Denny Crane 的评论:https://altinity.com/blog/clickhouse-and-s3-compatible-object-storage,我确实在没有足够解释的地方发表了评论,并在链接失效时保留它。
-
- 将您的 S3 磁盘添加到新的配置文件(假设
/etc/clickhouse-server/config.d/storage.xml
:
- 将您的 S3 磁盘添加到新的配置文件(假设
<yandex>
<storage_configuration>
<disks>
<!-- This tag is the name of your S3-emulated disk,used for the rest of this tutorial -->
<your_s3>
<type>s3</type>
<!-- Set this to the endpoint of your S3-compatible provider -->
<endpoint>https://nyc3.digitaloceanspaces.com</endpoint>
<!-- Set this to your access key ID provided by your provider -->
<access_key_id>*****</access_key_id>
<!-- Set this to your access key Secret provided by your provider -->
<secret_access_key>*****</secret_access_key>
</your_s3>
</disks>
<!-- Don't leave this file yet! We still have things to do there -->
...
</storage_configuration>
</yandex>
-
- 为您的数据存储添加存储策略:
<!-- Put this after the three dots in the snippet above -->
<policies>
<shared>
<volumes>
<default>
<!-- Default is the disk that is present in the default question -->
<disk>default</disk>
</default>
<your_s3>
<disk>your_s3</disk>
</your_s3>
</volumes>
</shared>
</policies>
完成后,您可以使用以下插入语句创建表:
CREATE TABLE visits (...)
ENGINE = MergeTree
TTL toStartOfYear(time) + interval 3 year to volume 'your_s3'
SETTINGS storage_policy = 'shared';
其中 shared
是您的策略名称,而 your_s3
是您在该策略中的磁盘名称。