解析GenericOptionsParser
hadoop源代码分析(4)-org.apache.hadoop.util包-GenericOptionsParser类【原创】
一 准备
hadoop版本:1.0.3,GenericOptionsParser所在的包:org.apache.hadoop.util
学习方法:理解GenericOptionsParser这个解析器的使用地方,从构造函数入手,理解GenericOptionsParser整个类的使用情况。
时间:2013-02-21
二 GenericOptionsParser功能描述
GenericOptionsParser是hadoop框架中解析命令行参数的基本类。它能够辨别一些标准的命令行参数,能够使应用程序轻易地指定namenode,jobtracker,以及其他额外的配置资源。一般它使用方法如下:

- 1 out.println("Generic options supported are");
- 2 out.println("-conf <configuration file> specify an application configuration file");
- 3 out.println("-D <property=value> use value for given property");
- 4 out.println("-fs <local|namenode:port> specify a namenode");
- 5 out.println("-jt <local|jobtracker:port> specify a job tracker");
- 6 out.println("-files <comma separated list of files> " +
- 7 "specify comma separated files to be copied to the map reduce cluster");
- 8 out.println("-libjars <comma separated list of jars> " +
- 9 "specify comma separated jar files to include in the classpath.");
- 10 out.println("-archives <comma separated list of archives> " +
- 11 "specify comma separated archives to be unarchived" +
- 12 " on the compute machines.\n");
- 13 out.println("The general command line syntax is");
- 14 out.println("bin/hadoop command [genericOptions] [commandOptions]\n");

使用示例:

- 1 * $ bin/hadoop dfs -fs darwin:8020 -ls /data
- 2 * list /data directory in dfs with namenode darwin:8020
- 3 *
- 4 * $ bin/hadoop dfs -D fs.default.name=darwin:8020 -ls /data
- 5 * list /data directory in dfs with namenode darwin:8020
- 6 *
- 7 * $ bin/hadoop dfs -conf hadoop-site.xml -ls /data
- 8 * list /data directory in dfs with conf specified in hadoop-site.xml
- 9 *
- 10 * $ bin/hadoop job -D mapred.job.tracker=darwin:50020 -submit job.xml
- 11 * submit a job to job tracker darwin:50020
- 12 *
- 13 * $ bin/hadoop job -jt darwin:50020 -submit job.xml
- 14 * submit a job to job tracker darwin:50020
- 15 *
- 16 * $ bin/hadoop job -jt local -submit job.xml
- 17 * submit a job to local runner
- 18 *
- 19 * $ bin/hadoop jar -libjars testlib.jar
- 20 * -archives test.tgz -files file.txt inputjar args
- 21 * job submission with libjars, files and archives

四 GenericOptionsParser主要方法、属性分析
GenericOptionsParser这个类是从构造函数开始的,它有多个构造函数,真正的处理是在parseGeneralOptions(options, conf, args)这个函数中。

- 1 /**
- 2 * 构造GenericOptionsParser来解析给定的选项以及基本的hadoop选项
- 3 * 命令行对象可以通过getCommandLine()函数获得
- 4 * @param conf the configuration to modify
- 5 * @param options options built by the caller
- 6 * @param args User-specified arguments
- 7 * @throws IOException
- 8 */
- 9 public GenericOptionsParser(Configuration conf,
- 10 Options options, String[] args) throws IOException {
- 11 parseGeneralOptions(options, conf, args);
- 12 this.conf = conf;
- 13 }

parseGeneralOptions(options, conf, args)这个函数解析用户指定的参数,获取基本选项以及根据需要修改配置。它首先指定每个通用选项的属性,然后解析选项,参数,把它转化为命令行对象(CommandLine),紧接着把设定好的命令行参数写入系统配置,源代码如下:

