MapReduce的数据流程:

    1. 预先加载本地的输入文件
    2. 经过MAP处理产生中间结果
    3. 经过shuffle程序将相同key的中间结果分发到同一节点上处理
    4. Recude处理产生结果输出
    5. 将结果输出保存在hdfs上

MAP

在map阶段,使用job.setInputFormatClass定义的InputFormat将输入的数据集分割成小数据块splits, 
同时InputFormat提供一个RecordReder的实现。默认的是TextInputFormat, 
他提供的RecordReder会将文本的一行的偏移量作为key,这一行的文本作为value。 
这就是自定义Map的输入是<longwritable, text="">的原因。 
然后调用自定义Map的map方法,将一个个<longwritable, text="">对输入给Map的map方法。

最终是按照自定义的MAP的输出key类,输出class类生成一个List<mapoutputkeyclass, mapoutputvalueclass="">。

Partitioner

在map阶段的最后,会先调用job.setPartitionerClass设置的类对这个List进行分区, 
每个分区映射到一个reducer。每个分区内又调用job.setSortComparatorClass设置的key比较函数类排序。

可以看到,这本身就是一个二次排序。 
如果没有通过job.setSortComparatorClass设置key比较函数类,则使用key的实现的compareTo方法。

Shuffle:

将每个分区根据一定的规则,分发到reducer处理

Sort

在reduce阶段,reducer接收到所有映射到这个reducer的map输出后, 
也是会调用job.setSortComparatorClass设置的key比较函数类对所有数据对排序。 
然后开始构造一个key对应的value迭代器。这时就要用到分组, 
使用jobjob.setGroupingComparatorClass设置的分组函数类。只要这个比较器比较的两个key相同, 
他们就属于同一个组,它们的value放在一个value迭代器

Reduce 
最后就是进入Reducer的reduce方法,reduce方法的输入是所有的(key和它的value迭代器)。 
同样注意输入与输出的类型必须与自定义的Reducer中声明的一致。

具体的例子:

