大数据技术入门:MapReduce(分布式计算框架),帮你解决95%以上的问题

23 阅读2分钟
  • MapReduce 编程模型只包含 Map 和 Reduce 两个过程,map 的主要输入是一对 <key,value> 值,经过 map 计算后输出一对 <key,value> 值;然后将相同 Key 合并,形成 <key,value> 集合;再将这个<key,value 集合>输入 reduce,经过计算输出零个或多个 <key,value> 对。

二、MapReduce工作原理

============================================================================

在这里插入图片描述

大数据应用进程(提交任务的客户端):

该进程是启动 MapReduce 程序的主入口,主要是指定 Map 和 Reduce 类、输入输出文件路径等,并提交作业给 Hadoop 集群

JobTracker进程:

Hadoop 集群常驻进程,根据要处理的输入数据量,命令 TaskTracker生成相应数量的Map和Reduce进程任务,并管理这个作业生命周期的任务的调度和监控

TaskTracker进程:

负责管理 Map 进程和 Reduce 进程。Hadoop 集群中绝大多数服务器同时运行 DataNode 进程和 TaskTracker 进程

三、MapReduce操作

==========================================================================

运维操作:


 hadoop    jar    jar包路径   入口程序类名   输入文件的hdfs目录     输出文件的hdfs目录



四、案例

=================================================================


public class WordCount {

  public static class TokenizerMapper  extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);

    private Text word = new Text();

    public void map(Object key, Text value, Context context

                    ) throws IOException, InterruptedException {

      StringTokenizer itr = new StringTokenizer(value.toString());

      while (itr.hasMoreTokens()) {

        word.set(itr.nextToken());

        //针对每个单词输出一个<word ,1>

        //MapReduce 计算框架会将这些<word ,1>收集起来,将相同的word放一起,形成

        //<word,<1,1,1,...>>这样的<key,value集合>,然后输入给reduce

        context.write(word, one);

      }

    }

  }

 

  public static class IntSumReducer    extends Reducer<Text,IntWritable,Text,IntWritable> {

    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values, Context context

                       ) throws IOException, InterruptedException {

      int sum = 0;

      for (IntWritable val : values) {

          //reduce对每个word对应的所有1 进行求和,最终将<word,合计>输出

        sum += val.get();

      }

      result.set(sum);

      context.write(key, result);

    }

  }

}



在这里插入图片描述

img img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!