2015-12-29 178 views
0

我想我的第一個代碼將文件從HDFS複製到本地文件系統。但是,我無法複製,即使這些文件已預先設置好,但我收到了異常:FileNotFoundExceptoinHadoop文件未找到異常

這裏是我的代碼: -

public class Exercise1 { 
    public static void main(String[] args) throws IOException ,URISyntaxException { 
     // TODO Auto-generated method stub 
     Path glob = new Path("/training/exercises/filesystem/*.txt"); 
     String localRoot = "/home/hadoop/Training/play_area/exercises/filesystem/e1/"; 

     FileSystem hdfs = FileSystem.get(new Configuration()); 
     FileStatus [] files = hdfs.globStatus(glob); 
     for (FileStatus file : files) { 
      Path from = file.getPath(); 
      Path to = new Path(localRoot, file.getPath().getName()); 
      System.out.println("Copying hdfs file [" + from + "] to local [" + to + "]"); 
      hdfs.copyToLocalFile(from, to); 
     } 
    } 
} 

我得到的錯誤是: -

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" java.io.FileNotFoundException: File /training/exercises/filesystem does not exist 
    at org.apache.hadoop.fs.RawLocalFileSystem.listStatus(RawLocalFileSystem.java:315) 
    at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1281) 
    at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1321) 
    at org.apache.hadoop.fs.ChecksumFileSystem.listStatus(ChecksumFileSystem.java:557) 
    at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1281) 
    at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1358) 
    at org.apache.hadoop.fs.FileSystem.globStatusInternal(FileSystem.java:1488) 
    at org.apache.hadoop.fs.FileSystem.globStatus(FileSystem.java:1443) 
    at org.apache.hadoop.fs.FileSystem.globStatus(FileSystem.java:1421) 
    at hdfs.javaAPI.Exercise1.main(Exercise1.java:24) 

幫我解決這個錯誤。

回答

0

我已經在我的機器上測試過你的代碼,沒有錯誤,沒有例外,並且從hdfs複製文件到本地成功。對不起,我幫不了你。請仔細檢查你的文件在hdfs上。

+0

需要更改的任何文件在什麼??? – daivik

+0

@daivik不需要!也許你的HDFS確實不存在那個文件! – Guo

0

當你用'/'開始你的路徑時,你的意思是你的路徑從根開始。你應該使用完整的URI。改變從「/培訓/運動/文件系統/ 的.txt」到「/家庭/ Hadoop的/培訓/運動/文件系統/的.txt」

+0

謝謝!但沒有工作。有什麼方法可以解決嗎? – daivik

+0

@daivik改爲使用您的完整hdfs網址。像這樣:'hdfs:// yourhostname:yourportnumber/training/exercises/filesystem/.txt' –

0

我有這樣的一些錯誤的路徑,可能這將幫助你只需創建一個名爲log4j.properties的文件。然後,填寫文件,此代碼

hadoop.root.logger=DEBUG, console 
log4j.rootLogger = DEBUG, console 
log4j.appender.console=org.apache.log4j.ConsoleAppender 
log4j.appender.console.target=System.out 
log4j.appender.console.layout=org.apache.log4j.PatternLayout 
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n 

請確保,這個代碼是corrent文件