参考了
虾皮的这篇
冷热分离的优势:
1、业务查询通常查近期数据(7天-14天),早期数据查询概率较低
2、降低成本
这个冷热分离实操起来很简单了。
1、阿里云后台申请一个bucket(aws的s3等也支持)
2、安装juicefs工具,并初始化
下载 juicefs-0.17.1-linux-amd64.tar.gz
tar -zxf juicefs-0.17.1-linux-amd64.tar.gz
sudo install juicefs /usr/local/bin
juicefs format --storage oss \
--bucket https://arch-ck.oss-cn-nanjing-internal.aliyuncs.com \
--access-key LxxxxxMaxxxxHxxxkm \
--secret-key xxxxxxxxxxxxxxx \
redis://192.168.2.11:6379/1 \
clickhouse-warm clickhouse-warm
# 这里依赖了redis,做metadata的存储
# 如果去redis db1查看,可以看到这些key如下:
[0]> select 1
[1]> keys *
totalInodes
setting
nextsession
i1
sessions
usedSpace
sessionInfos
3、在机器上挂载oss
Plain Text复制代码
mkdir -pv /ck_jfs
juicefs mount -d redis://192.168.2.11:6379/1 /ck_jfs
df
..... 省略 .....
JuiceFS:clickhouse-warm 1099511627776 7562176 1099504065600 1% /ck_jfs
# 卸载命令
# juicefs umount /ck_jfs
# 如果卸载失败可以试试 fuser -v /ck_jfs 查看原因
4、修改clikchouse配置
编辑 /data/clickhouse/config.xml 增加 storage_configuration 片段(开启多磁盘的支持)如下:
<tmp_path>/data/clickhouse/tmp/</tmp_path>
<storage_configuration>
<disks>
<jfs>
<path>/ck_jfs/</path>
</jfs>
</disks>
<policies>
<default>
<volumes>
<hot>
<disk>default</disk>
<max_data_part_size_bytes>10737418240</max_data_part_size_bytes>
</hot>
<cold>
<disk>jfs</disk>
</cold>
</volumes>
<move_factor>0.1</move_factor>
</default>
</policies>
</storage_configuration>
<user_files_path>/data/clickhouse/user_files/</user_files_path>
重启clickhouse进程
supervisorctl status
supervisorctl restart clickhouse-9200
supervisorctl status
重启完成后,查看下最新的storage_policies是否生效:
select policy_name,volume_name,volume_priority,disks,volume_type,max_data_part_size from system.storage_policies;
┌─policy_name─┬─volume_name─┬─volume_priority─┬─disks───────┬─volume_type─┬─max_data_part_size─┐
│ default │ hot │ 1 │ ['default'] │ JBOD │ 1073741824 │
│ default │ cold │ 2 │ ['jfs'] │ JBOD │ 0 │
└─────────────┴─────────────┴─────────────────┴─────────────┴─────────────┴────────────────────┘
人工移动数据到jfs
alter table tb1111 move partition '2021-08-22' to DISK 'jfs';
ALTER TABLE tb1111
MOVE PARTITION '2021-08-22' TO DISK 'jfs'
Query id: 1a2ff5d2-539d-4939-9419-31e15a3b6607
Ok.
0 rows in set. Elapsed: 25.935 sec.
移动完成后,可以看出下数据分布情况,使用如下命令:
SELECT partition,left(path,20),table,bytes_on_disk,
data_compressed_bytes,data_uncompressed_bytes
from system.parts where database='default' order by left(path,20)
LIMIT 10;
5、添加定时任务
# 删除30天前的分区表
1 2 * * * cd /usr/local/bin && bash purge.sh 30 tb1111 >> /tmp/purge.log 2>&1
5 2 * * * cd /usr/local/bin && bash purge.sh 30 tb2222 >> /tmp/purge.log 2>&1
# 移动5天前的数据到oss
1 3 * * * cd /usr/local/bin && bash archive.sh 5 tb1111 >> /tmp/archive.log 2>&1
3 3 * * * cd /usr/local/bin && bash archive.sh 5 tb2222 >> /tmp/archive.log 2>&1
相关脚本:
purge.sh
#!/bin/bash
source /etc/profile
function purge() {
day=$(date +"%Y-%m-%d" -d -$1day)
echo "alter table default.$2 drop partition '${day}'" > purge.sql
touch /data/clickhouse/flags/force_drop_table ; chmod 666 /data/clickhouse/flags/force_drop_table
clickhouse-client -h 127.0.0.1 --port 9200 -m < purge.sql
rm -f purge.sql
}
# demo: table tb1111 , drop partition before 10days
# purge 10 tb1111
if [ $# -ne 2 ]; then
echo "parameter must 2, like: 10 table1" && exit 10
else
purge $1 $2
fi
archive.sh
#!/bin/bash
source /etc/profile
function archive() {
day=$(date +"%Y-%m-%d" -d -$1day)
echo "alter table default.$2 move partition '${day}' to DISK 'jfs';" > archive.sql
clickhouse-client -h 127.0.0.1 --port 9200 -m < archive.sql
rm -f archive.sql
}
# demo: table tb1111 , move partition to oss before 5days
# archive 5 tb1111
if [ $# -ne 2 ]; then
echo "parameter must 2, like: 5 table1" && exit 10
else
archive $1 $2
fi
可能遇到的问题:
Redis 内存增长异常 (可以参考文章头部贴的那个虾皮的分享)