- 1 /**
- 2 * 解析用户指定的参数,获取基本选项以及根据需要修改配置
- 3 * Parse the user-specified options, get the generic options, and modify
- 4 * configuration accordingly
- 5 * @param conf Configuration to be modified
- 6 * @param args User-specified arguments
- 7 * @return Command-specific arguments
- 8 */
- 9 private String[] parseGeneralOptions(Options opts, Configuration conf,
- 10 String[] args) throws IOException {
- 11 // 指定每个通用选项的属性
- 12 opts = buildGeneralOptions(opts);
- 13 CommandLineParser parser = new GnuParser();
- 14 try {
- 15 // 解析选项,参数,获取命令行
- 16 commandLine = parser.parse(opts, args, true);
- 17 // 根据用户指定的参数(commandLine)修改系统的配置
- 18 processGeneralOptions(conf, commandLine);
- 19 return commandLine.getArgs();
- 20 } catch(ParseException e) {
- 21 LOG.warn("options parsing failed: "+e.getMessage());
- 22
- 23 HelpFormatter formatter = new HelpFormatter();
- 24 formatter.printHelp("general options are: ", opts);
- 25 }
- 26 return args;
- 27 }

processGeneralOptions函数作用是修改配置,利用CommandLine对象的相关方法,这个类包含处理选项以及选项描述,选项值的方法,源代码如下:

- 1 /**
- 2 * 根据用户指定的参数修改配置
- 3 * Modify configuration according user-specified generic options
- 4 * @param conf Configuration to be modified
- 5 * @param line User-specified generic options
- 6 */
- 7 private void processGeneralOptions(Configuration conf,
- 8 CommandLine line) throws IOException {
- 9 if (line.hasOption("fs")) {
- 10 // 设置NAMENODE的ip
- 11 FileSystem.setDefaultUri(conf, line.getOptionValue("fs"));
- 12 }
- 13
- 14 if (line.hasOption("jt")) {
- 15 conf.set("mapred.job.tracker", line.getOptionValue("jt"));
- 16 }
- 17 if (line.hasOption("conf")) {
- 18 String[] values = line.getOptionValues("conf");
- 19 for(String value : values) {
- 20 // 新增配置文件,除非是final属性,不然新配置文件会覆盖旧的配置文件
- 21 conf.addResource(new Path(value));
- 22 }
- 23 }
- 24 if (line.hasOption("libjars")) {
- 25 conf.set("tmpjars",
- 26 validateFiles(line.getOptionValue("libjars"), conf));
- 27 //setting libjars in client classpath
- 28 URL[] libjars = getLibJars(conf);
- 29 if(libjars!=null && libjars.length>0) {
- 30 conf.setClassLoader(new URLClassLoader(libjars, conf.getClassLoader()));
- 31 Thread.currentThread().setContextClassLoader(
- 32 new URLClassLoader(libjars,
- 33 Thread.currentThread().getContextClassLoader()));
- 34 }
- 35 }
- 36 if (line.hasOption("files")) {
- 37 conf.set("tmpfiles",
- 38 validateFiles(line.getOptionValue("files"), conf));
- 39 }
- 40 if (line.hasOption("archives")) {
- 41 conf.set("tmparchives",
- 42 validateFiles(line.getOptionValue("archives"), conf));
- 43 }
- 44 if (line.hasOption('D')) {
- 45 String[] property = line.getOptionValues('D');
- 46 for(String prop : property) {
- 47 String[] keyval = prop.split("=", 2);
- 48 if (keyval.length == 2) {
- 49 conf.set(keyval[0], keyval[1]);
- 50 }
- 51 }
- 52 }
- 53 conf.setBoolean("mapred.used.genericoptionsparser", true);
- 54
- 55 // tokensFile
- 56 if(line.hasOption("tokenCacheFile")) {
- 57 String fileName = line.getOptionValue("tokenCacheFile");
- 58 // check if the local file exists
- 59 try
- 60 {
- 61 FileSystem localFs = FileSystem.getLocal(conf);
- 62 Path p = new Path(fileName);
- 63 if (!localFs.exists(p)) {
- 64 throw new FileNotFoundException("File "+fileName+" does not exist.");
- 65 }
- 66
- 67 LOG.debug("setting conf tokensFile: " + fileName);
- 68 conf.set("mapreduce.job.credentials.json",
- 69 localFs.makeQualified(p).toString());
- 70 } catch (IOException e) {
- 71 throw new RuntimeException(e);
- 72 }
- 73 }
- 74 }

