Hive批量删除表分区

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

猜你喜欢

转载自blog.csdn.net/docsz/article/details/111685972