项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

在用java写MR的时候,定义类的第一行一般都是如下方式:

public class XXX extends Configured implements Tool

run方法的一个实例如下:

public int run(String[] args) throws Exception {Configuration conf = getConf();GenericOptionsParser optionparser = new GenericOptionsParser(conf, args);conf = optionparser.getConfiguration();@SuppressWarnings("deprecation")Job job = new Job(conf, "JudgeIfOrder");.........return (job.waitForCompletion(true) ? 0 : 1);}

main方法如下:

 public static void main(String[] args) throws Exception {int res = ToolRunner.run(new Configuration(), new JudgeIfOrder(),args);System.exit(res);}

为什么要这么写?这些类与接口内部是怎样实现的?他们之间是什么关系?相信不少小伙伴都对此会有疑问。为此,我结合相关源码,试图为大家缕缕hadoop里的作业具体是怎样配置的。这些Configured,Configrable,Tool等等又都是些什么鬼。

首先上一部分源码
Configurable接口

@InterfaceAudience.Public
@InterfaceStability.Stable
public interface Configurable {/** Set the configuration to be used by this object. */void setConf(Configuration conf);/** Return the configuration used by this object. */Configuration getConf();
}

Configured类

/** Base class for things that may be configured with a {@link Configuration}. */
@InterfaceAudience.Public
@InterfaceStability.Stable
public class Configured implements Configurable {private Configuration conf;/** Construct a Configured. */public Configured() {this(null);}/** Construct a Configured. */public Configured(Configuration conf) {setConf(conf);}// inherit javadoc@Overridepublic void setConf(Configuration conf) {this.conf = conf;}// inherit javadoc@Overridepublic Configuration getConf() {return conf;}

Tool接口:

@InterfaceAudience.Public
@InterfaceStability.Stable
public interface Tool extends Configurable {/*** Execute the command with the given arguments.* * @param args command specific arguments.* @return exit code.* @throws Exception*/int run(String [] args) throws Exception;
}

从源码上来看,首先我们可以明确的一点是:
Configurable是这三者底层的接口,Configured类简单地实现了Configurable接口,而Tool接口继承了Configurable接口。

run方法中,第一句是

Configuration conf = getConf();

因为类XXX已经继承了Configured,所以实际上是调用Configured里的getConf()方法,得到了一个Configuration对象。 Configuration类是配置模块中的最底层的类,从它的package信息就可以看出:

package org.apache.hadoop.conf;

Configuration到底有多底层?咱们看看它源码的一小部分:

