关于ToolRunner的典型实现方法
关于ToolRunner
1.该类没有继承任何类,也没有实现任何的接口
2.该类可以运行那些实现可Tool接口的应用程序,其实现是通过调用类中的run()方法
3.run()方法有两种形式:
public static int run(Configuration conf, Tool tool, String[] args) ;
public static int run(Tool tool, String[] args) ;
第二种方法最终还是调用了第一种run()方法;
4.ToolRunner完成以下两个功能:
(1)为Tool创建一个Configuration对象.
(2)根据客户端所提交的命令行为程序设置运行参数
在run()方法中使用了一个GenericOptionsParser 的类用来读取客户端命令行参数.
关于Configuration类
1.默认情况下Hadoop会自动的加载core-default.xml及core-site.xml中的参数.
在hadoop的Configuration.java中包含加载以上文件的代码,Configuration类中有大量的add()方法,用来设置运行参数:
static{
//print deprecation warning if hadoop-site.xml is found in classpath
ClassLoader cL = Thread.currentThread().getContextClassLoader();
if (cL == null) {
cL = Configuration.class.getClassLoader();
}
if(cL.getResource("hadoop-site.xml")!=null) {
LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
"Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
+ "mapred-site.xml and hdfs-site.xml to override properties of " +
"core-default.xml, mapred-default.xml and hdfs-default.xml " +
"respectively");
}
addDefaultResource("core-default.xml");
addDefaultResource("core-site.xml");
addDeprecatedKeys();
}
以上的代码通过静态语句加载两个文件.
2.运行时还可以通过以下的方式进行参数的设置(具体可以参考链接):
参数的解析由类GenericOptionsParser 完成.
关于Tool
Tool接口只有一个run()函数
下面时一种用实现了Tool接口的WordCount部分代码:
public class WordCount extends Configured implements Tool {
public static void main(String args[]) throws Exception {
int res = ToolRunner.run(new WordCount(), args);
System.exit(res);
}
public int run(String[] args) throws Exception {
...
}
.
.
.
}
程序的运行过程:
1.WordCount的main()函数中直接调用TooRunner的run()方法;
2.run()方法接收一个实现了Tool接口的Wordcount类;
3.run()方法中利用Configuration类自动加载运行参数
4.run()方法利用GenericOptionsParser 类对命令行参数进行解析,设置运行参数;