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 源码笔记
最新推荐文章于 2024-07-26 17:20:59 发布