hadoop初学之--------GenericOptionsParser解析器

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


GenericOptionsParser主要方法、属性分析

GenericOptionsParser这个类是从构造函数开始的,它有多个构造函数,真正的处理是在parseGeneralOptions(options, conf, args)这个函数中。

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





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

View parseGeneralOptions Code 
  /**
   * 解析用户指定的参数,获取基本选项以及根据需要修改配置
   * Parse the user-specified options, get the generic options, and modify
   * configuration accordingly
   * @param conf Configuration to be modified
   * @param args User-specified arguments
   * @return Command-specific arguments
   */
  private String[] parseGeneralOptions(Options opts, Configuration conf, 
      String[] args) throws IOException {
      // 指定每个通用选项的属性
    opts = buildGeneralOptions(opts);
    CommandLineParser parser = new GnuParser();
    try {
        // 解析选项,参数,获取命令行
      commandLine = parser.parse(opts, args, true);
      // 根据用户指定的参数(commandLine)修改系统的配置
      processGeneralOptions(conf, commandLine);
      return commandLine.getArgs();
    } catch(ParseException e) {
      LOG.warn("options parsing failed: "+e.getMessage());

      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("general options are: ", opts);
    }
    return args;
  }

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

View processGeneralOptions Code 
 /**
   * 根据用户指定的参数修改配置
   * Modify configuration according user-specified generic options
   * @param conf Configuration to be modified
   * @param line User-specified generic options
   */
  private void processGeneralOptions(Configuration conf,
      CommandLine line) throws IOException {
    if (line.hasOption("fs")) {
        // 设置NAMENODE的ip
      FileSystem.setDefaultUri(conf, line.getOptionValue("fs"));
    }

    if (line.hasOption("jt")) {
      conf.set("mapred.job.tracker", line.getOptionValue("jt"));
    }
    if (line.hasOption("conf")) {
      String[] values = line.getOptionValues("conf");
      for(String value : values) {
        // 新增配置文件,除非是final属性,不然新配置文件会覆盖旧的配置文件
        conf.addResource(new Path(value));
      }
    }
    if (line.hasOption("libjars")) {
      conf.set("tmpjars", 
               validateFiles(line.getOptionValue("libjars"), conf));
      //setting libjars in client classpath
      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()));
      }
    }
    if (line.hasOption("files")) {
      conf.set("tmpfiles", 
               validateFiles(line.getOptionValue("files"), conf));
    }
    if (line.hasOption("archives")) {
      conf.set("tmparchives", 
                validateFiles(line.getOptionValue("archives"), conf));
    }
    if (line.hasOption('D')) {
      String[] property = line.getOptionValues('D');
      for(String prop : property) {
        String[] keyval = prop.split("=", 2);
        if (keyval.length == 2) {
          conf.set(keyval[0], keyval[1]);
        }
      }
    }
    conf.setBoolean("mapred.used.genericoptionsparser", true);
    
    // tokensFile
    if(line.hasOption("tokenCacheFile")) {
      String fileName = line.getOptionValue("tokenCacheFile");
      // check if the local file exists
      try 
      {
        FileSystem localFs = FileSystem.getLocal(conf);
        Path p = new Path(fileName);
        if (!localFs.exists(p)) {
          throw new FileNotFoundException("File "+fileName+" does not exist.");
        }

        LOG.debug("setting conf tokensFile: " + fileName);
        conf.set("mapreduce.job.credentials.json", 
                 localFs.makeQualified(p).toString());
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
 }


原文出处:http://www.cnblogs.com/caoyuanzhanlang


  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
hadoop-eclipse-plugin-2.7.4-jar是Hadoop的一个插件,用于与Eclipse集成以方便开发和调试Hadoop应用程序。Hadoop是一个用于处理大规模数据集的分布式计算框架,它提供了对大数据的高效处理和存储能力。而Eclipse是一个流行的集成开发环境(IDE),广泛应用于Java开发。 通过hadoop-eclipse-plugin-2.7.4-jar,开发人员可以在Eclipse中创建、编辑和管理Hadoop应用程序。该插件提供了一系列功能,例如创建Hadoop项目、在本地运行和调试Hadoop应用程序、上传和下载文件到Hadoop集群等。 对于开发人员来说,使用hadoop-eclipse-plugin-2.7.4-jar可以带来一些好处。首先,它可以提高开发效率。开发人员可以在熟悉的Eclipse环境中编写Hadoop程序,提供更好的开发体验。其次,该插件提供了一些方便的工具和功能,如Hadoop项目模板、自动补全、错误检测和修复等,能够帮助开发人员更快地发现和解决问题。 另外,hadoop-eclipse-plugin-2.7.4-jar还支持与Hadoop集群的集成。开发人员可以通过插件直接与Hadoop集群进行交互,执行MapReduce任务,查看运行日志等。这使得开发人员可以更方便地调试和优化自己的应用程序。 总之,hadoop-eclipse-plugin-2.7.4-jar是一个强大的插件,通过与Eclipse集成,它为开发人员提供了更好的Hadoop开发环境和更高的开发效率。无论是对新手还是有经验的开发人员来说,该插件都是一个有用的工具,可以帮助他们更轻松地开发和调试Hadoop应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值