是hadoop mapreduce example中的例子,自己改写了一下并加入的注释

 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
 import java.util.StringTokenizer;

 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.LongWritable;
 import org.apache.hadoop.io.RawComparator;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.WritableComparable;
 import org.apache.hadoop.io.WritableComparator;
 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
 import org.apache.hadoop.mapreduce.Job;
 import org.apache.hadoop.mapreduce.Mapper;
 import org.apache.hadoop.mapreduce.Partitioner;
 import org.apache.hadoop.mapreduce.Reducer;
 import org.apache.hadoop.util.GenericOptionsParser;

 import com.catt.cdh.mr.example.SecondarySort2.FirstPartitioner;
 import com.catt.cdh.mr.example.SecondarySort2.Reduce;

 /**
  * This is an example Hadoop Map/Reduce application.
  * It reads the text input files that must contain two integers per a line.
  * The output is sorted by the first and second number and grouped on the
  * first number.
  *
  * To run: bin/hadoop jar build/hadoop-examples.jar secondarysort
  * <i>in-dir</i> <i>out-dir</i>  */
 public class SecondarySort {

     /**
      * Define a pair of integers that are writable.
      * They are serialized in a byte comparable format.
      */
     public static class IntPair implements WritableComparable<intpair> {
         private int first = 0;
         private int second = 0;

         /**
          * Set the left and right values.
          */
         public void set(int left, int right) {
             first = left;
             second = right;
         }

         public int getFirst() {
             return first;
         }

         public int getSecond() {
             return second;
         }

         /**
          * Read the two integers.
          * Encoded as: MIN_VALUE -> 0, 0 -> -MIN_VALUE, MAX_VALUE-> -1
          */
         @Override
         public void readFields(DataInput in) throws IOException {
             first = in.readInt() + Integer.MIN_VALUE;
             second = in.readInt() + Integer.MIN_VALUE;
         }

         @Override
         public void write(DataOutput out) throws IOException {
             out.writeInt(first - Integer.MIN_VALUE);
             out.writeInt(second - Integer.MIN_VALUE);
         }

         @Override
         // The hashCode() method is used by the HashPartitioner (the default
         // partitioner in MapReduce)
         public int hashCode() {
             return first * 157 + second;
         }

         @Override
         public boolean equals(Object right) {
             if (right instanceof IntPair) {
                 IntPair r = (IntPair) right;
                 return r.first == first && r.second == second;
             } else {
                 return false;
             }
         }

         /** A Comparator that compares serialized IntPair. */
         public static class Comparator extends WritableComparator {
             public Comparator() {
                 super(IntPair.class);
             }

             // 针对key进行比较,调用多次
             public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2,
                     int l2) {
                 return compareBytes(b1, s1, l1, b2, s2, l2);
             }
         }

         static {
             // 注意:如果不进行注册,则使用key.compareTo方法进行key的比较
             // register this comparator
             WritableComparator.define(IntPair.class, new Comparator());
         }

         // 如果不注册WritableComparator,则使用此方法进行key的比较
         @Override
         public int compareTo(IntPair o) {
             if (first != o.first) {
                 return first < o.first ? -1 : 1;
             } else if (second != o.second) {
                 return second < o.second ? -1 : 1;
             } else {
                 return 0;
             }
         }
     }

     /**
      * Partition based on the first part of the pair.
      */
     public static class FirstPartitioner extends
             Partitioner<intpair, intwritable=""> {
         @Override
         public int getPartition(IntPair key, IntWritable value,
                 int numPartitions) {
             return Math.abs(key.getFirst() * 127) % numPartitions;
         }
     }

     /**
      * Compare only the first part of the pair, so that reduce is called once
      * for each value of the first part.
      */
     public static class FirstGroupingComparator implements
             RawComparator<intpair> {

         // 针对key调用,调用多次
         @Override
         public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
             return WritableComparator.compareBytes(b1, s1, Integer.SIZE / 8,
                     b2, s2, Integer.SIZE / 8);
         }

         // 没有监控到被调用,不知道有什么用
         @Override
         public int compare(IntPair o1, IntPair o2) {
             int l = o1.getFirst();
             int r = o2.getFirst();
             return l == r ? 0 : (l < r ? -1 : 1);
         }
     }

     /**
      * Read two integers from each line and generate a key, value pair
      * as ((left, right), right).
      */
     public static class MapClass extends
             Mapper<longwritable, text,="" intpair,="" intwritable=""> {

         private final IntPair key = new IntPair();
         private final IntWritable value = new IntWritable();

         @Override
         public void map(LongWritable inKey, Text inValue, Context context)
                 throws IOException, InterruptedException {
             StringTokenizer itr = new StringTokenizer(inValue.toString());
             int left = 0;
             int right = 0;
             if (itr.hasMoreTokens()) {
                 left = Integer.parseInt(itr.nextToken());
                 if (itr.hasMoreTokens()) {
                     right = Integer.parseInt(itr.nextToken());
                 }
                 key.set(left, right);
                 value.set(right);
                 context.write(key, value);
             }
         }
     }

     /**
      * A reducer class that just emits the sum of the input values.
      */
     public static class Reduce extends
             Reducer<intpair, intwritable,="" text,="" intwritable=""> {
         private static final Text SEPARATOR = new Text(
                 "------------------------------------------------");
         private final Text first = new Text();

         @Override
         public void reduce(IntPair key, Iterable<intwritable> values,
                 Context context) throws IOException, InterruptedException {
             context.write(SEPARATOR, null);
             first.set(Integer.toString(key.getFirst()));
             for (IntWritable value : values) {
                 context.write(first, value);
             }
         }
     }

     public static void main(String[] args) throws Exception {
         Configuration conf = new Configuration();
         String[] ars = new String[] { "hdfs://data2.kt:8020/test/input",
                 "hdfs://data2.kt:8020/test/output" };
         conf.set("fs.default.name", "hdfs://data2.kt:8020/");

         String[] otherArgs = new GenericOptionsParser(conf, ars)
                 .getRemainingArgs();
         if (otherArgs.length != 2) {
             System.err.println("Usage: secondarysort <in> <out>");
             System.exit(2);
         }
         Job job = new Job(conf, "secondary sort");
         job.setJarByClass(SecondarySort.class);
         job.setMapperClass(MapClass.class);

         // 不再需要Combiner类型,因为Combiner的输出类型<text, intwritable="">对Reduce的输入类型<intpair, intwritable="">不适用
         // job.setCombinerClass(Reduce.class);
         // Reducer类型
         job.setReducerClass(Reduce.class);
         // 分区函数
         job.setPartitionerClass(FirstPartitioner.class);
         // 设置setSortComparatorClass,在partition后,
         // 每个分区内又调用job.setSortComparatorClass设置的key比较函数类排序
         // 另外,在reducer接收到所有映射到这个reducer的map输出后,
         // 也是会调用job.setSortComparatorClass设置的key比较函数类对所有数据对排序
         // job.setSortComparatorClass(GroupingComparator2.class);
         // 分组函数

         job.setGroupingComparatorClass(FirstGroupingComparator.class);

         // the map output is IntPair, IntWritable
         // 针对自定义的类型,需要指定MapOutputKeyClass
         job.setMapOutputKeyClass(IntPair.class);
         // job.setMapOutputValueClass(IntWritable.class);

         // the reduce output is Text, IntWritable
         job.setOutputKeyClass(Text.class);
         job.setOutputValueClass(IntWritable.class);

         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
         System.exit(job.waitForCompletion(true) ? 0 : 1);
     }

 }</intpair,></text,></out></in></intwritable></intpair,></longwritable,></intpair></intpair,></intpair>

转载自: http://www.open-open.com/lib/view/open1385602212718.html

