我有一份工作,我想通过多个Mappers访问同一个文件。最初,我尝试在每个映射器中打开并查找文件,但事实证明,这是非常慢的。
是否有可能在run()方法中打开文件(我在这里做job.SetOutputPath等事情),然后与Mappers共享这个打开的文件,这样我就不会有惊人的100多个Mappers分别打开同一个文件的开销了吗?
发布于 2013-11-07 19:35:49
是的,这实际上是有可能的。如果您在作业开始前设置了分布式缓存并将文件加载到它,它将自动发送到映射器。
示例分布式缓存设置:
String fileLocation;//set this to file absolute location
Configuration conf; //job Configuration
DistributedCache.addLocalFiles(conf,fileLocation);
conf.set("fileLocation",fileLocation);在Mapper安装方法中检索:
Configuration mapConf = context.getConfiguration();
URI[] cacheURIArray = DistributedCache.getCacheFiles();
String file2Location = mapConf.get("file2Location");
List<String> fileWords = new ArrayList<String>(); //set this as a clas variable so it can be accessed outside of the setup method of the mapper
for(URI uri: cacheURIArray){
if( uri.toString().matches(".*"+fileLocation)){
BufferedReader br = new BufferedReader(new InputStream(cacheFileSystem.open(new Path(uri.toString()))));
String line = "";
line = br.readLine();
while(line != null){
fileWords.add(line);
line = br.readLine();
}
}
}您的检索方法可能至少与我提供的示例略有不同,但它有助于说明如何使用分布式缓存。有关更多信息,请访问分布式缓存
https://stackoverflow.com/questions/19844191
复制相似问题