筛选需要删除的分区,此处指定时间
#!/bin/bash#author:robin#why: clean clickhouse dataDATE=$(date -d"15 day ago" +%Y-%m-%d)QUERY="select table,partition,database from parts where active and max_date < '$DATE';"echo "$QUERY"/opt/dtstack/clickhouse/bin/clickhouse-client -h 0.0.0.0 -d system -q "$QUERY" > /tmp/clean_ck_data.listif [ $? -eq 0 ]thenecho "[ok]"elseecho "[failed]"fi
清理筛选出来的分区
#!/bin/bash#author: robin#why: clean clickhouse dataQUERY="ALTER TABLE $TABLE DROP PARTITION '$PARTITION'"for((i=1;i<3000;i++))doTABLE=`sed -n "${i},1p" /tmp/clean_ck_data.list | awk '{print $1}'`PARTITION=`sed -n "$i,1p" /tmp/clean_ck_data.list | awk '{print $2}'`DATABASE=`sed -n "${i},1p" /tmp/clean_ck_data.list | awk '{print $3}'`if [ -z "$TABLE" ]thenbreakfisudo touch '/data/clickhouse/data/flags/force_drop_table' && sudo chmod 666 '/data/clickhouse/data/flags/force_drop_table'ssh xx.xx.xx.xx "sudo touch '/data/clickhouse/data/flags/force_drop_table' && sudo chmod 666 '/data/clickhouse/data/flags/force_drop_table'"/opt/dtstack/clickhouse/bin/clickhouse-client -h 0.0.0.0 -d $DATABASE -q "ALTER TABLE \"${TABLE}\" DROP PARTITION '${PARTITION}';" >> /tmp/clean_ck_data.logif [ $? -eq 0 ]thenecho "[ok]"elseecho "[failed] clickhouse-client -h 0.0.0.0 -q "ALTER TABLE "${DATABASE}.${TABLE}" DROP PARTITION "'${PARTITION}'";""fidone
Tips
ClickHouse client version 19.14.6.12版本,如果分区过大,清理时会限制删除操作,需要每次执行前创建一个临时文件,如果集群中有多个节点,多个节点都需要创建此文件
Reason:
- Size (53.95 GB) is greater than max_[table/partition]_size_to_drop (50.00 GB)
- File ‘/data/clickhouse/data/flags/force_drop_table’ intended to force DROP doesn’t exist
How to fix this:- Either increase (or set to zero) max_[table/partition]_size_to_drop in server config and restart ClickHouse
- Either create forcing file /data/clickhouse/data/flags/force_drop_table and make sure that ClickHouse has write permission for it.
Example:
sudo touch ‘/data/clickhouse/data/flags/force_drop_table’ && sudo chmod 666 ‘/data/clickhouse/data/flags/force_drop_table’.
[failed] clickhouse-client -h 0.0.0.0 -q ALTER TABLE local_3.im DROP PARTITION ‘2020-02-25180d’
