转载请注明出处: http://www.codelast.com/ 现象:和 这个 帖子描述的一样,简单说来就是,在hadoop 2.x上,用新的distributedcache的api,在mapper中会获取不到这个cache文件。 下面就详细地描述一下新旧api的用法区别以及解决办法。 『1』 旧api 将hdfs文
转载请注明出处:http://www.codelast.com/
现象:和这个帖子描述的一样,简单说来就是,在hadoop 2.x上,用新的distributedcache的api,在mapper中会获取不到这个cache文件。
下面就详细地描述一下新旧api的用法区别以及解决办法。
『1』旧api
将hdfs文件添加到distributed cache中:
configuration conf = job.getconfiguration();distributedcache.addcachefile(new uri(inputfileonhdfs), conf); // add file to distributed cache
其中,inputfileonhdfs是一个hdfs文件的路径,也就是你要用作distribute cache的文件的路径,例如 /user/codelast/123.txt
在mapper的setup()方法中:
configuration conf = context.getconfiguration();path[] localcachefiles = distributedcache.getlocalcachefiles(conf);readcachefile(localcachefiles[0]);
其中,readcachefile()是我们自己的读取cache文件的方法,可能是这样做的(仅举个例子):
private static void readcachefile(path cachefilepath) throws ioexception { bufferedreader reader = new bufferedreader(new filereader(cachefilepath.touri().getpath())); string line; while ((line = reader.readline()) != null) { //todo: your code here } reader.close();}
文章来源:http://www.codelast.com/
『2』新api
上面的代码中,addcachefile() 方法和 getlocalcachefiles() 都已经被hadoop 2.x标记为 @deprecated 了。
因此,有一套新的api来实现同样的功能,这个链接里有示例,我在这里再详细地写一下。
将hdfs文件添加到distributed cache中:
job.addcachefile(new path(inputfileonhdfs).touri());
在mapper的setup()方法中:
configuration conf = context.getconfiguration();uri[] localcachefiles = context.getcachefiles();readcachefile(localcachefiles[0]);
其中,readcachefile()是我们自己的读取cache文件的方法,可能是这样做的(仅举个例子):
private static void readcachefile(uri cachefileuri) throws ioexception { bufferedreader reader = new bufferedreader(new filereader(cachefileuri.getpath())); string line; while ((line = reader.readline()) != null) { //todo: your code here } reader.close();}
但是就像文章开头的那个链接里所描述的问题一样,你可能会发现 context.getcachefiles() 总是返回null,也就是你无法读到cache文件。
这个问题有可能是这个bug造成的,你可以对比一下你的hadoop版本。
文章来源:http://www.codelast.com/
『3』解决办法
(1)打patch
(2)升级hadoop版本
(3)使用旧的distributedcache api,经测试ok
文章来源:http://www.codelast.com/
原文地址:[原创] hadoop 2.x的distributedcache无法工作的问题, 感谢原作者分享。
