zoukankan      html  css  js  c++  java
  • 【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法 分类: H3_NUTCH 2014-08-22 21:57 928人阅读 评论(0) 收藏

    小结:

    (1)在nutch中,一般通过ToolRunner来运行hadoop job,此方法可以方便的通过ToolRunner.run(Configuration conf,Tool tool,String[] args)来加载配置文件。
    (2)conf参数会通过NutchConfiguration.creat()方法创建,此方法先加载hadoop的core-default.xml与core-site.xml,然后再加载nutch-default.xml与nutch-site.xml。



    1、NutchConfiguration.java用于加载及获取Nutch的相关参数。
    Utility to create Hadoop Configurations that include Nutch-specific  resources. 
    即它会加载hadoop及nutch中的参数文件。
    关键是2个create()方法,它加载了参数文件的同时,又返回了Configuration对象。

    2、不带参数的create方法
    public static Configuration create() {
        Configuration conf = new Configuration();
        setUUID(conf);
        addNutchResources(conf);
        return conf;
      }
    首先,new Configuration()时,默认会加载core-default.xml与core-site.xml。
    然后增加UUID这个参数。
    最后增加nutch相关的参数:
     private static Configuration addNutchResources(Configuration conf) {
        conf.addResource("nutch-default.xml");
        conf.addResource("nutch-site.xml");
        return conf;
      }

    3、带参数的create方法
     
    /** Create a {@link Configuration} from supplied properties.
       * @param addNutchResources if true, then first <code>nutch-default.xml</code>,
       * and then <code>nutch-site.xml</code> will be loaded prior to applying the
       * properties. Otherwise these resources won't be used.
       * @param nutchProperties a set of properties to define (or override)
       */
      public static Configuration create(boolean addNutchResources, Properties nutchProperties) {
        Configuration conf = new Configuration();
        setUUID(conf);
        if (addNutchResources) {
          addNutchResources(conf);
        }
        for (Entry<Object, Object> e : nutchProperties.entrySet()) {
          conf.set(e.getKey().toString(), e.getValue().toString());
        }
        return conf;
      }

    此方法根据传入参数决定是否加载core-default.xml与core-site.xml,然后再加载properties中的属性。

    4、NutchConfiguration使用了单例模式,
      private NutchConfiguration() {} // singleton
    通过上述的create方法得到一个Configuration对象。
    事实上,这不是一个典型的单例模式,因为create返回的不是NutchConfiguration对象,而是Configuration对象,,并且是通过静态方法来得到这个对象。

    5、这个类可以参考用作基于hadoop的应用程序的加载配置文件的典型方法。

    6、Nutch中调用NutchConfiguration的方法:
      public static void main(String[] args) throws Exception {
        final int res = ToolRunner.run(NutchConfiguration.create(),
            new SolrIndexerJob(), args);
        System.exit(res);
      }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    安装一个Linux 罗晓杜
    基本正则表达 罗晓杜
    安装MySQL 罗晓杜
    复选框返回选中项ID到后台
    软件工程的理解
    作业一:计科131邵楠
    javascript中函数和方法的区别
    Javascript动画系列之 —— lightbox实现(一)
    如何让自己的javascript代码具有可维护性?
    javascript动画系列 —— 切换图片(原生)
  • 原文地址:https://www.cnblogs.com/lujinhong2/p/4637252.html
Copyright © 2011-2022 走看看