辅助类GenericOptionsParser,Tool和ToolRunner

(1)为什么要用ToolRunner

将MapReduce Job配置参数写到java代码里,一旦变更意味着修改java文件源码、编译、打包、部署一连串事情。当MapReduce 依赖配置文件的时候,你需要手工编写java代码使用DistributedCache将其上传到HDFS中,以便map和reduce函数可以读取。:当你的map或reduce 函数依赖第三方jar文件时,你在命令行中使用”-libjars”参数指定依赖jar包时,但根本没生效。Hadoop有个可以GenericOptionsParser是一个类,用来解释常用的Hadoop命令行选项,通过简单的命令行参数来实现这样的功能,为Configuration对象设置相应的取值。通常不直接使用GenericOptionsParser,更方便的方式是:实现Tool接口,通过ToolRunner来运行应用程序,ToolRunner内部调用GenericOptionsParser来解析命令行。设置Configuration对象。

(2)使用ToolRunner步骤

自定义一个ToolRunner类ToolRunnerDemo类,继承Configured类,实现Tool接口,实现Tool的run(String [] args)方法,并在main函数中调用ToolRunner. run(Tool tool, String[] args)静态方法。Run方法内部创建GenericOptionsParser parser = new GenericOptionsParser(conf, args);调用GenericOptionsParser解析命令行参数,解析完之后将参数设置到Configuration对象中。

1)创建ToolRunnerDemo对象

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);//ToolRunnerDemo对象实现了Tool接口,形参传入对象引用,在调用tool.run()方法,实际是调用ToolRunner重写的run方法

System.exit(exitCode);

}

}

Configurable接口只有两个函数,获取设置Configuration对象

package org.apache.hadoop.conf;

public interface Configurable {

void setConf(Configuration conf);

Configuration getConf();

}

Configred类实现了Configurable接口

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)ToolRunner.run()函数内部创建GenericOptionsParser对象

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);

}

3)GenericOptionsParser构造函数1调用构造函数2,构造函数2调用解析函数parseGeneralOptions

public GenericOptionsParser(Options opts, String[] args) throws IOException {

this(new Configuration(), opts, args);

}//构造函数1

public GenericOptionsParser(Configuration conf, Options options, String[] args) throws IOException {

this.parseGeneralOptions(options, conf, args);

this.conf = conf;

}//构造函数2

4)parseGeneralOptions先调用解析函数parser.parse解析命令行,然后再用函数this.processGeneralOptions()执行命令。

private void parseGeneralOptions(Options opts, Configuration conf, String[] args) throws IOException {

opts = buildGeneralOptions(opts);

GnuParser parser = new GnuParser();

try {

 this.commandLine = parser.parse(opts, this.preProcessForWindows(args), true);

 this.processGeneralOptions(conf, this.commandLine);

} catch (ParseException var7) {

LOG.warn("options parsing failed: " + var7.getMessage());

HelpFormatter formatter = new HelpFormatter();

formatter.printHelp("general options are: ", opts);

}

}

5)processGeneralOptions函数内部会根据不同的命令选项:fs、jt、conf、libjars、files、archives进行设置。

private void processGeneralOptions(Configuration conf, CommandLine line) throws IOException {

//设置默认的文件系统

if (line.hasOption("fs")) {

FileSystem.setDefaultUri(conf, line.getOptionValue("fs"));

}

//设置jobtracker服务ip地址和端口,用于监听并接收来自各个TaskTracker发送的心跳信息,包括资源使用情况和任务运行情况等信息。

String fileName;

if (line.hasOption("jt")) {

fileName = line.getOptionValue("jt");

if (fileName.equalsIgnoreCase("local")) {

conf.set("mapreduce.framework.name", fileName);

}

conf.set("yarn.resourcemanager.address", fileName, "from -jt command line option");

}

//添加新的配置文件

String[] arr$;

int len$;

int i$;

String prop;

String[] property;

if (line.hasOption("conf")) {

property = line.getOptionValues("conf");

arr$ = property;

len$ = property.length;

for(i$ = 0; i$ < len$; ++i$) {

prop = arr$[i$];

conf.addResource(new Path(prop));

}

}

//从本地文件系统中复制指定的jar包到jobtracker使用的共享文件系统中,添加到mapreduce任务路径,这个选项是一个很有用放入方法来添加任务的依赖jar包。

if (line.hasOption("libjars")) {

conf.set("tmpjars", this.validateFiles(line.getOptionValue("libjars"), conf), "from -libjars command line option");

URL[] libjars = getLibJars(conf);

if (libjars != null && libjars.length > 0) {

conf.setClassLoader(new URLClassLoader(libjars, conf.getClassLoader()));

Thread.currentThread().setContextClassLoader(new URLClassLoader(libjars, Thread.currentThread().getContextClassLoader()));

}

}

//从本地文件系统中复制指定的文件到jobtracker使用的共享文件系统中,使他们能够被mapreduce任务使用

if (line.hasOption("files")) {

conf.set("tmpfiles", this.validateFiles(line.getOptionValue("files"), conf), "from -files command line option");

}

//从本地文件系统中复制指定的档案到jobtracker使用的共享文件系统中,使他们能够被mapreduce任务使用。

if (line.hasOption("archives")) {

conf.set("tmparchives", this.validateFiles(line.getOptionValue("archives"), conf), "from -archives command line option");

}

//给属性设置属性值

if (line.hasOption('D')) {

property = line.getOptionValues('D');

arr$ = property;

len$ = property.length;

for(i$ = 0; i$ < len$; ++i$) {

prop = arr$[i$];

String[] keyval = prop.split("=", 2);

if (keyval.length == 2) {

conf.set(keyval[0], keyval[1], "from command line");

}

}

}

conf.setBoolean("mapreduce.client.genericoptionsparser.used", true);

if (line.hasOption("tokenCacheFile")) {

fileName = line.getOptionValue("tokenCacheFile");

FileSystem localFs = FileSystem.getLocal(conf);

Path p = localFs.makeQualified(new Path(fileName));

if (!localFs.exists(p)) {

throw new FileNotFoundException("File " + fileName + " does not exist.");

}

if (LOG.isDebugEnabled()) {

LOG.debug("setting conf tokensFile: " + fileName);

}

UserGroupInformation.getCurrentUser().addCredentials(Credentials.readTokenStorageFile(p, conf));

conf.set("mapreduce.job.credentials.json", p.toString(), "from -tokenCacheFile command line option");

}

}

