为了简化命令行方式运行作业,Hadoop自带了一些辅助类。GenericOptionsParser是一个类,用来解释常用的Hadoop命令行选项,并根据需要,为Configuration对象设置相应的取值。通常不直接使用GenericOptionsParser,更方便的方式是:实现Tool接口,通过ToolRunner来运行应用程序,ToolRunner内部调用GenericOptionsParser。



一、相关的类及接口解释

(一)相关类及其对应关系如下:




关于ToolRunner典型的实现方法
1、定义一个类(如上图中的MyClass),继承configured,实现Tool接口。
2、在main()方法中通过ToolRunner.run(...)方法调用上述类的run(String[]方法)
见第三部分的例子。

(二)关于ToolRunner
1、ToolRunner与上图中的类、接口无任何的继承、实现关系,它只继承了Object,没实现任何接口。
2、ToolRunner可以方便的运行那些实现了Tool接口的类(调用其run(String[])方法,并通过GenericOptionsParser 可以方便的处理hadoop命令行参数。

A utility to help run Tools.

ToolRunner can be used to run classes implementing Tool interface. It works in conjunction with GenericOptionsParser to parse the generic hadoop command line arguments and modifies the Configuration of the Tool.
The application-specific options are passed along without being modified.

3、ToolRunner除了一个空的构造方法以外,只有一个方法,即run()方法,它有以下2种形式:

run

  1. public static int run(Configuration conf,
  2. Tool tool,
  3. String[] args)
  4. throws Exception
Runs the given Tool by Tool.run(String[]), after parsing with the given generic arguments. Uses
the given Configuration, or builds one if null. Sets the Tool's configuration with the possibly modified version of the conf.
Parameters:
conf - Configuration for the Tool.
tool - Tool to run.
args - command-line arguments to the tool.
Returns:
exit code of the Tool.run(String[]) method.
Throws:
Exception

run

  1. public static int run(Tool tool,
  2. String[] args)
  3. throws Exception
Runs the Tool with its Configuration. Equivalent to run(tool.getConf(), tool, args).
Parameters:
tool - Tool to run.
args - command-line arguments to the tool.
Returns:
exit code of the Tool.run(String[]) method.
Throws:
Exception

它们均是静态方法,即可以通过类名调用。

(1)public static int run(Configuration conf,Tool tool, String[] args)
这个方法调用tool的run(String[])方法,并使用conf中的参数,以及args中的参数,而args一般来源于命令行。
(2)public static int run(Tool tool, String[] args)
这个方法调用tool的run方法,并使用tool类的参数属性,即等同于run(tool.getConf(), tool, args)。

除此以外,还有一个方法:

static void
printGenericCommandUsage(PrintStream out) 

          Prints generic command-line argurments and usage information.

4、ToolRunner完成以下2个功能:

(1)为Tool创建一个Configuration对象。

(2)使得程序可以方便的读取参数配置。

ToolRunner完整源代码如下:

  1. package org.apache.hadoop.util;
  2.  
  3. import java.io.PrintStream;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6.  
  7. /**
  8. * A utility to help run {@link Tool}s.
  9. *
  10. * <p><code>ToolRunner</code> can be used to run classes implementing
  11. * <code>Tool</code> interface. It works in conjunction with
  12. * {@link GenericOptionsParser} to parse the
  13. * <a href="{@docRoot}/org/apache/hadoop/util/GenericOptionsParser.html#GenericOptions">
  14. * generic hadoop command line arguments</a> and modifies the
  15. * <code>Configuration</code> of the <code>Tool</code>. The
  16. * application-specific options are passed along without being modified.
  17. * </p>
  18. *
  19. * @see Tool
  20. * @see GenericOptionsParser
  21. */
  22. public class ToolRunner {
  23.  
  24. /**
  25. * Runs the given <code>Tool</code> by {@link Tool#run(String[])}, after
  26. * parsing with the given generic arguments. Uses the given
  27. * <code>Configuration</code>, or builds one if null.
  28. *
  29. * Sets the <code>Tool</code>'s configuration with the possibly modified
  30. * version of the <code>conf</code>.
  31. *
  32. * @param conf <code>Configuration</code> for the <code>Tool</code>.
  33. * @param tool <code>Tool</code> to run.
  34. * @param args command-line arguments to the tool.
  35. * @return exit code of the {@link Tool#run(String[])} method.
  36. */
  37. public static int run(Configuration conf, Tool tool, String[] args)
  38. throws Exception{
  39. if(conf == null) {
  40. conf = new Configuration();
  41. }
  42. GenericOptionsParser parser = new GenericOptionsParser(conf, args);
  43. //set the configuration back, so that Tool can configure itself
  44. tool.setConf(conf);
  45.  
  46. //get the args w/o generic hadoop args
  47. String[] toolArgs = parser.getRemainingArgs();
  48. return tool.run(toolArgs);
  49. }
  50.  
  51. /**
  52. * Runs the <code>Tool</code> with its <code>Configuration</code>.
  53. *
  54. * Equivalent to <code>run(tool.getConf(), tool, args)</code>.
  55. *
  56. * @param tool <code>Tool</code> to run.
  57. * @param args command-line arguments to the tool.
  58. * @return exit code of the {@link Tool#run(String[])} method.
  59. */
  60. public static int run(Tool tool, String[] args)
  61. throws Exception{
  62. return run(tool.getConf(), tool, args);
  63. }
  64.  
  65. /**
  66. * Prints generic command-line argurments and usage information.
  67. *
  68. * @param out stream to write usage information to.
  69. */
  70. public static void printGenericCommandUsage(PrintStream out) {
  71. GenericOptionsParser.printGenericCommandUsage(out);
  72. }
  73.  
  74. }


(三)关于Configuration
1、默认情况下,hadoop会加载core-default.xml以及core-site.xml中的参数。

Unless explicitly turned off, Hadoop by default specifies two resources, loaded in-order from the classpath:

  1. core-default.xml : Read-only defaults for hadoop.
  2. core-site.xml: Site-specific configuration for a given hadoop installation.
见以下代码:
  1. static{
  2. //print deprecation warning if hadoop-site.xml is found in classpath
  3. ClassLoader cL = Thread.currentThread().getContextClassLoader();
  4. if (cL == null) {
  5. cL = Configuration.class.getClassLoader();
  6. }
  7. if(cL.getResource("hadoop-site.xml")!=null) {
  8. LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
  9. "Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
  10. + "mapred-site.xml and hdfs-site.xml to override properties of " +
  11. "core-default.xml, mapred-default.xml and hdfs-default.xml " +
  12. "respectively");
  13. }
  14. addDefaultResource("core-default.xml");
  15. addDefaultResource("core-site.xml");
  16. }

