筛选需要删除的分区,此处指定时间
#!/bin/bash
#author:robin
#why: clean clickhouse data
DATE=$(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.list
if [ $? -eq 0 ]
then
echo "[ok]"
else
echo "[failed]"
fi
清理筛选出来的分区
#!/bin/bash
#author: robin
#why: clean clickhouse data
QUERY="ALTER TABLE $TABLE DROP PARTITION '$PARTITION'"
for((i=1;i<3000;i++))
do
TABLE=`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" ]
then
break
fi
sudo 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.log
if [ $? -eq 0 ]
then
echo "[ok]"
else
echo "[failed] clickhouse-client -h 0.0.0.0 -q "ALTER TABLE "${DATABASE}.${TABLE}" DROP PARTITION "'${PARTITION}'";""
fi
done
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’