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;
}
}
hadoop 源碼筆記
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.