hadoop 源码笔记

public interface Tool extends Configurable {
  /*
   实现Tool接口可以获得hadoop命令行参数,通过run方法的args传入
   */
  int run(String [] args) throws Exception;
}
/***************************MyApp实现了Tool接口的类************************************/
public class MyApp extends Configured implements Tool {
     
       public int run(String[] args) throws Exception {
  //返回父类Configured保存的conf实例(已经被ToolRunner改过了,加入了从命令行获取的配置项)
         Configuration conf = getConf();
         
         // Create a JobConf using the processed conf
   //第一个参数为配置项,第二个为将来要分发的jobJar
         JobConf job = new JobConf(conf, MyApp.class);
         
         // Process custom command-line options
         Path in = new Path(args[1]);
         Path out = new Path(args[2]);
         
         // Specify various job-specific parameters     
         job.setJobName("my-app");
         job.setInputPath(in);
         job.setOutputPath(out);
         job.setMapperClass(MyApp.MyMapper.class);
         job.setReducerClass(MyApp.MyReducer.class);

         // Submit the job, then poll for progress until the job is complete
   //jobJobClient是我们的job 跟 JobTracker交互的接口
   JobClient.runJob(job);
       }
       
       public static void main(String[] args) throws Exception {
         // 调用ToolRunner的run方法(ToolRunner的run方法内部又调用了MyApp的run方法)
   //即ToolRunner只是转换了参数,把参数的设置项加入到configuration里,再运行
         int res = ToolRunner.run(new Configuration(), new MyApp(), args);
         
         System.exit(res);
       }
     }
/***************************ToolRunner运行实现了Tool接口的类************************************/
public class ToolRunner {
 /* 
 conf Configuration for the Tool.
 tool Tool to run.
 args command-line arguments to the tool.
 */
  public static int run(Configuration conf, Tool tool, String[] args) 
    throws Exception{
    if(conf == null) {
      conf = new Configuration();
    }
 //转换命令行参数args为配置项,并设置到conf里面
    GenericOptionsParser parser = new GenericOptionsParser(conf, args);
    //重新设置conf到tool里面,即改变了tool的设置
    tool.setConf(conf);
    
    //取得没有转换的参数 hadoop args
    String[] toolArgs = parser.getRemainingArgs();
    //调用tool的run方法
 return tool.run(toolArgs);
  }
  
  /*
 根据Tool自己的配置运行
   */
  public static int run(Tool tool, String[] args) 
    throws Exception{
    return run(tool.getConf(), tool, args);
  }
  
  public static void printGenericCommandUsage(PrintStream out) {
    GenericOptionsParser.printGenericCommandUsage(out);
  }
  
}
/********************Configured类实现了可配置接口,并保存了一个conf实例**************************/
public class Configured implements Configurable {

  //保存了一个conf实例
  private Configuration conf;

  /** Construct a Configured. */
  public Configured() {
    this(null);
  }

  public Configured(Configuration conf) {
    setConf(conf);
  }

  public void setConf(Configuration conf) {
    this.conf = conf;
  }
  //返回保存的conf实例
  public Configuration getConf() {
    return conf;
  }

}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值