MapReduce的数据流程、执行流程的更多相关文章

  1. MapReduce On Yarn的执行流程

    1.概述 Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,而MapReduce等运算程序则相当于运行于操作系统之上的应用程序. Yarn的架构如下图所示: ...

  2. MapReduce架构与执行流程

    一.MapReduce是用于解决什么问题的? 每一种技术的出现都是用来解决实际问题的,否则必将是昙花一现,那么MapReduce是用来解决什么实际的业务呢? 首先来看一下MapReduce官方定义: ...

  3. MySQL更新数据时,日志(redo log、binlog)执行流程

    1:背景 项目需要做Es和数据库的同步,而手动在代码中进行数据同步又是Es的一些不必要的数据同步操作和业务逻辑耦合,所以使用的了读取mysql的binlog日志的方式进行同步Es的数据. 问题1:根据 ...

  4. MapReduce执行流程及程序编写

    MapReduce 一种分布式计算模型,解决海量数据的计算问题,MapReduce将计算过程抽象成两个函数 Map(映射):对一些独立元素(拆分后的小块)组成的列表的每一个元素进行指定的操作,可以高度 ...

  5. MapReduce作业的执行流程

    MapReduce任务执行总流程 一个MapReduce作业的执行流程是:代码编写 -> 作业配置 -> 作业提交 -> Map任务的分配和执行 -> 处理中间结果 -> ...

  6. [Hadoop]浅谈MapReduce原理及执行流程

    MapReduce MapReduce原理非常重要,hive与spark都是基于MR原理 MapReduce采用多进程,方便对每个任务资源控制和调配,但是进程消耗更多的启动时间,因此MR时效性不高.适 ...

  7. 关于大数据T+1执行流程

    关于大数据T+1执行流程 前提: 搭建好大数据环境(hadoop hive hbase sqoop zookeeper oozie hue) 1.将所有数据库的数据汇总到hive (这里有三种数据源 ...

  8. 大数据学习day23-----spark06--------1. Spark执行流程(知识补充:RDD的依赖关系)2. Repartition和coalesce算子的区别 3.触发多次actions时,速度不一样 4. RDD的深入理解(错误例子,RDD数据是如何获取的)5 购物的相关计算

    1. Spark执行流程 知识补充:RDD的依赖关系 RDD的依赖关系分为两类:窄依赖(Narrow Dependency)和宽依赖(Shuffle Dependency) (1)窄依赖 窄依赖指的是 ...

  9. Hive SQL执行流程分析

    转自 http://www.tuicool.com/articles/qyUzQj 最近在研究Impala,还是先回顾下Hive的SQL执行流程吧. Hive有三种用户接口: cli (Command ...

随机推荐

  1. 添加数据库的Maven依赖(SqlServer,Oracle)

    oracle: 1.在Oracle官网下载ojdbc的jar包 例:ojdbc7.jar,版本是12.1.0.2,存储地址/home/peng/下载 2.dos中进入存储地址执行如下命令行(注意各项对 ...

  2. Harry Potter

    Names appearing in "Harry Potter" 1.Harry Potter ①Harry is from Henry. ②Harry is related t ...

  3. [算法] get_lucky_price price

    int get_lucky_price(int price, const vector & number) 题意大概是给你一个数price,比如1000,然后有unlucky_num,有{1, ...

  4. Passbook教程中生成pass时遇到的“Couldn't find a passTypeIdentifier in the pass”

    报错如下: 2014-03-28 15:19:17.990 signpass[6358:507] Couldn't find a passTypeIdentifier in the pass 解决方案 ...

  5. 关于C#虚函数和构造函数的一点理解

    虚函数感觉总是很神秘,在本质的原理上一直也没有弄得很透彻,今天又有一点的新的感悟,纪录下来,有时间的话可以去研究一下C++对象模型 using System; using System.Collect ...

  6. PHOTOSHOP 制作虚线和实线

    1.制作实线可以直接用直线工具,选择合适的粗细大小. 2. 制作虚线首先要用钢笔或者绘图工具画出所需要的形状,如弧线,圆形等等     然后在路径面板中用画笔描边,画笔需要提前设置好粗细和间距,用方形 ...

  7. mysqldump备份与还原mysql数据的实例

    有关mysql数据库的备份与还原,我们一般用下面两种方式来处理:1.使用into outfile 和 load data infile导入导出备份数据 本文原始链接:http://www.jbxue. ...

  8. Android开发第1篇 - Android开发环境搭建

    归结一下,需要进行Android开发所需要的工具或软件: Eclipse - Android是基于JAVA的开发,所以选用目前来说使用较高的Eclipse作为IDE. ADT (Android Dev ...

  9. 从Windows远程Ubuntu

    关键字:Windows,Ubuntu,Putty,WinSCP OS:Windows 7,Ubuntu. 1.下载Putty:http://www.putty.org/. 2.双击运行putty.ex ...

  10. codeforces 615D - Multipliers

    Multipliers 题意:给定一个2e5范围内的整数m,之后输入m个2e5内的素数(当然可以重复了),问把这些输入的素数全部乘起来所得的数的约数的乘积mod(1e9+7)等于多少? 思路:对题目样 ...