Configuration.java的源代码中包含了以上代码,即通过静态语句为程序加载core-default.xml以及core-site.xml中的参数。

同时,检查是否还存在hadoop-site.xml,若还存在,则给出warning,提醒此配置文件已经废弃。

如何查找到上述2个文件:(见hadoop命令的脚本)
(1)定位HADOOP_CONF_DIR  Alternate conf dir. Default is ${HADOOP_HOME}/conf.
(2)将HADOOP_CONF_DIR加入CLASSPATH="${HADOOP_CONF_DIR}"
(3)可以在CLASSPATH中直接查找上述文件。


2、在程序运行时,可以通过命令行修改参数,修改方法如下

3、Configuration类中有大量的add****,set****,get****方法,用于设置及获取参数。

4、Configuration实现了Iterable<Map.Entry<String,String>>,因此可以通过以下方式对其内容进行遍历:
  1. for (Entry<String, String> entry : conf){
  2. .....

(四)关于Tool

1、Tool类的源文件如下
  1. package org.apache.hadoop.util;
  2.  
  3. import org.apache.hadoop.conf.Configurable;
  4.  
  5. public interface Tool extends Configurable {
  6.  
  7. int run(String [] args) throws Exception;
  8. }

由此可见,Tool自身只有一个方法run(String[]),同时它继承了Configuable的2个方法。


(五)关于Configrable与Conifgured
1、Configurable的源文件如下:
  1. package org.apache.hadoop.conf;
  2.  
  3. public interface Configurable {
  4.  
  5. void setConf(Configuration conf);
  6.  
  7. Configuration getConf();
  8. }
有2个对于Configuration的set与get方法。

2、Configured的源文件如下:

  1. package org.apache.hadoop.conf;
  2.  
  3. public class Configured implements Configurable {
  4.  
  5. private Configuration conf;
  6. public Configured() {
  7. this(null);
  8. }
  9.  
  10. public Configured(Configuration conf) {
  11. setConf(conf);
  12. }
  13.  
  14. public void setConf(Configuration conf) {
  15. this.conf = conf;
  16. }
  17.  
  18. public Configuration getConf() {
  19. return conf;
  20. }
  21.  
  22. }

它有2个构造方法,分别是带Configuration参数的方法与不还参数的方法。

实现了Configuable中对于Configuration的set与get方法。



二、示例程序一:呈现所有参数
下面是一个简单的程序:
  1. package org.jediael.hadoopdemo.toolrunnerdemo;
  2.  
  3. import java.util.Map.Entry;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.conf.Configured;
  7. import org.apache.hadoop.util.Tool;
  8. import org.apache.hadoop.util.ToolRunner;
  9.  
  10. public class ToolRunnerDemo extends Configured implements Tool {
  11. static {
  12. //Configuration.addDefaultResource("hdfs-default.xml");
  13. //Configuration.addDefaultResource("hdfs-site.xml");
  14. //Configuration.addDefaultResource("mapred-default.xml");
  15. //Configuration.addDefaultResource("mapred-site.xml");
  16. }
  17.  
  18. @Override
  19. public int run(String[] args) throws Exception {
  20. Configuration conf = getConf();
  21. for (Entry<String, String> entry : conf) {
  22. System.out.printf("%s=%s\n", entry.getKey(), entry.getValue());
  23. }
  24. return 0;
  25. }
  26.  
  27. public static void main(String[] args) throws Exception {
  28. int exitCode = ToolRunner.run(new ToolRunnerDemo(), args);
  29. System.exit(exitCode);
  30. }
  31. }

以上程序用于输出上述xml文件中定义的属性。

1、直接运行程序
[root@jediael project]#hadoop jar toolrunnerdemo.jar org.jediael.hadoopdemo.toolrunnerdemo.ToolRunnerDemo

io.seqfile.compress.blocksize=1000000 

keep.failed.task.files=false 

mapred.disk.healthChecker.interval=60000 

dfs.df.interval=60000 

dfs.datanode.failed.volumes.tolerated=0 

mapreduce.reduce.input.limit=-1 

mapred.task.tracker.http.address=0.0.0.0:50060 

mapred.used.genericoptionsparser=true 

mapred.userlog.retain.hours=24 

dfs.max.objects=0 

mapred.jobtracker.jobSchedulable=org.apache.hadoop.mapred.JobSchedulable 

mapred.local.dir.minspacestart=0 

hadoop.native.lib=true
......................

2、通过-D指定新的参数
[root@jediael project]# hadoop org.jediael.hadoopdemo.toolrunnerdemo.ToolRunnerDemo -D color=yello | grep color 

color=yello

3、通过-conf增加新的配置文件
(1)原有参数数量
[root@jediael project]# hadoop jar toolrunnerdemo.jar org.jediael.hadoopdemo.toolrunnerdemo.ToolRunnerDemo | wc                 
67 67 2994
(2)增加配置文件后的参数数量
[root@jediael project]# hadoop jar toolrunnerdemo.jar org.jediael.hadoopdemo.toolrunnerdemo.ToolRunnerDemo-conf /opt/jediael/hadoop-1.2.0/conf/mapred-site.xml |
wc 

    68 68 3028
其中mapred-site.xml的内容如下:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration> 
     <property>
         <name>mapred.job.tracker</name>
         <value>localhost:9001</value>
     </property>
</configuration>
可见此文件只有一个property,因此参数数量从67个变成了68个。

4、在代码中增加参数,如上面程序中注释掉的语句
static {
  Configuration.addDefaultResource("hdfs-default.xml");
  Configuration.addDefaultResource("hdfs-site.xml");
  Configuration.addDefaultResource("mapred-default.xml");
  Configuration.addDefaultResource("mapred-site.xml");
 }
更多选项请见第Configuration的解释。



三、示例程序二:典型用法(修改wordcount程序)
修改经典的wordcount程序,参考:Hadoop入门经典:WordCount
  1. package org.jediael.hadoopdemo.toolrunnerdemo;
  2.  
  3. import java.io.IOException;
  4. import java.util.StringTokenizer;
  5.  
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.conf.Configured;
  8. import org.apache.hadoop.fs.Path;
  9. import org.apache.hadoop.io.IntWritable;
  10. import org.apache.hadoop.io.LongWritable;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import org.apache.hadoop.mapreduce.Reducer;
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  16. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  17. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  18. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  19. import org.apache.hadoop.util.Tool;
  20. import org.apache.hadoop.util.ToolRunner;
  21.  
  22. public class WordCount extends Configured implements Tool{
  23.  
  24. public static class WordCountMap extends
  25. Mapper<LongWritable, Text, Text, IntWritable> {
  26.  
  27. private final IntWritable one = new IntWritable(1);
  28. private Text word = new Text();
  29.  
  30. public void map(LongWritable key, Text value, Context context)
  31. throws IOException, InterruptedException {
  32. String line = value.toString();
  33. StringTokenizer token = new StringTokenizer(line);
  34. while (token.hasMoreTokens()) {
  35. word.set(token.nextToken());
  36. context.write(word, one);
  37. }
  38. }
  39. }
  40.  
  41. public static class WordCountReduce extends
  42. Reducer<Text, IntWritable, Text, IntWritable> {
  43.  
  44. public void reduce(Text key, Iterable<IntWritable> values,
  45. Context context) throws IOException, InterruptedException {
  46. int sum = 0;
  47. for (IntWritable val : values) {
  48. sum += val.get();
  49. }
  50. context.write(key, new IntWritable(sum));
  51. }
  52. }
  53.  
  54. @Override
  55. public int run(String[] args) throws Exception {
  56. Configuration conf = new Configuration();
  57. Job job = new Job(conf);
  58. job.setJarByClass(WordCount.class);
  59. job.setJobName("wordcount");
  60.  
  61. job.setOutputKeyClass(Text.class);
  62. job.setOutputValueClass(IntWritable.class);
  63.  
  64. job.setMapperClass(WordCountMap.class);
  65. job.setReducerClass(WordCountReduce.class);
  66.  
  67. job.setInputFormatClass(TextInputFormat.class);
  68. job.setOutputFormatClass(TextOutputFormat.class);
  69.  
  70. FileInputFormat.addInputPath(job, new Path(args[0]));
  71. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  72.  
  73. return(job.waitForCompletion(true)?0:-1);
  74.  
  75. }
  76.  
  77. public static void main(String[] args) throws Exception {
  78. int exitCode = ToolRunner.run(new WordCount(), args);
  79. System.exit(exitCode);
  80. }
  81.  
  82. }

运行程序:

  1. [root@jediael project]# hadoop fs -mkdir wcin2
  2. [root@jediael project]# hadoop fs -copyFromLocal /opt/jediael/apache-nutch-2.2.1/CHANGES.txt wcin2
  3. [root@jediael project]# hadoop jar wordcount2.jar org.jediael.hadoopdemo.toolrunnerdemo.WordCount wcin2 wcout2
由上可见,关于ToolRunner的典型用法是:
1、定义一个类,继承Configured,实现Tool接口。其中Configured提供了getConf()与setConfig()方法,而Tool则提供了run()方法。
2、在main()方法中通过ToolRunner.run(...)方法调用上述类的run(String[]方法)。


四、总结
1、通过使用ToolRunner.run(...)方法,可以更便利的使用hadoop命令行参数。
2、ToolRunner.run(...)通过调用Tool类中的run(String[])方法来运行hadoop程序,并默认加载core-default.xml与core-site.xml中的参数。


使用ToolRunner运行Hadoop程序基本原理分析的更多相关文章

  1. 使用ToolRunner运行Hadoop程序基本原理分析 分类: A1_HADOOP 2014-08-22 11:03 3462人阅读 评论(1) 收藏

    为了简化命令行方式运行作业,Hadoop自带了一些辅助类.GenericOptionsParser是一个类,用来解释常用的Hadoop命令行选项,并根据需要,为Configuration对象设置相应的 ...

  2. eclipse运行hadoop程序报错:Connection refused: no further information

    eclipse运行hadoop程序报错:Connection refused: no further information log4j:WARN No appenders could be foun ...

  3. 【爬坑】在 IDEA 中运行 Hadoop 程序 报 winutils.exe 不存在错误解决方案

    0. 问题说明 环境为 Windows 10 在 IDEA 中运行 Hadoop 程序报   winutils.exe 不存在  错误 1. 解决方案 [1.1 解压] 解压 hadoop-2.7.3 ...

  4. 如何在Ubuntu的idea上运行Hadoop程序

    如何在Ubuntu的idea上运行Hadoop程序 一.前言 在idea上运行Hadoop程序,需要使用Hadoop的相关库,Ubuntu为Hadoop的运行提供了良好的支持. 二.操作方法 首先我们 ...

  5. WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path

    之前在mac上调试hadoop程序(mac之前配置过hadoop环境)一直都是正常的.因为工作需要,需要在windows上先调试该程序,然后再转到linux下.程序运行的过程中,报Failed to ...

  6. 关于在Eclipse上运行Hadoop程序的日志输出问题

    在安装由Eclipse-Hadoop-Plugin的Eclipse中, 可以直接运行Hadoop的MapReduce程序, 但是如果什么都不配置的话你发现Eclipse控制台没有任何日志输出, 这个问 ...

  7. 用java运行Hadoop程序报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.

    用java运行Hadoop例程报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.所写代码如下: package ...

  8. Ubuntu中使用终端运行Hadoop程序

    接上一篇<Ubuntu Kylin系统下安装Hadoop2.6.0> 通过上一篇,Hadoop伪分布式基本配好了. 下一步是运行一个MapReduce程序,以WordCount为例: 1. ...

  9. IDEA下调试和运行Hadoop程序例子

    准备 配置好JDK和Hadoop环境, 在IDEA中建立maven项目,建立后的目录结构为: 修改pom..xml引入相关支持: <?xml version="1.0" en ...

随机推荐

  1. php用get_meta_tags轻松获取网页的meta信息

    之前没发现php还有这个函数,get_meta_tags()直接就可以获取文件中meta标签的属性值,返回数组: <?php $metas = get_meta_tags('http://www ...

  2. destoon系统商城加淘宝客按钮方法

    destoon系统很多喜欢运营B2B的站长都在用,其中的商城模块常常被用来做淘宝客,其中的难点是如何把购买按钮做成淘宝客地址,这个问题的修改在论坛上被叫价50元,下面小编把这个实用的方法分享下,希望对 ...

  3. 开发纯ndk程序之环境搭配

    安装ndk 从安卓官网下载,ndk,双击解压到当前文件夹.建议想装在那个文件夹便解压到那个文件夹,而且文件夹的路径中不要有空格,因为gcc编译的时候会把空格前后两个字符串作为两个文件夹来对待. 使用g ...

  4. oracle 主键应用序列和触发器实现自动增长

    oracle 主键自动增长 这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create table simon_example ( id number(4) not null pr ...

  5. Android 网络通信框架Volley的简单使用

    Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮. Volley提供的功能: JSON,图像等的异步下载: 网络请求的排序(scheduling) 网络请求的优先级处理 ...

  6. CCI_chapter 1

    1.1Implement an algorithm to determine if a string has all unique characters What if  you can not us ...

  7. Cmake设置环境变量

    references: http://stackoverflow.com/questions/21047399/cmake-set-environment-variables-from-a-scrip ...

  8. Delphi 调试 通过BreakPoint

    1.打个断点, 如下图 2. 在断点上,邮件,如下图 3. 弹出一个窗体 ,如下图 在 condition 中写条件就可以了.  这样就可以按你假设的条件来进行了,方便.

  9. cxSplitter.HotZone 怎么给分隔条增加值

    请使用它的类名指定HotZoneClassName . cxSplitter1.ResizeUpdate := True; cxSplitter1.HotZoneClassName := 'TcxMe ...

  10. Oracle优化笔记

    2016-11-22   子查询:标量子查询 内联视图(in-line view) 半连接/反连接   标量子查询 select 后跟子查询 类似自定义函数 可用开窗函数之类的改写   内联视图(in ...