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的更多相关文章

  1. 6.2.2 辅助类GenericOptionsParser,Tool和ToolRunner深入解析

    辅助类GenericOptionsParser,Tool和ToolRunner (1)为什么要用ToolRunner 将MapReduce Job配置参数写到java代码里,一旦变更意味着修改java ...

  2. 琐碎-关于hadoop的GenericOptionsParser类

    GenericOptionsParser 命令行解析器 是hadoop框架中解析命令行参数的基本类.它能够辨别一些标准的命令行参数,能够使应用程序轻易地指定namenode,jobtracker,以及 ...

  3. hadoop2.2编程:Tool, ToolRunner, GenericOptionsParser, Configuration

    继承关系:   1. java.util Interface Map.Entry<K,V> description: public static interface Map.Entry&l ...

  4. 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新

    本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...

  5. .NET Core中的认证管理解析

    .NET Core中的认证管理解析 0x00 问题来源 在新建.NET Core的Web项目时选择“使用个人用户账户”就可以创建一个带有用户和权限管理的项目,已经准备好了用户注册.登录等很多页面,也可 ...

  6. Html Agility Pack 解析Html

    Hello 好久不见 哈哈,今天给大家分享一个解析Html的类库 Html Agility Pack.这个适用于想获取某网页里面的部分内容.今天就拿我的Csdn的博客列表来举例. 打开页面  用Fir ...

  7. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  8. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  9. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

随机推荐

  1. 关闭HTC手机充电时屏幕一直亮着绿色电池的办法

            首先必须取得ROOT权限,用RE文件管理器进system/bin/把最下面的zchgd文件改名字或者直接删掉重新启动手机充电时就不会亮屏了,电池图标也不会出来,再用数据线连电脑充电吧, ...

  2. 大数据阶乘(The factorial of large data)

    题目描述 Description 阶乘是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(long long类型)的一个数,求它的阶乘,请给出正确结果.为提高速 ...

  3. 【实验室笔记】C#上位机学习笔记

    用C#编写上位机,基本流程是[1]串口配置,[2]串口发送数据,[3]串口接收数据. [1]串口配置 串口的属性配置包括: No.1串口端口号 No.2串口波特率 No.3串口数据位 No.4串口停止 ...

  4. Eclipse没有提示了,按Alt+/ 也无代码提示

    1.菜单window->Preferences->Java->Editor->Content Assist->Enable auto activation 选项要打上勾  ...

  5. 将网页中的html代码的table保存成word文件

    1.后台代码: string nowtime = DateTime.Now.ToString("yyyy-MM-dd"); Response.Clear(); Response.B ...

  6. 发现一个c++ vector sort的bug

    在开发中遇到一个非常诡异的问题:我用vector存储了一组数据,然后调用sort方法,利用自定义的排序函数进行排序,但是一直都会段错误,在排序函数中打印参加排序的值,发现有空值,而且每次都跟同一个数据 ...

  7. python实现邮件发送完整代码(带附件发送方式)

    实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import ...

  8. QTP连接MySQL

    1.首先安装mysql数据驱动,qtp在windows系统下操作连接mysql,所以下载mysql-connector-odbc-   5.1.8-win32.msi 下载地址:http://mysq ...

  9. @Autowired 和 @Resource

    转自:Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resourc ...

  10. redis第一篇--综述

    1 redis里边有数据库的概念.可分为1-255这些表.在存储或者查找的时候要指明. redis_sentinel 集群里边封装成了namespace这样的概念.与db是不一样的.