博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MapReduce 判断输出路径是否存在问题
阅读量:4094 次
发布时间:2019-05-25

本文共 1107 字,大约阅读时间需要 3 分钟。

          写MapReduce程序时,最后加一个判断当前输出路径是否存在的代码,如果输出路径存在则删除。这样可以避免出现如下错误:

Output directory hdfs://192.168.42.130:9000/output already exists

具体代码如下:

final static String OUTPUT_PATH = "hdfs://192.168.42.130:9000/output";   //输出路径用字符串表示,在主类中定义,或者由主方法参数给出Path path = new Path(OUTPUT_PATH);    FileSystem fileSystem = path.getFileSystem(conf);     //getFileSystem()函数功能  Return the FileSystem that owns this Path. 	if (fileSystem.exists(new Path(OUTPUT_PATH))) {	    fileSystem.delete(new Path(OUTPUT_PATH),true);	}

在FileSystem 类下的delete()函数源码如下:

public boolean delete(Path f) throws IOException {    return delete(f, true);  }   /** Delete a file.   *   * @param f the path to delete.   * @param recursive if path is a directory and set to   * true, the directory is deleted else throws an exception. In   * case of a file the recursive can be set to either true or false.   * @return  true if delete is successful else false.   * @throws IOException   */  public abstract boolean delete(Path f, boolean recursive) throws IOException;

          函数由两个参数,第一个为要删除文件的路径(类型Path),第二个函数为是否递归删除文件夹及其字目录(类型boolean),一般为true。删除失败抛出IOException异常。

转载地址:http://pntii.baihongyu.com/

你可能感兴趣的文章
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
【增强学习在无人驾驶中的应用】
查看>>
《python+opencv实践》四、图像特征提取与描述——29理解图像特征
查看>>
《python+opencv实践》四、图像特征提取与描述——30Harris 角点检测
查看>>
《python+opencv实践》四、图像特征提取与描述——31 Shi-Tomasi 角点检测& 适合于跟踪的图像特征
查看>>
OpenCV meanshift目标跟踪总结
查看>>
人工神经网络——神经元模型介绍
查看>>
人工神经网络——感知器介绍
查看>>
人工神经网络——反向传播算法(BackPropagation)
查看>>
进程的地址空间概述
查看>>
Windows 窗口底层原理
查看>>
一种函数指针的运用
查看>>
Win32程序之进程的原理
查看>>
C++虚函数原理
查看>>
MySQL的索引
查看>>
今天,Python信息量很大!
查看>>
Flash 已死,Deno 当立?
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
都无代码了,还要程序员吗?
查看>>