设为首页 收藏本站
查看: 1026|回复: 0

[经验分享] hadoop源代码分析(4)-org.apache.hadoop.util包-GenericOptionsParser类【原创】

[复制链接]

尚未签到

发表于 2015-7-12 10:05:30 | 显示全部楼层 |阅读模式
一  准备
    hadoop版本:1.0.3,GenericOptionsParser所在的包:org.apache.hadoop.util
    学习方法:理解GenericOptionsParser这个解析器的使用地方,从构造函数入手,理解GenericOptionsParser整个类的使用情况。
    时间:2013-02-21

二  GenericOptionsParser功能描述
  GenericOptionsParser是hadoop框架中解析命令行参数的基本类。它能够辨别一些标准的命令行参数,能够使应用程序轻易地指定namenode,jobtracker,以及其他额外的配置资源。一般它使用方法如下:
  


DSC0000.gif DSC0001.gif View Code


1     out.println("Generic options supported are");
2     out.println("-conf      specify an application configuration file");
3     out.println("-D             use value for given property");
4     out.println("-fs       specify a namenode");
5     out.println("-jt     specify a job tracker");
6     out.println("-files     " +
7       "specify comma separated files to be copied to the map reduce cluster");
8     out.println("-libjars     " +
9       "specify comma separated jar files to include in the classpath.");
10     out.println("-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");
  
    使用示例:
  


View Code


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)这个函数中。


View Code


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),紧接着把设定好的命令行参数写入系统配置,源代码如下:


View parseGeneralOptions Code


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对象的相关方法,这个类包含处理选项以及选项描述,选项值的方法,源代码如下:


View processGeneralOptions Code


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。

六  结语
  
    原文出处:http://www.iyunv.com/caoyuanzhanlang
    ==========================================================================================================
    ===================================    以上分析仅代表个人观点,欢迎指正与交流   ===================================
    ===================================    尊重劳动成果,转载请注明出处,万分感谢   ===================================
    ==========================================================================================================
     DSC0002.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-85704-1-1.html 上篇帖子: Hadoop 下篇帖子: hadoop集群搭建之SSH无密码登录配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表