【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法
生活随笔
收集整理的這篇文章主要介紹了
【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
小結(jié):
(1)在nutch中,一般通過(guò)ToolRunner來(lái)運(yùn)行hadoop job,此方法可以方便的通過(guò)ToolRunner.run(Configuration conf,Tool tool,String[] args)來(lái)加載配置文件。
(2)conf參數(shù)會(huì)通過(guò)NutchConfiguration.creat()方法創(chuàng)建,此方法先加載hadoop的core-default.xml與core-site.xml,然后再加載nutch-default.xml與nutch-site.xml。
2、不帶參數(shù)的create方法 public static Configuration create() {Configuration conf = new Configuration();setUUID(conf);addNutchResources(conf);return conf;} 首先,new Configuration()時(shí),默認(rèn)會(huì)加載core-default.xml與core-site.xml。 然后增加UUID這個(gè)參數(shù)。 最后增加nutch相關(guān)的參數(shù): private static Configuration addNutchResources(Configuration conf) {conf.addResource("nutch-default.xml");conf.addResource("nutch-site.xml");return conf;} 關(guān)于Configuraion,請(qǐng)參見(jiàn):http://blog.csdn.net/jediael_lu/article/details/38751885 關(guān)于UUID,請(qǐng)參見(jiàn):http://blog.csdn.net/jediael_lu/article/details/38758337
3、帶參數(shù)的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;}
此方法根據(jù)傳入?yún)?shù)決定是否加載core-default.xml與core-site.xml,然后再加載properties中的屬性。
4、NutchConfiguration使用了單例模式, private NutchConfiguration() {} // singleton 通過(guò)上述的create方法得到一個(gè)Configuration對(duì)象。 事實(shí)上,這不是一個(gè)典型的單例模式,因?yàn)閏reate返回的不是NutchConfiguration對(duì)象,而是Configuration對(duì)象,,并且是通過(guò)靜態(tài)方法來(lái)得到這個(gè)對(duì)象。 關(guān)于單例械,可參考:【設(shè)計(jì)模式:單例模式】使用單例模式加載properties文件
5、這個(gè)類(lèi)可以參考用作基于hadoop的應(yīng)用程序的加載配置文件的典型方法。
6、Nutch中調(diào)用NutchConfiguration的方法: public static void main(String[] args) throws Exception {final int res = ToolRunner.run(NutchConfiguration.create(),new SolrIndexerJob(), args);System.exit(res);} 關(guān)于ToolRunner,請(qǐng)參見(jiàn):http://blog.csdn.net/jediael_lu/article/details/38751885
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用ToolRunner运行Hadoop
- 下一篇: 【Nutch2.2.1源代码分析之5】索