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


转载于:https://www.cnblogs.com/eaglegeek/p/4557852.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值