shell脚本删除hive外部表数据(元数据+hdfs数据)

本人分区字段 dt 格式为 2021-06-28
分区建表hdfs存储位置为 /warehouse/xxx/ods/ods_tbl_table1/dt=2021-06-28/

删除hive外部表数据
1.删除元数据
2.删除hdfs存储数据

#!/bin/bash

# 执行sh xx.sh 2021-05-01 2021-06-01
# 删除hive表分区元数据及hdfs数据
# APP为hive数据库名称
APP=xxx
# hive启动目录
hive=/usr/hdp/3.1.4.0-315/hive/bin/hive

# 所执行的表名称
tables=(
"ods_tbl_table1"
"ods_tbl_table2"
"ods_tbl_table3"
)

if [[ -n "$1" ]]; then
    do_date=$1
fi

if [[ -n "$2" ]]; then
    endDay=$2
fi

for table in ${tables[*]}
        do
        echo $table
        #使用正则匹配目标表在hdfs上的路径
        info=`$hive -e "desc formatted $APP.$table"`
         #清楚分区元数据
        info=(`echo $info| sed -r "s/.*?(hdfs:.*?) Table.*?/\1/g"`)
        info=${info[0]}
        pp=("dt")
        ldate=$do_date
        rdate=$endDay
        while [ $ldate != $rdate ]
        do
        sql="alter table ${APP}.$table drop partition(dt='$ldate');"
        $hive -e "$sql1"
        #循环拼出分区名
        path=$info"/"${pp}=$ldate
        hdfs dfs -rm -r "$path"
        ldate=`date -d "+1 day ${ldate}" +%F` #获取第二天的时间
        done
done


猜你喜欢

转载自blog.csdn.net/weixin_41772761/article/details/118301956
今日推荐