Hive批量删除表分区
shell脚本
#!/usr/bin/bash
#获取当前脚本所在路径
cur_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd ${cur_dir}
#导出所有hive数据库名
hive -e "show databases;" > ${cur_dir}/all_database.txt
#获取以src开头的库名
cat all_database.txt | awk '{print $2}' | awk '/^src_.*/{print}' > ${cur_dir}/src_database.txt
for database in `cat ${cur_dir}/src_database.txt`
do
#获取表名
hive -e "use ${database};show tables;" > ${cur_dir}/${database}_tbs.txt
for table in `tail -n +3 ${cur_dir}/${database}_tbs.txt | awk '/[a-zA-Z]/{print $2}'`
do
echo -e "\e[36m -----${database}.${table}------ \e[0m"
#获取表分区
hive -e "show partitions ${database}.${table}" > ${cur_dir}/${database}.${table}_pts.txt
#循环表的分区
for partition in `tail -n +3 ${cur_dir}/${database}.${table}_pts.txt | awk '/[a-zA-Z0-9]/{print $2}' | awk '/^batch_no=202005.*/{print}'`
do
echo -e "\e[36m-----${partition}------\e[0m"
#删除表的分区
hive -e "alter table ${database}.${table} drop if exists partition (${partition});"
done
done
done
HDFS清理回收站
hdfs dfs -du -h /user/hive/
hdfs dfs -rm -r -skipTrash /user/hive/.Trash
版权声明:本文为docsz原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。