我正在尝试动态创建一个资源,并在我的作业运行之前将其放置在 Hadoop 分布式缓存中——也就是说,这将是一个自动化作业,需要在映射器执行之前将几件事收集在一起(通过 HTTP)。
我面临的问题是,由于我运行的映射器数量太多,我无法将其置于设置阶段——这给被调用的服务器带来了过高的负载。我希望能够检索我的资源,将它们写入文件,然后将其放在分布式缓存中以便以后轻松访问。
大节点:我不想将文件写入 Hadoop,我宁愿将它放在本地节点上。
// The whitelist cache file
File resourceFile = new File("resources.json");
// Create an output stream
FileOutputStream outputStream = new FileOutputStream(resourceFile.getAbsoluteFile());
// Write the whitelist to the local file
// (this is using Jackson JSON, FYI)
mapper.writeValue(outputStream, myResources);
// Add the file to the job
job.addCacheFile(new URI("file://" + resourceFile.getAbsolutePath()));
这在我工作的 run()
方法中运行,即在映射器开始之前 - 但每当我尝试访问 new File("resources.json") 时它都会给我一个 FileNotFoundException
在我的映射器中。
创建这些临时文件的正确方法是什么,以及在作业中访问它们的最佳方法是什么?
最佳答案
尝试将您的文件放入分布式缓存中:
_job.addCacheFile(new URI(filePath+"#"+filename));
其中文件名是文件在分布式缓存中的名称。
在 Mapper 上像这样读取文件:
Path path = new Path (filename);
FileSystem fs = FileSystem.getLocal(context.getConfiguration());
BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(path)));
关于java - 在 Hadoop 分布式缓存中创建和放置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484242/