本文共 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/