zoukankan      html  css  js  c++  java
  • 【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法

    小结:

    (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);
      }


  • 相关阅读:
    2. Add Two Numbers
    1. Two Sum
    22. Generate Parentheses (backTracking)
    21. Merge Two Sorted Lists
    20. Valid Parentheses (Stack)
    19. Remove Nth Node From End of List
    18. 4Sum (通用算法 nSum)
    17. Letter Combinations of a Phone Number (backtracking)
    LeetCode SQL: Combine Two Tables
    LeetCode SQL:Employees Earning More Than Their Managers
  • 原文地址:https://www.cnblogs.com/jediael/p/4304062.html
Copyright © 2011-2022 走看看