  static{//print deprecation warning if hadoop-site.xml is found in classpathClassLoader cL = Thread.currentThread().getContextClassLoader();if (cL == null) {cL = Configuration.class.getClassLoader();}if(cL.getResource("hadoop-site.xml")!=null) {LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +"Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "+ "mapred-site.xml and hdfs-site.xml to override properties of " +"core-default.xml, mapred-default.xml and hdfs-default.xml " +"respectively");}addDefaultResource("core-default.xml");addDefaultResource("core-site.xml");}

同学们请看最后两行,它会加载core-default.xml与core-site.xml这两个最基础的配置文件。

GenericOptionsParser这个类,一看就知道是解析参数用的。

       GenericOptionsParser optionparser = new GenericOptionsParser(conf, args);conf = optionparser.getConfiguration();

上面这两行代码,自然就是用来解析输入参数用的。。。

咱们来看最后一行代码

int res = ToolRunner.run(new Configuration(), new JudgeIfOrder(),args);

上ToolRunner的源码

public static int run(Configuration conf, Tool tool, String[] args) throws Exception{if(conf == null) {conf = new Configuration();}GenericOptionsParser parser = new GenericOptionsParser(conf, args);//set the configuration back, so that Tool can configure itselftool.setConf(conf);//get the args w/o generic hadoop argsString[] toolArgs = parser.getRemainingArgs();return tool.run(toolArgs);}public static int run(Tool tool, String[] args) throws Exception{return run(tool.getConf(), tool, args);}

ToolRunner中与run相关的方法是这两个。我们的代码里给run方法传了三个参数,实际上执行的就是ToolRunner里第一个run方法了,ToolRunner会调用tool接口中的run方法。而我们自己写的XXX类实现了tool接口,重写了run方法。所以最后实际上调用的是我们自己写的run()方法了!

写到这里,同学们应该差不多明白里头的来龙去脉了吧。。。

在stackoverflow里看到了一段相关的描述,直接copy过来,就不翻译了,请自行阅读

hadoop Configured Configrable Configuration Tool 源码详解相关推荐

  1. Mapreduce源码分析(一):FileInputFormat切片机制,源码详解

    FileInputFormat切片机制,源码详解 1.InputFormat:抽象类 只有两个抽象方法 public abstract List<InputSplit> getSplits ...

  2. Integer源码详解

    尊重原创,转载请标明出处    http://blog.csdn.net/abcdef314159 对于Integer这个类估计大家也都非常熟悉了,以前看过他的源码,但也只是粗略的看了一下,最近有时间 ...

  3. Spring事务源码详解

    一. 简介 事务: 事务是逻辑上的一组操作,要么都执行,要么都不执行,关于事务的基本知识可以看我的这篇文章:事务的基础知识 Spring事务: Spring 支持两种方式的事务管理:编程式事务管理.声 ...

  4. 源码详解Android 9.0(P) 系统启动流程之SystemServer

    源码详解Android 9.0(P) 系统启动流程目录: 源码详解Android 9.0(P)系统启动流程之init进程(第一阶段) 源码详解Android 9.0(P)系统启动流程之init进程(第 ...

  5. 【Live555】live555源码详解(九):ServerMediaSession、ServerMediaSubsession、live555MediaServer

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的三个类所在的位置: ServerMediaSession.ServerMediaSubsession.Dy ...

  6. 【Live555】live555源码详解系列笔记

    [Live555]liveMedia下载.配置.编译.安装.基本概念 [Live555]live555源码详解(一):BasicUsageEnvironment.UsageEnvironment [L ...

  7. 【Live555】live555源码详解(八):testRTSPClient

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的testRTSPClient实现的三个类所在的位置: ourRTSPClient.StreamClient ...

  8. 【Live555】live555源码详解(七):GenericMediaServer、RTSPServer、RTSPClient

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的三个类所在的位置: GenericMediaServer.RTSPServer.RTSPClient 14 ...

  9. 【Live555】live555源码详解(六):FramedSource、RTPSource、RTPSink

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的三个类所在的位置: FramedSource.RTPSource.RTPSink 11.FramedSou ...

  10. 【Live555】live555源码详解(五):MediaSource、MediaSink、MediaSession、MediaSubsession

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的四个类所在的位置: MediaSource.MediaSink.MediaSession.MediaSub ...

最新文章

  1. wordpress php教程 pdf,wordpress二次开发全能教程.pdf
  2. 移动游戏市场Testin云测占有率超过90%
  3. linux mysql timestamp_MySQL时间类型Timestamp和Datetime 的深入理解
  4. 【转载】简直可爱极了的即时通讯
  5. 灵格斯怎么屏幕取词_灵格斯词霸(Lingoes)基础使用教程
  6. iozone测试文件系统IO性能
  7. Froala编辑器使用经历
  8. BI—SSIS初步认识
  9. 昭阳E43G/K43G升级T9600失败,蓝屏
  10. Spring - 解决 SpringUtil getBean NPE 问题
  11. 去掉任务栏中SATA硬盘的安全删除硬件图标
  12. 剑灵革命android汉化教程,剑灵革命手游中文界面怎么设置?台服中文界面设置教程...
  13. 我,单身沪漂,想有只猫
  14. 【java之汉字转拼音】
  15. poi设置表格内容水平垂直居中
  16. [向量] 点积应用-两个向量夹角
  17. 芝村乡大学生投资理财
  18. 程序员就业和发展前景?
  19. 如何利用 GitHub 从零开始搭建一个博客
  20. 交通或制图相关英文缩写记录(持续更新)

热门文章

  1. SQLLDR载数加速,优化参数
  2. java 线程的执行原理
  3. Referenced file contains errors
  4. Ice_cream’s world III(prime)
  5. scite editor on mac
  6. FMS3 客户端call服务器端
  7. [照片]51cto众生相
  8. 解决“HTTP Error 401 – Unauthorized”
  9. 内存管理学习之内存寻址
  10. oracle 偶尔登录超时,OracleClient,间歇性连接问题:应用程序挂起在OracleConnection.Open()上,没有超时,没有引发异常...