6)设置好配置之后,所有的命令行都解析执行,参数都添加到了Configuration对象之中,接下来就可以获取这些参数。

在第2)步的ToolRunner的run函数中

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);

}

parser.getRemainingArgs();获取的实际上是第4)步中解析的命令行参数

public String[] getRemainingArgs() {

return this.commandLine == null ? new String[0] : this.commandLine.getArgs();

}

7)调用tool接口的run方法,实际是调用ToolRunnerDemo重写的run方法。因为ToolRunnerDemo对象实现了Tool接口,ToolRunner.run函数形参传入ToolRunnerDemo对象引用,在调用tool.run()方法, 实际是调用ToolRunnerDemo重写的run方法。

@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;

}

(3)使用ToolRunnerDemo设置hadoop参数调用实例

1)使用ToolRunnerDemo输出所有配置属性

[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指定新的参数,-D设置参数color为yello,grep查看设置属性

[root@jediael project]# hadoop org.jediael.hadoopdemo.toolrunnerdemo.ToolRunnerDemo -D color=yello | grep color

color=yello

3)通过-conf增加新的配置文件,-conf用于添加配置文件,wc命令用于查看配置数量。

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>

(4)ToolRunner使用汇总

-D color=yello

-D设置参数color为yello,grep查看设置属性

-conf conf/mapred-site.xml

-conf用于添加配置文件

-fs uri

//设置文件系统为uri指定的路径,等同-D fs.default.FS=url

-jt 10.21.34.11:3800

//hadoop1中用于设置jobtracker的ip地址和端口,用于监听并接收来自各个TaskTracker发送的心跳信息,包括资源使用情况和任务运行情况等信息。hadoop2中用于指定YARN资源管理器地址。等同-D yarn.resourcemanager.address= 10.21.34.11:3800.

-files file1,file2

从本地文件系统中复制指定的文件到jobtracker使用的共享文件系统中,使他们能够被mapreduce任务使用

-libjars jars1,jars2

从本地文件系统中复制指定的jar包到jobtracker使用的共享文件系统中,添加到mapreduce任务路径,这个选项是一个很有用放入方法来添加任务的依赖jar包。

-archives archive1,archive2

从本地文件系统中复制指定的档案到jobtracker使用的共享文件系统中,使他们能够被mapreduce任务使用。

参考文献:

https://blog.csdn.net/xin15200793067/article/details/12623797

https://blog.csdn.net/jediael_lu/article/details/38751885

https://blog.csdn.net/tototuzuoquan/article/details/72856761

自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

https://www.cnblogs.com/bclshuai/p/11380657.html

最新文章

  1. [转]PLSQL Developer备份恢复oracle数据
  2. 对Oracle10g rac srvctl命令使用理解
  3. 【HDU 3810】 Magina (01背包,优先队列优化,并查集)
  4. Shell变量命名规则
  5. hadoop权威指南 chapter1 Meet Hadoop
  6. Effective C++_笔记_条款06_若不想使用编译器自动生成的函数,就该明确拒绝
  7. 菜鸟玩云计算之十一:Hadoop 手动安装指南
  8. openstack私有云布署实践【10.1 计算nova - kxcontroller节点配置(科兴环境)】
  9. Xcode--Alcatraz来管理Xcode插件
  10. RHL 6.0学习日记, 先记下来,以后整理。
  11. jquery页面水印插件,支持多行水印、行错开
  12. java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.&lt;init&gt;([Ljava
  13. Java注解开发与应用案例
  14. cve-2018-4878漏洞复现
  15. php实现遍历目录
  16. C++ boost.python折腾笔记
  17. Elasticsearch系列(2):安装Elasticsearch(Linux环境)
  18. Microsoft Bot Framework with LUIS
  19. FineUILearning
  20. ECharts基础

热门文章

  1. 微信小程序前端页面书写
  2. Bran的内核开发教程(bkerndev)-02 准备工作
  3. 域渗透-凭据传递攻击(pass the hash)完全总结
  4. 为什么阿里巴巴Java开发手册中不建议在循环体中使用+进行字符串拼接?
  5. 13.多级代理下Nginx透传真实IP
  6. 21.Linux系统服务之大坑
  7. 高性能封装检测浏览器支持css3属性函数
  8. 最近学到的ABTest知识
  9. Spring Cloud ---- 服务消费与负载均衡(feign)
  10. Java多线程编程(五)定时器Timer