MapReduce 从任务中的类路径读取文件

MapReduce read file from classpath in Tasks

我已经在我的 fat jar 中捆绑了一个文件 "xxx.txt.gz"

我需要在每个 Map 任务中的每个 YARN 容器中引用这个文件。

所以如果你看看我的罐子里面:

你会看到 xxx.txt.gz*

我正在尝试通过

访问此文件
File mappingFile = new File(getClass().getClassLoader().getResource("xxx.txt.gz").getFile())

但是,在 运行 时,我从所有任务尝试的日志中收到以下错误

java.io.FileNotFoundException: file:/local/hadoop/1/yarn/local/usercache/USER/appcache/application_1431608807540_0071/filecache/10/job.jar/job.jar!/xxx.txt.gz (No such file or directory)

换句话说,即使我的 fat jar 有文件,job.jar 却没有。

我该如何补救?

非常感谢。

还有另一种从 Mappers/Reducers 访问文件的方法。希望这个想法在 mapreduce 中可能是理想的。

您可以使用 mapreduce 中可用的 Distributed Cache 选项。通过这种方式,您可以让 hadoop 将您的文件分发到您的作业 Mappers/Reducers 将在其上执行的所有容器。

我实际上意识到在 Hadoop 2.7 中不推荐使用 DistributedCache。但是,对于小 utility/lookup 文件,可以将它们添加到 HDFS,然后使用常规机制将它们加载到 Mapper/Reducer JVMS。

例如:

public void setup(Context ctx) {
   // gets the job config, therefore, handles the case where the file is located on the local FS or HDFS)
   Configuration jobConf = context.getConfiguration();
   Path filePath = new Path(jobConf.get("my.mapping.file"));
   FileSystem.get(conf).open(filePath);
}