从Hadoop分布式缓存中读取文件时的FileNotFoundExcepton

时间:2014-11-01 02:38:54

标签: java hadoop filenotfoundexception

我遇到运行Hadoop作业的问题,在尝试从分布式缓存中检索文件时收到FileNotFoundException,即使该文件存在。当我在我的本地文件系统上运行它时,它可以工作。

使用Hadoop 1.0.4版和Java 1.7版,集群托管在Amazon Web Services上。我无法控制群集或它是如何设置的。

在main函数中,我将文件添加到分布式缓存中。这似乎工作正常。我想,至少它没有抛出任何例外。

....
JobConf conf = new JobConf(Driver.class);
conf.setJobName("mean");
conf.set("lookupfile", args[2]);
Job job = new Job(conf);
DistributedCache.addCacheFile(new Path(args[2]).toUri(), conf);
...

在Map之前调用的Setup函数中,我为文件创建一个Path,并调用一个将文件加载到哈希映射中的函数。

Configuration conf = context.getConfiguration();
String inputPath = conf.get("lookupfile");                          
Path dataFile = new Path(inputPath);
loadHashMap(dataFile, context);

异常发生在加载哈希映射的函数的第一行。

brReader = new BufferedReader(new FileReader(filePath.toString()));

我这样开始工作。

hadoop jar Driver.jar Driver /tmp/input output /tmp/DATA.csv

我收到以下错误

Error: Found class org.apache.hadoop.mapreduce.Counter, but interface was expected
attempt_201410300715_0018_m_000000_0: java.io.FileNotFoundException: /tmp/DATA.csv (No such file or directory)
attempt_201410300715_0018_m_000000_0:   at java.io.FileInputStream.open(Native Method)
attempt_201410300715_0018_m_000000_0:   at java.io.FileInputStream.<init>(FileInputStream.java:146)
attempt_201410300715_0018_m_000000_0:   at java.io.FileInputStream.<init>(FileInputStream.java:101)
attempt_201410300715_0018_m_000000_0:   at java.io.FileReader.<init>(FileReader.java:58)
attempt_201410300715_0018_m_000000_0:   at Map.loadHashMap(Map.java:49)
attempt_201410300715_0018_m_000000_0:   at Map.setup(Map.java:98)
attempt_201410300715_0018_m_000000_0:   at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:145)
attempt_201410300715_0018_m_000000_0:   at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:771)
attempt_201410300715_0018_m_000000_0:   at org.apache.hadoop.mapred.MapTask.run(MapTask.java:375)
attempt_201410300715_0018_m_000000_0:   at org.apache.hadoop.mapred.Child$4.run(Child.java:259)
attempt_201410300715_0018_m_000000_0:   at java.security.AccessController.doPrivileged(Native Method)
attempt_201410300715_0018_m_000000_0:   at javax.security.auth.Subject.doAs(Subject.java:415)
attempt_201410300715_0018_m_000000_0:   at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1140)
attempt_201410300715_0018_m_000000_0:   at org.apache.hadoop.mapred.Child.main(Child.java:253)
14/11/01 02:12:49 INFO mapred.JobClient: Task Id : attempt_201410300715_0018_m_000001_0, Status : FAILED

我已经验证该文件存在于HDFS和本地文件系统中。

hadoop@hostname:~$ hadoop fs -ls /tmp
Found 2 items
drwxr-xr-x   - hadoop supergroup          0 2014-10-30 11:19 /tmp/input
-rw-r--r--   1 hadoop supergroup     428796 2014-10-30 11:19 /tmp/DATA.csv

hadoop@hostname:~$ ls -al /tmp/
-rw-r--r--  1 hadoop hadoop 428796 Oct 30 11:30 DATA.csv

老实说,我不明白这里有什么问题。该例外列出了文件的正确路径。我已经验证该文件存在于HDFS和本地文件系统上。我在这里缺少什么吗?

2 个答案:

答案 0 :(得分:0)

BufferedReader的输入应该来自Setup()中的DistributedCache.getLocalCacheFiles()返回的路径。更像是..

Path[] localFiles = DistributedCache.getLocalCacheFiles();
if (localFiles.length > 0){
   brReader = new BufferedReader(new FileReader(localFiles[0].toString());      
}

答案 1 :(得分:0)

我遇到了同样的问题,下面的代码对我有用:

Configuration conf = context.getConfiguration();  
URI[] uriList = DistributedCache.getCacheFiles(conf);
BufferedReader br = new BufferedReader(new FileReader(uriList[0].getPath()))

如您所见,我在这里使用getCacheFiles方法,然后获取文件路径并读取文件。