客户端在连接hiveserver2时,会在hdfs上创建${hive.exec.scratchdir}/<username> (开启doAs为登录用户,否则为启动用户)目录,用于存放作业执行过程中产生的临时文件,在执行某些作业时会产生大量的临时文件,如遇客户端异常或jvm异常退出,造成数据无法清理。

hive提供如下方案解决清理临时文件问题:

1、 hive.start.cleanup.scratchdir : true  默认 :false

hiveserver2在启动的时候回清理 ${hive.exec.scratchdir}/<username> 下目录,若为多hiveserver2 服务且目录配置一致时,会造成删除正在执行作业的临时目录

2、hive.scratchdir.lock :true 默认: false

在scratchdir中保存锁文件,以防止被cleardanglingscratchdir删除

3、hive.server2.clear.dangling.scratchdir :true 默认 :false

开启一个线程池自动清理异常情况未被清理的临时目录,和hive.scratchdir.loc共同管理临时目录的清理,如果客户端处于活动状态,则锁定文件保持打开状态,并且 cleardanglingscratchdir 将检测到它并且不将其视为悬空暂存目录。只有当 Hive 进程意外死亡并留下临时目录时,它才会成为 cleardanglingscratchdir 删除的目标。

如上所述,有如下方案处理临时目录:

1、hive.start.cleanup.scratchdir 设置true;在hive启动时自动清理临时目录,对于多hiveserver2服务,修改hive.exec.scratchdir 配置,以服务命名。

2、hive.scratchdir.lock 设置true,hive.server2.clear.dangling.scratchdir设置true;在hiveserver2服务端口开启临时目录清理线程池,自动清理未被占用的临时目录。

建议采用方案1,只是在启动时清理临时目录,hiveserver2 也有自己的session管理机制,超时异常的session会被清理,临时目录也会被清理,所以异常生成的临时目录不会很多,方案2虽然会自动处理,但会在每个临时目录下生产锁文件,对于服务任务负载高时也会对hdfs产生影响,并且需要清理的临时目录也不会很多,故建议方案1;hiveserver2的正确下线方式为先删除注册到zookeeper上的临时目录,这样客户端就不会通过连接串登录此服务, 当该服务上的作业运行完成session退出时,hiveserver2服务也会停止。

如下为hiveserver2自动清理临时目录的逻辑,利用hdfs的文件锁判断是否被占用,借鉴

