zoukankan      html  css  js  c++  java
  • Hadoop 分布式缓存简单应用

     
    Hadoop有一个叫做分布式缓存(distributed cache)的机制来将数据分发到集群上的所有节点上。为了节约网络带宽,在每一个作业中,各个文件通常只需要复制到一个节点一次。
    缓存文件复制位置:mapred-site.xml中
    <property>
    <name>mapred.local.dir</name>
    <value>/home/hadoop/tmp</value>
    </property>
     
    操作步骤:
    1.将数据的分发到每个节点上:
    DistributedCache.addCacheFile(new URI("hdfs://cloud01:9000/user/hadoop/mrinput/ST.txt"), conf);
    注意,此操作一定要在创建Job,将conf传递给Job之前进行,否则数据文件的路径不会被Mapper中取到。
    2.在每个Mapper中获取文件URI,再进行相关操作:
                URI[] uris=DistributedCache.getCacheFiles(context.getConfiguration());
     
    比如读取该文件:
           FileSystem fs = FileSystem.get(URI.create("hdfs://cloud01:9000"), context.getConfiguration());
      FSDataInputStream in = null;
      in = fs.open(new Path(uris[0].getPath()));
      BufferedReader br=new BufferedReader(new InputStreamReader(in));
  • 相关阅读:
    学习进度02
    dataX windows10安装
    架构漫谈 阅读笔记03
    质量属性及战术
    架构漫谈 阅读笔记02
    2020.12.12收获
    2020.12.11收获
    2020.12.10收获
    2020.12.9收获
    2020.12.8收获
  • 原文地址:https://www.cnblogs.com/yanzhenxing/p/2664761.html
Copyright © 2011-2022 走看看