五 GenericOptionsParser相关类、接口简述
跟这个类相关的类是:Options,Option,ComandLine,FileSystem。
六 结语
解析GenericOptionsParser的更多相关文章
- 6.2.2 辅助类GenericOptionsParser,Tool和ToolRunner深入解析
辅助类GenericOptionsParser,Tool和ToolRunner (1)为什么要用ToolRunner 将MapReduce Job配置参数写到java代码里,一旦变更意味着修改java ...
- 琐碎-关于hadoop的GenericOptionsParser类
GenericOptionsParser 命令行解析器 是hadoop框架中解析命令行参数的基本类.它能够辨别一些标准的命令行参数,能够使应用程序轻易地指定namenode,jobtracker,以及 ...
- hadoop2.2编程:Tool, ToolRunner, GenericOptionsParser, Configuration
继承关系: 1. java.util Interface Map.Entry<K,V> description: public static interface Map.Entry&l ...
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
- .NET Core中的认证管理解析
.NET Core中的认证管理解析 0x00 问题来源 在新建.NET Core的Web项目时选择“使用个人用户账户”就可以创建一个带有用户和权限管理的项目,已经准备好了用户注册.登录等很多页面,也可 ...
- Html Agility Pack 解析Html
Hello 好久不见 哈哈,今天给大家分享一个解析Html的类库 Html Agility Pack.这个适用于想获取某网页里面的部分内容.今天就拿我的Csdn的博客列表来举例. 打开页面 用Fir ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
随机推荐
- digitalocean vpn安装配置教程
digitalocean是美国一家专业的vps提供商,优势是性价比高,最低配置512MB内存vps每月只要5美元,导致大陆用户疯狂涌入.关于digitalocean申请方法.digitalocean速 ...
- Chapter 15_3 使用环境
创建模块的基本方法的缺点在于,忘记使用local,很容易就污染全局空间. “函数环境”是一种有趣的技术,它能够解决上面的问题.就是让模块的主程序块独占一个环境. 这样不仅它的所有函数可以共享这个tab ...
- chapter 13_3 table访问的元方法
前两节的算术类.关系类运算符的元方法都为各种错误情况定义了行为,它们不会改变语言的常规行为. 但是Lua还提供了两种可以改变table行为的方法: 一种是查询table中不存在的字段.一种是修改tab ...
- 高精度运算专题-输出函数与字符串转数字函数(Output function and the string to number function)
输出函数:这个函数别看它小,但浓缩的都是精华啊 作用:对于高精度的数组进行倒序输出 思路:首先从被传入的数组第一位开始,一直往前扫输出就可以了(i--) 注释:因为每个数组的第一位是用来存储这个数组的 ...
- 【Vmware】VirtualBox下虚拟机的网络配置
1.VirtualBox的提供了四种网络接入模式,它们分别是:1.NAT 网络地址转换模式(NAT,Network Address Translation)2.Bridged Adapter ...
- Objective-C中的instancetype与id的区别
一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...
- 斯坦福大学公开课:iOS 7应用开发 笔记
2015-07-06 第一讲 课务.iOS概述 -------------------------------------------------- 开始学习斯坦福大学公开课:iOS 7应用开发留 ...
- wcf中的使用全双工通信
wcf中的契约通信默认是请求恢复的方式,当客户端发出请求后,一直到服务端回复时,才可以继续执行下面的代码. 除了使用请求应答方式的通信外,还可以使用全双工.下面给出例子: 1.添加一个wcf类库 2. ...
- Vim 配置Markdown
通过vundle工具安装以下插件: vim-markdown 语法高亮 vim-markdown-preview.vim 通过浏览器实时预览(支持同步滚动) -/.vimrc vundle部分添 ...
- rm删除文件时排除特定文件
删除当前目录下所有的*.txt文件,但除了test.txt文件: rm `ls *.txt | grep -v test.txt` 或者 rm `ls *.txt | egrep -v test.tx ...