采用ToolRunner执行Hadoop基本面分析程序
2015-09-23 16:49
211 查看
为了简化执行作业的命令行。Hadoop它配备了一些辅助类。GenericOptionsParser它是一类。经常用来解释Hadoop命令行选项,并根据需要。至Configuration采取相应的对象设置值。
通常不直接使用GenericOptionsParser,更方便的方式是:实现Tool接口,通过ToolRunner来执行应用程序,ToolRunner内部调用GenericOptionsParser。
一、相关的类及接口解释
(一)相关类及其相应关系例如以下:
![](http://img.blog.csdn.net/20140822103518218?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamVkaWFlbF9sdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20140822103536515?<br/><br/>watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamVkaWFlbF9sdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
关于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种形式:
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
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
4、ToolRunner完毕下面2个功能:
(1)为Tool创建一个Configuration对象。
(2)使得程序能够方便的读取參数配置。
ToolRunner完整源码例如以下:
(三)关于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:
core-default.xml : Read-only defaults for hadoop.
core-site.xml: Site-specific configuration for a given hadoop installation.
见下面代码:
同一时候,检查是否还存在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、在程序执行时,能够通过命令行改动參数,改动方法例如以下
![](http://img.blog.csdn.net/20140822110634887?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamVkaWFlbF9sdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3、Configuration类中有大量的add****,set****,get****方法,用于设置及获取參数。
4、Configuration实现了Iterable<Map.Entry<String,String>>,因此能够通过下面方式对其内容进行遍历:
(四)关于Tool
1、Tool类的源文件例如以下
(五)关于Configrable与Conifgured
1、Configurable的源文件例如以下:
有2个对于Configuration的set与get方法。
2、Configured的源文件例如以下:
实现了Configuable中对于Configuration的set与get方法。
二、演示样例程序一:呈现全部參数
以下是一个简单的程序:
以上程序用于输出上述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
由上可见。关于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中的參数。
通常不直接使用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
public static int run(Configuration conf, Tool tool, String[] args) 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
public static int run(Tool tool, String[] args) 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完整源码例如以下:
package org.apache.hadoop.util; import java.io.PrintStream; import org.apache.hadoop.conf.Configuration; /** * A utility to help run {@link Tool}s. * * <p><code>ToolRunner</code> can be used to run classes implementing * <code>Tool</code> interface. It works in conjunction with * {@link GenericOptionsParser} to parse the * <a href="{@docRoot}/org/apache/hadoop/util/GenericOptionsParser.html#GenericOptions"> * generic hadoop command line arguments</a> and modifies the * <code>Configuration</code> of the <code>Tool</code>. The * application-specific options are passed along without being modified. * </p> * * @see Tool * @see GenericOptionsParser */ public class ToolRunner { /** * Runs the given <code>Tool</code> by {@link Tool#run(String[])}, after * parsing with the given generic arguments. Uses the given * <code>Configuration</code>, or builds one if null. * * Sets the <code>Tool</code>'s configuration with the possibly modified * version of the <code>conf</code>. * * @param conf <code>Configuration</code> for the <code>Tool</code>. * @param tool <code>Tool</code> to run. * @param args command-line arguments to the tool. * @return exit code of the {@link Tool#run(String[])} method. */ public static int run(Configuration conf, Tool tool, String[] args) throws Exception{ if(conf == null) { conf = new Configuration(); } GenericOptionsParser parser = new GenericOptionsParser(conf, args); //set the configuration back, so that Tool can configure itself tool.setConf(conf); //get the args w/o generic hadoop args String[] toolArgs = parser.getRemainingArgs(); return tool.run(toolArgs); } /** * Runs the <code>Tool</code> with its <code>Configuration</code>. * * Equivalent to <code>run(tool.getConf(), tool, args)</code>. * * @param tool <code>Tool</code> to run. * @param args command-line arguments to the tool. * @return exit code of the {@link Tool#run(String[])} method. */ public static int run(Tool tool, String[] args) throws Exception{ return run(tool.getConf(), tool, args); } /** * Prints generic command-line argurments and usage information. * * @param out stream to write usage information to. */ public static void printGenericCommandUsage(PrintStream out) { GenericOptionsParser.printGenericCommandUsage(out); } }
(三)关于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:
core-default.xml : Read-only defaults for hadoop.
core-site.xml: Site-specific configuration for a given hadoop installation.
见下面代码:
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"); }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>>,因此能够通过下面方式对其内容进行遍历:
for (Entry<String, String> entry : conf){ ..... }
(四)关于Tool
1、Tool类的源文件例如以下
package org.apache.hadoop.util; import org.apache.hadoop.conf.Configurable; public interface Tool extends Configurable { int run(String [] args) throws Exception; }由此可见,Tool自身仅仅有一个方法run(String[]),同一时候它继承了Configuable的2个方法。
(五)关于Configrable与Conifgured
1、Configurable的源文件例如以下:
package org.apache.hadoop.conf; public interface Configurable { void setConf(Configuration conf); Configuration getConf(); }
有2个对于Configuration的set与get方法。
2、Configured的源文件例如以下:
package org.apache.hadoop.conf; public class Configured implements Configurable { private Configuration conf; public Configured() { this(null); } public Configured(Configuration conf) { setConf(conf); } public void setConf(Configuration conf) { this.conf = conf; } public Configuration getConf() { return conf; } }它有2个构造方法。各自是带Configuration參数的方法与不还參数的方法。
实现了Configuable中对于Configuration的set与get方法。
二、演示样例程序一:呈现全部參数
以下是一个简单的程序:
package org.jediael.hadoopdemo.toolrunnerdemo; import java.util.Map.Entry; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class ToolRunnerDemo extends Configured implements Tool { static { //Configuration.addDefaultResource("hdfs-default.xml"); //Configuration.addDefaultResource("hdfs-site.xml"); //Configuration.addDefaultResource("mapred-default.xml"); //Configuration.addDefaultResource("mapred-site.xml"); } @Override public int run(String[] args) throws Exception { Configuration conf = getConf(); for (Entry<String, String> entry : conf) { System.out.printf("%s=%s\n", entry.getKey(), entry.getValue()); } return 0; } public static void main(String[] args) throws Exception { int exitCode = ToolRunner.run(new ToolRunnerDemo(), args); System.exit(exitCode); } }
以上程序用于输出上述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
package org.jediael.hadoopdemo.toolrunnerdemo; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class WordCount extends Configured implements Tool{ public static class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> { private final IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer token = new StringTokenizer(line); while (token.hasMoreTokens()) { word.set(token.nextToken()); context.write(word, one); } } } public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } @Override public int run(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf); job.setJarByClass(WordCount.class); job.setJobName("wordcount"); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(WordCountMap.class); job.setReducerClass(WordCountReduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); return(job.waitForCompletion(true)? 0:-1); } public static void main(String[] args) throws Exception { int exitCode = ToolRunner.run(new WordCount(), args); System.exit(exitCode); } }执行程序:
[root@jediael project]# hadoop fs -mkdir wcin2 [root@jediael project]# hadoop fs -copyFromLocal /opt/jediael/apache-nutch-2.2.1/CHANGES.txt wcin2 [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中的參数。
相关文章推荐
- SCADA软件整体架构
- linux 启动过程
- 鸟哥Linux私房菜
- VS2012+Win7网站发布详细步骤
- 【opencv学习】lucas金字塔光流算法的实现——基于opencv3.0+vs2013+windows10
- Apache Shiro权限框架在SpringMVC+Hibernate中的应用
- tomcat共享jar包(maven项目)
- 运维生存时间呕血之作:网站运维黑锅如何甩
- CentOS 下源码安装LAMP环境
- SSH密钥登录让Linux VPS/服务器更安全
- 分布式服务器集群架构方案思考
- 把cdrom变为yum repository
- linux下清理缓存的方法
- linux编译多文件程序
- 无法使用密钥激活Parallels Desktop 9 for Mac
- hadoop hdfs 读写错误解决:java.io.IOException: Filesystem closed org.apache.hadoop.hdfs.DFSClient.checkOpe
- beanutils.populate用法
- 分布式服务器集群架构方案思考
- Topshelf安装失败
- 如何JOPtionPane的showConfirmDialog对话框button设置监视器