1、原hdfs上文件目录
2、编写shell脚本delete_external_partitiondata.sh
#! /bin/bash
#要求传入的数据格式为yyyyMMdd的两个开始和结束参数,如20210302 20210303
tableName=$1
partitionName=$2
start=$3
end=$4
#将输入的日期转为的时间戳格式
startDate=`date -d "${start}" +%s`
endDate=`date -d "${end}" +%s`
#计算两个时间戳的差值除于每天86400s即为天数差
stampDiff=`expr $endDate - $startDate`
dayDiff=`expr $stampDiff / 86400`
for ((i=0;i<=$dayDiff;i++))
do
process_date=`date -d "${start} $i day" +'%Y%m%d'`
info=`hive -e "desc formatted $tableName"`
info=(`echo $info| sed -r "s/.*?(hdfs:.*?) Table.*?/\1/g"`)
info=${info[0]}
hdfs dfs -rm -r "$info/$partitionName=$process_date"
done
3、执行脚本
[root@sgh1vhdp01 shelltest]# ./clearpartitiondata.sh clearpartitiondata_test dt 20210301 20210302
4、删除后hdfs文件目录