(5-3)Mapper源码分析

来源:互联网 发布:不同数据类型运算vb中 编辑:程序博客网 时间:2024/06/07 06:48


//一个4个方法:setup()、map()、cleanup()、run()。
//setup()在map任务之前执行,clean()在map任务之后执行,run()方法控制这些函数执行,run()方法由框架调用。
//代码中给出了一个map()实例。
package org.apache.hadoop.mapreduce; * <p>Example:</p> * <p><blockquote><pre> * public class TokenCounterMapper  *     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()); *       context.write(word, one); *     } *   } * }@InterfaceAudience.Public@InterfaceStability.Stablepublic class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> {  public abstract class Context    implements MapContext<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {  }    protected void setup(Context context                       ) throws IOException, InterruptedException {    // NOTHING  }  @SuppressWarnings("unchecked")  protected void map(KEYIN key, VALUEIN value,                      Context context) throws IOException, InterruptedException {    context.write((KEYOUT) key, (VALUEOUT) value);  }  protected void cleanup(Context context                         ) throws IOException, InterruptedException {    // NOTHING  }    public void run(Context context) throws IOException, InterruptedException {    setup(context);    try {      while (context.nextKeyValue()) {        map(context.getCurrentKey(), context.getCurrentValue(), context);      }    } finally {      cleanup(context);    }  }}


0 0