DistributedCache使用

DistributedCache使用: 

1.import包 

Java代码   收藏代码
  1. import org.apache.hadoop.filecache.DistributedCache;  

2.加到Cache中 

Java代码   收藏代码
  1. DistributedCache.addCacheFile(new Path(args[++i]).toUri(), job.getConfiguration());  

3.Map或Reduce中调用 

Java代码   收藏代码
  1. Configuration conf = context.getConfiguration();  
  2.   
  3. Path [] pathwaysFiles = new Path[0];  
  4.   
  5. try {  
  6.   
  7. pathwaysFiles = DistributedCache.getLocalCacheFiles(conf);  
  8.   
  9. }catch (IOException ioe) {  
  10.   
  11. System.err.println("Caught exception while getting cached files: " + StringUtils.stringifyException(ioe));  
  12.   
  13. }  
  14.   
  15. for (Path pathwaysFile : pathwaysFiles) {  
  16.   
  17. try {  
  18.   
  19. BufferedReader fis = new BufferedReader(new FileReader(pathwaysFile.toString()));  
  20.   
  21. String pathway = null;  
  22.   
  23. while ((pathway = fis.readLine()) != null) {  
  24.   
  25. String [] p = pathway.split(" ");  
  26.   
  27. pathways.add(p);  
  28.   
  29. }  
  30.   
  31. }catch (IOException ioe) {  
  32.   
  33. }  
  34.   
  35. }  

DistributeCche的存储路径: 

Java代码   收藏代码
  1. /ifshk4/HDFS/hadoop/hadoop12/tmp/mapred/local/taskTracker/archive/compute-7-0.local/user/hadoop/kipu/expression_head.txt/expression_head.txt  

执行命令: 

Java代码   收藏代码
  1. /tmp/hadoop/bin/hadoop jar kipu.jar org.bgi.kipu.kipu /user/hadoop/kipu/expression_final.txt /user/hadoop/kipu/output/ -pathways /user/hadoop/kipu/pathways_final.txt -head /user/hadoop/kipu/expression_head.txt  


这个路径其实是当前map/reduce任务的本地路径再加上hdfs上的路径, 

因此当然会跟执行任务时输入的参数不一致啦。 

因此在DistributeCache中有多个文件的时候可以用以下方法判断: 

1.将参数的内容放到conf中去: 

Java代码   收藏代码
  1. job.getConfiguration().set("kipu.head.path", args[i]);  

2.用contains来判断 

Java代码   收藏代码
  1. pathwaysFile.toString().contains(conf.get("kipu.head.path"))  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值