【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法
生活随笔
收集整理的這篇文章主要介紹了
【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。
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;} 關于Configuraion,請參見:http://blog.csdn.net/jediael_lu/article/details/38751885 關于UUID,請參見:http://blog.csdn.net/jediael_lu/article/details/38758337
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對象,,并且是通過靜態方法來得到這個對象。 關于單例械,可參考:【設計模式:單例模式】使用單例模式加載properties文件
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);} 關于ToolRunner,請參見:http://blog.csdn.net/jediael_lu/article/details/38751885
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用ToolRunner运行Hadoop
- 下一篇: 【Nutch2.2.1源代码分析之5】索