Mapreduce中分布式缓存的使用注意点

job.addArchiveToClassPath(archive); // 缓存jar包到task运行节点的classpath中
job.addFileToClassPath(file); // 缓存普通文件到task运行节点的classpath中
job.addCacheArchive(uri); // 缓存压缩包文件到task运行节点的工作目录
job.addCacheFile(uri) // 缓存普通文件到task运行节点的工作目录

conf.set(“mapreduce.framework.name”, “local”);本地模式

job.addCacheFile(new URI(“file:/D:/srcdata/mapjoincache/pdts.txt#pdts.txt”));//添加缓存

使用时
本地模式测试
URI[] cacheFiles = context.getCacheFiles();
Path cacheFile = new Path(cacheFiles[0]);

BufferedReader br = new BufferedReader(new FileReader(cacheFile.toUri().getPath()));

集群模式时

conf.set("mapreduce.framework.name", "yarn");yarn模式
job.addCacheFile(new URI("hdfs://nameservice1/test/srcdata/mapjoincache/pdts.txt#pdts.txt"));//添加hdfs文件做缓存
protected void setup(Context context) throws IOException, InterruptedException {
   URI[] cacheFiles = context.getCacheFiles();
   Path cacheFile = new Path(cacheFiles[0]);
   System.out.println("分布式缓存"+cacheFile);
   System.out.println("分布式缓存路径"+cacheFile.toUri().getPath());
   //DistributedCache.getLocalCacheFiles(conf)
   //BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("pdts.txt")));
   //BufferedReader br = new BufferedReader(new FileReader("pdts.txt"));
   BufferedReader br = new BufferedReader(new FileReader(cacheFile.toUri().getPath()));
   String line;
   while (StringUtils.isNotEmpty(line = br.readLine())) {
    String[] fields = line.split(",");
    pdInfoMap.put(fields[0], fields[1]);
   }
   br.close();
  }
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值