public void run() {try {//获取session临时目录即 hive.exec.scratchdir目录Path rootHDFSDirPath = new Path(rootHDFSDir);FileSystem fs = FileSystem.get(rootHDFSDirPath.toUri(), conf);FileStatus[] userHDFSDirList = fs.listStatus(rootHDFSDirPath);List<Path> scratchDirToRemove = new ArrayList<Path>();for (FileStatus userHDFSDir : userHDFSDirList) {FileStatus[] scratchDirList = fs.listStatus(userHDFSDir.getPath());for (FileStatus scratchDir : scratchDirList) {Path lockFilePath = new Path(scratchDir.getPath(), SessionState.LOCK_FILE_NAME);//判断是否有锁文件,没有掉过,不做清理if (!fs.exists(lockFilePath)) {String message = "Skipping " + scratchDir.getPath() + " since it does not contain " +SessionState.LOCK_FILE_NAME;if (verbose) {consoleMessage(message);}continue;}boolean removable = false;boolean inuse = false;try {//往此文件追加内容IOUtils.closeStream(fs.append(lockFilePath));removable = true;} catch (RemoteException eAppend) {// RemoteException with AlreadyBeingCreatedException will be thrown// if the file is currently held by a writer//如果文件当前由编写器持有,将引发带有AlreadyBeingCreateDexException的RemoteExceptionif(AlreadyBeingCreatedException.class.getName().equals(eAppend.getClassName())){inuse = true;} else if (UnsupportedOperationException.class.getName().equals(eAppend.getClassName())) {// Append is not supported in the cluster, try to use create//群集中不支持追加,请尝试使用创建try {IOUtils.closeStream(fs.create(lockFilePath, false));} catch (RemoteException eCreate) {if (AlreadyBeingCreatedException.class.getName().equals(eCreate.getClassName())){// If the file is held by a writer, will throw AlreadyBeingCreatedException//如果文件由编写器保存,将抛出AlreadyBeingCreatedExceptioninuse = true;}  else {consoleMessage("Unexpected error:" + eCreate.getMessage());}} catch (FileAlreadyExistsException eCreateNormal) {// Otherwise, throw FileAlreadyExistsException, which means the file owner is// dead//否则,抛出FileAlreadyExistsException,这意味着文件所有者已死亡,后续可以清理此临时目录removable = true;}} else {consoleMessage("Unexpected error:" + eAppend.getMessage());}}if (inuse) {// Cannot open the lock file for writing, must be held by a live processString message = scratchDir.getPath() + " is being used by live process";if (verbose) {consoleMessage(message);}}if (removable) {scratchDirToRemove.add(scratchDir.getPath());}}}if (scratchDirToRemove.size()==0) {consoleMessage("Cannot find any scratch directory to clear");return;}consoleMessage("Removing " + scratchDirToRemove.size() + " scratch directories");for (Path scratchDir : scratchDirToRemove) {if (dryRun) {System.out.println(scratchDir);} else {boolean succ = fs.delete(scratchDir, true);if (!succ) {consoleMessage("Cannot remove " + scratchDir);} else {String message = scratchDir + " removed";if (verbose) {consoleMessage(message);}}}}} catch (IOException e) {consoleMessage("Unexpected exception " + e.getMessage());}
}

hiveserver2 清理hdfs的临时目录相关推荐

  1. Linux临时目录/tmp(mktemp命令、mktemp指令)(mktemp -d)临时目录清理服务systemd-tmpfiles-clean.service

    文章目录 mktemp -d linux为什么要设置这个临时目录`/tmp` 临时目录`/tmp`的定时清理 示例:查看ubuntu20.04系统下的临时目录定时清理服务 mktemp -d mkte ...

  2. 清理临时目录mysql,把MySQL的临时目录迁移到内存上-临时文件夹

    如果你的查询一直在写磁盘,但是又常常因为无理的业务需求导致无法修改你的查询语句,于是就一直在崩溃的边缘,并且要哭的感觉的时候,可以试试为mysql tmpdir创建一个内存支持的文件系统,以获得更快的 ...

  3. [hbase]Hbase 在HDFS上的目录树结构

    自从学习了HDFS之后,再学习HBASE之后,我了解到HBASE是架设在HDFS上的,数据存储在HDFS上,哪HBASE在HDFS上的目录树是怎样的呢? 一.0.94-cdh4.2.1版本 系统级别的 ...

  4. python tempfile 创建临时目录

    一.tempfile介绍 该模块创建临时文件和目录.它适用于所有支持的平台.TemporaryFile,NamedTemporaryFile,TemporaryDirectory,和SpooledTe ...

  5. python tempfile cleanup_Python tempfile模块:生成临时文件和临时目录

    Python tempfile模块:生成临时文件和临时目录 tempfile 模块专门用于创建临时文件和临时目录,它既可以在 UNIX 平台上运行良好,也可以在 Windows 平台上运行良好. te ...

  6. python tempfile自动删除_Python tempfile模块生成临时文件和临时目录

    tempfile 模块专门用于创建临时文件和临时目录,它既可以在 UNIX 平台上运行良好,也可以在 Windows 平台上运行良好. tempfile 模块中常用的函数,如表 1 所示. 表 1 t ...

  7. MySQL 临时目录

    MySQL数据目录/data/mysql所在的上层目录/data磁盘空间不足导致MySQL启动失败,所以清理了/data目录下除了mysql子目录外的其他无用目录.重启发现还是失败.检查错误日志.看到 ...

  8. 服务器删除临时目录问题

    注:文中的"Doctor"是精分对话者,另一个我,道德约束者,话唠,请无视他. 开门见山: 最近某一天,项目经理打开测试环境看的时候,脸一黑,跑过来拍我肩膀:老兄呀,你看看这个批量 ...

  9. Go 学习笔记(49)— Go 标准库之 io/ioutil(读写文件、获取目录下的文件和子目录、创建临时目录和文件)

    1. 简要概述 import "io/ioutil" 包 io/ioutil 实现一些 I/O 实用程序函数. 2. 相关函数 2.1 func ReadAll func Read ...

  10. java创建临时文件夹_如何在Java中创建一个临时目录/文件夹?

    有没有在Java应用程序内部创建临时目录的标准可靠方法? Java的问题数据库中有一个条目,注释中包含一些代码,但是我想知道在一个常用的库(Apache Commons等)中是否找到标准解决方案? # ...

最新文章

  1. python时间处理模块datetime+dateutil、numpy时间处理模块datetime64以及pandas时间处理模块Timestamp的演化路径及常用处理接口
  2. SQL Server 2008 R2 安全性专题(一):安全原则
  3. uni-app中使用lodash_Vue 使用中的小技巧
  4. [Leetcode] Majority Element 众数
  5. 基于Spring boot+Vue的在线考试系统
  6. Redis持久化的原理及优化
  7. sybase相关的知识
  8. scala中的柯里化函数
  9. C++ 顺序容器基础知识总结
  10. 通过代理查看Myeclipse的主页
  11. WinXP系统连接网络教程
  12. Linux内核加载f2fs,固态硬盘使用f2fs文件系统作为deepin引导分区经验
  13. Python实战之12306抢票
  14. 获取计算机登录用户名和密码,查看电脑的登录用户名_查看电脑用户名和密码...
  15. drozer工具介绍和使用
  16. ABBYY FineReader15老牌ocr文字识别软件
  17. 配合字体图标的搜索框
  18. 拥抱云原生,聊聊高度解耦的密码管理解法
  19. stm32 hid游戏手柄程序
  20. Linux常见、常用命令(二)

热门文章

  1. 【航拍中国第二季】甘肃笔记
  2. PMP项目管理—项目风险管理(7)
  3. windows server 2008共享文件夹
  4. ContextCaptureMaster/Smart3D集群设置经验分享
  5. C++ 实现即时通信软件(直接运行)
  6. 什么叫定向广告?定向传播有哪些好处
  7. 华为外包数据库面试问题分享20211225
  8. COT为平台各项功能,进行TOKEN化赋能
  9. 第二届CCF计算机职业资格认证考试题解(C++)
  10. 深度linux卡在logo,记录一次深度deepin开机卡logo解决方案