Shell脚本-表数据覆盖重写方式合并Hive表小文件
Hive表小文件合并-表数据覆盖重写:
tablename=$1 #带库名的表面
dateStr=$2 #需要过滤的分区子字符串
# 列出所有分区进行grep分区
for i in `hive -e "show partitions $tablename" | grep $dateStr`
do
partitionKey1=`echo "$i"|awk -F '/' '{print $1}'|awk -F '=' '{print $1}'`
partitionValue1=`echo "$i"|awk -F '/' '{print $1}'|awk -F '=' '{print $2}'`
partitionKey2=`echo "$i"|awk -F '/' '{print $2}'|awk -F '=' '{print $1}'`
partitionValue2=`echo "$i"|awk -F '/' '{print $2}'|awk -F '=' '{print $2}'`
echo "$i $partitionKey1 $partitionValue1 $partitionKey2 $partitionValue2"
#例分区字符串是:sample_date=20211201/partition_name=0 则输出是:sample_date 20211201 partition_name 0
sqlstr="insert overwrite table $tablename select * from $tablename where $partitionKey1='$partitionValue1' and $partitionKey2='$partitionValue2';"
echo "set hive.execution.engine=tez; set hive.merge.mapredfiles=true; $sqlstr"
hive -e "$sqlstr"
done启动命令:
./mergeSmallFileByHiveOverwrite.sh 库名.表名 过滤字符串后台进程启动
nohup ./mergeSmallFileByHiveOverwrite.sh 库名.表名 过滤字符串 > mergeSmallFileByHiveOverwrite.out 2>&1 &