MRv1到MRv2

来源:互联网 发布:linux mac地址 编辑:程序博客网 时间:2024/05/20 23:04

概述

引入YARN作为通用资源调度平台后,Hadoop得以支持多种计算框架,如MapReduce、Spark、Storm等。MRv1是Hadoop1中的MapReduce,MRv2是Hadoop2中的MapReduce。下面是MRv1和MRv2之间的一些基本变化:

  1. MRv1包括三个部分:运行时环境(jobtracker和tasktracker)、编程模型(MapReduce)、数据处理引擎(Map任务和Reduce任务)
  2. MRv2中,重用了MRv1中的编程模型和数据处理引擎。但是运行时环境被重构了。jobtracker被拆分成了通用的资源调度平台YARN和负责各个计算框架的任务调度模型AM。
  3. MRv1中任务是运行在Map slot和Reduce slot中的,计算节点上的Map slot资源和Reduce slot资源不能重用。而MRv2中任务是运行在container中的,map任务结束后,相应container结束,空闲出来的资源可以让reduce使用。

MRv2参数配置

MRv2上的参数可以参考官方文档进行配置,但是在mapred-site.xml中有一个参数需要注意:mapreduce.job.user.classpath.first,本文推荐将其配置成true。如果不配置该参数的话,在执行jar程序的时候,系统会优先选择Hadoop框架中已经存在的java类而不是用户指定包中自己编写的java类

新旧API

  1. MapReduce新旧API是指org.apache.hadoop.mapred包(旧包)和org.apache.hadoop.mapreduce包(新包)。
  2. 这两个包是指MapReduce的编程API发生了变化,旧包中的Map和Reduce是通过接口实现的,而新包中的Map和Reduce是通过继承实现的,这两个包在MRv1中就存在了。
  3. MRv2中mapred包的API基本没发生变化,以前通过MRv1编译过的程序基本可以重新运行;但是mapreduce包的API发生了变化,通过MRv1编译过的程序需要重新编译,甚至需要修改代码才能运行
  4. 推荐使用新API进行编程

MapReduce包wordcount事例

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 {        //context.nextKeyValue()      StringTokenizer itr = new StringTokenizer(value.toString());      while (itr.hasMoreTokens()) {        word.set(itr.nextToken());        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) {        sum += val.get();      }      result.set(sum);      context.write(key, result);    }  }  public static void main(String[] args) throws Exception {    Configuration conf = new Configuration();    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();    if (otherArgs.length != 2) {      System.err.println("Usage: wordcount <in> <out>");      System.exit(2);    }    Job job = new Job(conf, "word count");    job.setJarByClass(WordCount.class);    job.setMapperClass(TokenizerMapper.class);    job.setCombinerClass(IntSumReducer.class);    job.setReducerClass(IntSumReducer.class);    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);  }}

参考文献

  1. http://book.51cto.com/art/201312/422025.htm
  2. http://hadoop.apache.org/docs/r2.4.0/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduce_Compatibility_Hadoop1_Hadoop2.html
1 0