Hadoop2.x从源码讲解作业配置

来源:互联网 发布:网络打印机重启后脱机 编辑:程序博客网 时间:2024/05/17 02:38

Job在新API的作业配置
Job 类继承了JobContextImpl 类实现了接口JobContext接口
Job提供了写setter方法,例如 setNumReduceTasks 设置reduce数量 setMapperClass 设置运行Mapper的类等等。用来这只任务在运行过程中的一些属性值,这个方法其实实现都是用conf调用setter来实现的,
而JobContext提供了一些getter方法,就是将变量和方法封装成上下文(context)类,当Job的task正在运行中的时候,这个接口只是一个可读接口,这个JobContext本身也继承了MRJobConfig这个类,MRJobConfig就是一些常量数量,用来记录一些参数的值,
而与此相对的JobContextImpl类,有一组geter方法,在Job类设置的属性值,我们可以在JobContextImpl中得到,这个也是当Job的task正在运行中的时候,这个类只是一个可读类

源码上分析
在Job类中我们可以看到,当我们写到 Job job = new Job(conf, “word count”);如果没有传入jobname 默认是空的。其实调用了本身类的一个构造函数,这个构造函数调用了this(conf) 和setJobName(jobName)方法,而这个this(conf)调用的是Job(Configuration conf) 这个构造函数,里面是实现是this(new JobConf(conf)),所有我们可以看出jobconf继承了configuration类,这个jobconf就是将job中的方法实现体实现了。

@Deprecated  public Job(Configuration conf) throws IOException {    this(new JobConf(conf));  }  /**   * @deprecated Use {@link #getInstance(Configuration, String)}   */  @Deprecated  public Job(Configuration conf, String jobName) throws IOException {    this(conf);    setJobName(jobName);  }

Job类和Jobconf类的结合实现方法

Job类中

  /**   * Set the {@link Mapper} for the job.   * @param cls the <code>Mapper</code> to use   * @throws IllegalStateException if the job is submitted   */  public void setMapperClass(Class<? extends Mapper> cls                             ) throws IllegalStateException {    ensureState(JobState.DEFINE);    conf.setClass(MAP_CLASS_ATTR, cls, Mapper.class);  }

Jobconf类中 setMapperClass 中调用自己的setClass方法,这个方法调用了父类configuration 中的set方法,父类中set的方法的实现,可以去查看configuration中的源码,其实就是将值写入对应的属性中,例如这个MapperClass这个设置Mapper运行的类,这个最终我们可以再MRJobConfig 这个类中的MAP_CLASS_ATTR 属性中得到值

public void setMapperClass(Class<? extends Mapper> theClass) {    setClass("mapred.mapper.class", theClass, Mapper.class);  }public void setClass(String name, Class<?> theClass, Class<?> xface) {    if (!xface.isAssignableFrom(theClass))      throw new RuntimeException(theClass+" not "+xface.getName());    set(name, theClass.getName());  }
0 0