Yarn(MapReduce 2.0)下分布式缓存(DistributedCache)的注意事项

1、问题

最近公司的集群从 Apache hadoop 0.20.203 升级到了 CDH 4,迈进了 Hadoop 2.0 的新时代,虽然新一代的 hadoop 努力做了架构、API 上的各种兼容, 但总有“照顾不周”的地方,下面说的这个有关分布式缓存的案例就是于此有关:一些 MR job 迁移到 Yarn 上后,发觉没数据了,而且没有报错。
查了下数据源和代码,发现是分布式缓存(DistributedCache)的用法有点小变化。以前的老代码大致如下:
(1)在 main 函数中添加分布式缓存文件:
...
String cacheFilePath = "/dsap/rawdata/cmc_unitparameter/20140308/part-m-00000";
DistributedCache.addCacheFile(new Path(cacheFilePath).toUri(), job.getConfiguration());
...
(2)在 MR 初始化的时候读取缓存文件做数据字典:
...
// 从当前作业中获取要缓存的文件
Path[] paths = DistributedCache.getLocalCacheFiles(context.getConfiguration());
for (Path path : paths) {
    if (path.toString().contains("cmc_unitparameter")) {
        ...
(3)结果:
这两段代码在 MR1 时代毫无问题,但是到了 MR2 时代 if 是永远为 false 的。
特意对比了下 MR1 和 MR2 时代的 path 格式,可以看到在 MRv2 下,Path 中不包含原始路径信息了:
MR1 Path:   hdfs://host:fs_port/dsap/rawdata/cmc_unitparameter/20140308/part-m-00000
MR1 Path:   hdfs://host:fs_port/dsap/rawdata/cmc_unitparameter/20140308/part-m-00000


MR2 Path:   /data4/yarn/local/usercache/root/appcache/application_1394073762364_1884/container_1394073762364_1884_01_000006/part-m-00000
MR2 Path:   /data17/yarn/local/usercache/root/appcache/application_1394073762364_1884/container_1394073762364_1884_01_000002/part-m-00000
MR2 Path:   /data23/yarn/local/usercache/root/appcache/application_1394073762364_1884/container_1394073762364_1884_01_000005/part-m-00000
看了上面两种差异我想你能明白为啥分布式缓存在 MR2 下面“失效了”。。。

2、解决方案

解决这个问题不难:

其实在 MR1 时代我们上面的代码是不够规范的,每次都遍历了整个分布式缓存,我们应该用到一个小技巧:createSymlink

(1)main 函数中为每个缓存文件添加符号链接:类似于 HTTP URL 的 # 锚点一样
...
String cacheFilePath = "/dsap/rawdata/cmc_unitparameter/20140308/part-m-00000";
Path inPath = new Path(cacheFilePath);
// # 号之后的名称是对上面文件的链接,不同文件的链接名不能相同,虽然由你自己随便取
String inPathLink=inPath.toUri().toString()+"#"+"DIYFileName";
DistributedCache.addCacheFile(new URI(inPathLink), job.getConfiguration());
...

加了软链接后,path 信息的最后部分就是你刚才的 DIYFileName:

/data4/yarn/local/usercache/root/appcache/application_1394073762364_1966/container_1394073762364_1966_01_000005/cmcs_paracontrolvalues
/data4/yarn/local/usercache/root/appcache/application_1394073762364_1966/container_1394073762364_1966_01_000005/cmc_unitparameter
(2)在需要用缓存文件的地方直接根据你刚才 # 后面自定义的文件名读取即可
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(new FileInputStream("DIYFileName")));
(3)其它地方的用法和代码与 MR1 无任何变化。

3、Refer:

1、Hadoop 多表 join:map side join 范例

http://my.oschina.net/leejun2005/blog/111963

2、Hadoop DistributedCache详解

http://dongxicheng.org/mapreduce-nextgen/hadoop-distributedcache-details/

3、迭代式MapReduce解决方案(二) DistributedCache

http://hongweiyi.com/2012/02/iterative-mapred-distcache/

4、DistributedCache小记

http://www.cnblogs.com/xuxm2007/p/3344930.html

转载于:https://my.oschina.net/leejun2005/blog/206341

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值