hadoop2.4新api编程:Hadoop Tool,ToolRunner原理分析

原地址:https://www.aboutyun.com/thread-8302-1-1.html
问题导读:
1.Tool是接口还是类?
2.Tool继承了那个类?
3.Tool与ToolRunner的关系是什么?
4. Tool与ToolRunner作用分别是什么?




hadoop分为新旧api,由于hadoop目前最新版本2.4,本文是以hadoop2.4发布的api来进行分析的。

首先我们需要会查看源码,源码的查看,可以参考:如何通过eclipse查看、阅读hadoop2.4源码

我们首先查看接口Tool:
(Tool.java)
 
 
  1. @InterfaceAudience.Public
  2. @InterfaceStability.Stable
  3. public interface Tool extends Configurable {
  4.   /**
  5.    * Execute the command with the given arguments.
  6.    * 
  7.    * @param args command specific arguments.
  8.    * @return exit code.
  9.    * @throws Exception
  10.    */
  11.   int run(String [] args) throws Exception;
  12. }
复制代码
Tool接口继承了Configurable接口,只有一个run()方法。(接口继承接口)

继续自Configurable接口

 
 
  1. public interface Configurable {
  2.  
  3.   /** Set the configuration to be used by this object. */
  4.   void setConf(Configuration conf);
  5.  
  6.   /** Return the configuration used by this object. */
  7.   Configuration getConf();
  8. }
复制代码


Configurable接口只定义了两个方法:setConf与 getConf。

Configured类实现了Configurable接口:


 
 
  1. @InterfaceAudience.Public
  2. @InterfaceStability.Stable
  3. public class Configured implements Configurable {
  4.  
  5.   private Configuration conf;
  6.  
  7.   /** Construct a Configured. */
  8.   public Configured() {
  9.     this(null);
  10.   }
  11.   
  12.   /** Construct a Configured. */
  13.   public Configured(Configuration conf) {
  14.     setConf(conf);
  15.   }
  16.  
  17.   // inherit javadoc
  18.   @Override
  19.   public void setConf(Configuration conf) {
  20.     this.conf = conf;
  21.   }
  22.  
  23.   // inherit javadoc
  24.   @Override
  25.   public Configuration getConf() {
  26.     return conf;
  27.   }
  28.  
  29. }
复制代码
继承关系如下:
 
再看ToolRunner类的一部分:


下面两个是重载函数:
  1. public static int run(Configuration conf, Tool tool, String[] args) 
  2.     throws Exception{
  3.     if(conf == null) {
  4.       conf = new Configuration();
  5.     }
  6.     GenericOptionsParser parser = new GenericOptionsParser(conf, args);
  7.     //set the configuration back, so that Tool can configure itself
  8.     tool.setConf(conf);
  9.     
  10.     //get the args w/o generic hadoop args
  11.     String[] toolArgs = parser.getRemainingArgs();
  12.     return tool.run(toolArgs);
  13.   }
复制代码


 
  1. /**
  2.    * Runs the <code>Tool</code> with its <code>Configuration</code>.
  3.    * 
  4.    * Equivalent to <code>run(tool.getConf(), tool, args)</code>.
  5.    * 
  6.    * @param tool <code>Tool</code> to run.
  7.    * @param args command-line arguments to the tool.
  8.    * @return exit code of the {@link Tool#run(String[])} method.
  9.    */
  10.   public static int run(Tool tool, String[] args) 
  11.     throws Exception{
  12.     return run(tool.getConf(), tool, args);
  13.   }
复制代码
 


从上面两个ToolRunner的静态方法run()可以看到,处理hadoop的通用命令行参数,然后将args交给tool来处理,再由tool来运行自己的run方法。

这里在强调一下:以下面函数为准
  /**
   * Runs the given <code>Tool</code> by {@link Tool#run(String[])}, after 
   * parsing with the given generic arguments. Uses the given 
   * <code>Configuration</code>, or builds one if null.
   * 
   * Sets the <code>Tool</code>'s configuration with the possibly modified 
   * version of the <code>conf</code>.  
   * 
   * @param conf <code>Configuration</code> for the <code>Tool</code>.
   * @param tool <code>Tool</code> to run.
   * @param args command-line arguments to the tool.
   * @return exit code of the {@link Tool#run(String[])} method.
   */
  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 itself
    tool.setConf(conf);
    
    //get the args w/o generic hadoop args
    String[] toolArgs = parser.getRemainingArgs();
    return tool.run(toolArgs);
  }


Tool是一个接口,ToolRunner是一个类,ToolRunner类里面的run函数,如下
public static int run(Configuration conf, Tool tool, String[] args) 
    throws Exception{

.............................
    return tool.run(toolArgs);
}
这个函数把二者给结合起来了,也就是说我们ToolRunner的run方法本质是调用的tool的run方法。而run方法,则是BookCount类继承了tool,然后重写了
@Override
public int run(String[] args) throws Exception

这个run方法里,我们把job的各种设置由驱动主函数mian()移植到run方法中,然后驱动函数main()通过 ToolRunner.run(conf, new BookCount(), args);调用这个重写方法,代码可以参考下面






我们run重写run方法
 
  1. @Override
  2.         public int run(String[] args) throws Exception {
  3.  
  4.                 try {
  5.  
  6.                         //函数实现
  7.  
  8.                 } catch (Exception e) {
  9.  
  10.                         logger.error(e.getMessage());
  11.  
  12.                         e.printStackTrace();
  13.  
  14.                 }
  15.  
  16.                 return 0;
  17.  
  18.         }
复制代码

驱动主函数:
 
public class BookCount extends Configured implements Tool {

public static final Logger logger = Logger.getLogger(BookCount.class);

public static void main(String[] args) throws Exception {

PropertyConfigurator.configure("conf/log4j.properties");

logger.info("BookCountNew starting");

System.setProperty("HADOOP_USER_NAME", "hduser");

Configuration conf = new Configuration();

int res = ToolRunner.run(conf, new BookCount(), args);

logger.info("BookCountNew end");

System.exit(res);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值