hdfs java.io.IOException: Mkdirs failed to create

来源:互联网 发布:中华人民网络安全法 编辑:程序博客网 时间:2024/04/28 02:51

学习到 <<Hadoop in Action>> chapter 3.1时,使用hdfs java api编写FileMerge时发现报个错误,开始时觉得有点莫名其妙,后来查看api才只有原来是这样。


需求: 准备把local file system 中的某个文件夹下所有文件,合并到hdfs文件系统中一个文件里。

程序如下:


[java] view plain copy
  1. package com.test.study;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.FSDataInputStream;  
  7. import org.apache.hadoop.fs.FSDataOutputStream;  
  8. import org.apache.hadoop.fs.FileStatus;  
  9. import org.apache.hadoop.fs.FileSystem;  
  10. import org.apache.hadoop.fs.Path;  
  11.   
  12. public class FileMerge {  
  13.   
  14.     public static void main(String[] args) {  
  15.         Path inputDir = new Path(args[0]);  
  16.         Path outputFile = new Path(args[1]);  
  17.           
  18.         try {  
  19.             mergeFile(inputDir, outputFile);  
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.       
  25.     public static void mergeFile(Path inputDir, Path outputFile) throws IOException{  
  26.         Configuration conf = new Configuration();  
  27.         FileSystem local = FileSystem.getLocal(conf);  
  28.         FileSystem hdfs = FileSystem.get(conf);  
  29.           
  30.         // open output file stream  
  31.         FSDataOutputStream out = null;  
  32.         if(!hdfs.exists(outputFile)){  
  33.             out = hdfs.create(outputFile);  
  34.         }else{  
  35.             System.out.println("output file ["+outputFile.getName()+"] has existed.");  
  36.             return;  
  37.         }  
  38.           
  39.         FileStatus[] inputFiles = local.listStatus(inputDir);  
  40.         FSDataInputStream input = null;  
  41.         byte[] buffer = new byte[1024];  
  42.         int length = -1;  
  43.         for(FileStatus fileStatus: inputFiles){  
  44.             if(fileStatus.isDir()){  
  45.                 continue;  
  46.             }  
  47.             input = local.open(fileStatus.getPath());  
  48.             while((length = input.read(buffer))>0){  
  49.                 out.write(buffer, 0, length);  
  50.             }  
  51.             input.close();  
  52.         }  
  53.           
  54.         out.close();  
  55.     }  
  56.   
  57. }  

运行程序:  java org.test.study.FileMerge /home/walkerJong/hadoop-1.2/logs /user/walkerJong/hadoop.log


发现报以下错误:

[plain] view plain copy
  1. java.io.IOException: Mkdirs failed to create /user/walkerJong  
  2.     at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:378)  
  3.     at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:364)  
  4.     at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:564)  
  5.     at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:545)  
  6.     at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:452)  
  7.     at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:444)  
  8.     at com.b5m.study.FileMerge.mergeFile(FileMerge.java:33)  
  9.     at com.b5m.study.FileMerge.main(FileMerge.java:19)  

百思不得解决,看了看源码,发现还是没找到错在哪里?

最后在 FileSystem hdfs = FileSystem.get(conf);  之后加了一句: System.out.println(hdfs.getName());   打印出来的结果居然是 file:///  

明显获得的FileSystem是 local file system. 

错就应该错在 FileSystem hdfs = FileSystem.get(conf); 这里,因为我们想获取得到的时hdfs的文件系统,但是运行结果确实local file system.


查看FileSystem.get()相关api说明发现:

FileSystem.get(Configuration conf) 使用配置文件来获取文件系统, 配置文件conf/core-site.xml,若没有指定则返回local file system. (原来是这样)

FileSystem.get(URI uri, Configuration conf) 根据uri和conf来确定文件系统。


修改程序: FileSystem hdfs = FileSystem.get(URI.create("hdfs://localhost:9000/"), conf); 

再次运行程序,OK;


参考:http://blog.csdn.net/walkerJong/article/details/37763777

0 0
原创粉丝点击