MapReduce 判断输出路径是否存在问题

来源:互联网 发布:东方财富网龙虎榜数据 编辑:程序博客网 时间:2024/04/30 20:20


          写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://blog.csdn.net/yongh701/article/details/50601811



原创粉丝点击