1.产生原因
1.使用Hue的界面工具执行Hive-sql。Hue会自动保存sql执行结果方便用户能够查看历史执行记录,所以会在相应目录下生成hive-staging文件;
2.Hive-sql任务执行过程中出现异常,导致hive-staging文件未删除,未出现异常时,hive会自行删除hive-staging文件;
3.使用spark-sql on yarn 跑sql 程序生成的hive-staging文件不会被删除
2.解决方案
1.配置hive-site.xml文件
<property>
<name>hive.insert.into.multilevel.dirs</name>
<value>true</value>
<description>允许生成多级目录</description>
</property>
<property>
<name>hive.exec.stagingdir</name>
<value>/tmp/hive/staging/.hive-staging</value>
<description>临时文件暂放目录</description>
</property>
CDH环境配置
2.使用shell脚本定时删除临时文件
#! /bin/bash #hive-staging文件夹位置 param=$1 #获取前一天时间 time=`date +%F -d "-1 days"` #获取待删除的文件 echo 'get files------' regexp="*{0,}hive-staging_hive_""${time}*" echo "regexp---""${regexp}" arr=(`hdfs dfs -ls ${param} | awk '{print $8}' | egrep "${regexp}" | tr '\n' ' '`) echo ${arr[0]} for i in "${arr[@]}" do file="$i" `hdfs dfs -rm -r "${file}"` done
3.将shell脚本加入linux定时任务就可以了
vi /etc/crontab
#加入下面命令
01 0 * * * /usr/bin/sh /home/shell-file/test.sh ${你的hive-staging目录} >>${你的日志存放路径}/`date +%F`.log 2>&1
3.参考资料
1.【大数据】hive 删除临时文件 .hive-staging_hive - TurboWay - 博客园 (cnblogs.com)
2.占用_06535.16.1Hive Staging目录占用大量HDFS空间问题分析_weixin_39604819的博客-CSDN博客