hive集群中间(临时)表定时清理shell脚本

    在使用hive集群的过程中,经常会生成一些一次性使用的中间表,这种中间表的使用率不高,而已可以再次生成,但大量的中间表会占用很多的集群存储资源,下面我教大家写一个定时清理hive集群中间表的shell脚本,但中间表必须有固定格式,例如:博主的集群中间表全是vra开头,并且中间含有res,并以tmp结尾。那么该脚本如下:

#!/bin/bash
#######hive集群中间表定时清理脚本################

databases="000ad34d 010c1ce4 011a2ba2 01352ff8 0171bf86 018d1922 0192b00e 0301591f 031b7df5 0347f5c5 03672e0f 038ce06 050cb58d 05102e7b 059c1852 070ae557 071fbbd7 0749d483 075d33f 079f3c90 08183aa4 083b116c 0842f4n1 085afq01 086326a6 08765t9a 0889be7e 089cdf6a 0904f126 091by3a9 0974c2da"
#####hive集群中所有库库名
stime=`date +%s`
#记录当前时间
for db in $databases
do
#遍历所有库
echo "===========================开始清理库$db======================================="
hive -e "use $db;show tables like 'vra*res*tmp';" >/home/deployer/zcb/tmp/db_tmp.txt
#取出当前库中中间表表名
echo "=========================该库下有以下中间表===================================="
cat /home/deployer/zcb/tmp/db_tmp.txt
#输出显示当前库中中间表表名
echo "=========================中间表展示完毕========================================"
    
         for tb in `cat /home/deployer/zcb/tmp/db_tmp.txt`
		do
			hive -e "use $db;drop table if exists $tb"
#挨个表删除
		done
echo "===========================清理库结束$db======================================="
done

etime=`date +%s`
s=`echo "scale=0; ($etime - $stime)%60" | bc`
m=`echo "scale=0; ($etime - $stime)/60%60" | bc`
h=`echo "scale=0; ($etime - $stime)/60/60" | bc`

echo  ========================================脚本执行完毕=========================================                                                                     

添加定时任务,每月自动执行

#集群中间表定时清理脚本每月1号晚上8点开始执行
0 20 1 * * sh /home/deployer/zcb/tmp/saas_tb_tmp.sh >> /home/deployer/zcb/tmp/log/saas_`date +"\%Y\%m\%d"`.log 2>&1

猜你喜欢

转载自blog.csdn.net/zcb_data/article/details/80576963
今日推荐