关于ToolRunner.run()方法运行Hadoop程序原理分析
文章开始把我喜欢的这句话送个大家:这个世界上还有什么比自己写的代码运行在一亿人的电脑上更酷的事情吗,如果有那就是让这个数字再扩大十倍
一、相关的类及接口解释
二.关于ToolRunner典型用法关于ToolRunner
ToolRunner与上图中的类、接口无任何的继承、实现关系,它只继承了Object,没实现任何接口。
ToolRunner可以方便的运行那些实现了Tool接口的类(调用其run(String[])方法,并通过GenericOptionsParser 可以方便的处理hadoop命令行参数。
它的典型用法即:
1、定义一个类(如上图中的MyClass),继承configured,实现Tool接口。
2、在main()方法中通过ToolRunner.run(...)方法调用上述类的run(String[]方法)
三.例子:
package org.apache.hadoop.util;
import java.io.PrintStream;
import org.apache.hadoop.conf.Configuration;
public class 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);
tool.setConf(conf);
String[] toolArgs = parser.getRemainingArgs();
return tool.run(toolArgs);
}
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);
}
}
加油吧,程序员!