Nutch 使用入门(三)——配置文件的加载

/** 
 *本人亦初学者,如有不正确的地方请多多指教。谢谢! 
 *部分内容参考自互联网,如有冒犯,请见谅。
 **/ 

 Nutch的配置文件主要有三类:

1.Hadoop的配置文件,Hadoop-default.xml和Hadoop-site.xml。

2.Nutch的配置文件,Nutch-default.xml和Nutch-site.xml。

3.Nutch的插件的配置文件,这些插件的配置文件在加载插件的时候由插件自行加载,如filter的配置文件。

 

配置文件的加载顺序决定了配置文件的优先级,先加载的配置文件优先级低,后加载的配置文件优先级高,优先级低的配置会被优先级高的配置覆盖。因此,了解Nutch配置文件加载的顺序对学习使用Nutch是非常必要的。下面我们通过对Nutch源代码的分析来看看Nutch加载配置文件的过程。

 

Nutch1.0使用入门(一) 介绍了Nutch主要命令--crawl的使用,下面我们就从crawl的main类(org.apache.nutch.crawl.Crawl)的main方法开始分析:

 

Crawl类main方法中加载配置文件的源码如下:

 

Configuration conf = NutchConfiguration.create();
    conf.addResource("crawl-tool.xml");
    JobConf job = new NutchJob(conf);

 

上面代码中,生成了一个NutchConfiguration类的对象,NutchConfiguration是Nutch管理自己配置文件的类,Configuration是Hadoop管理自己配置文件的类。下面我们进入NutchConfiguration类的create()方法。

 

 /** Create a {@link Configuration} for Nutch. */
  public static Configuration create() {
    Configuration conf = new Configuration();
    addNutchResources(conf);
    return conf;
  }

 

create()方法中,先生成了一个Configuration类的对象。请看Configuration类中的源码:

 

 /** A new configuration. */
  public Configuration() {
    this(true);
  }

  /** A new configuration where the behavior of reading from the default 
   * resources can be turned off.
   * 
   * If the parameter {@code loadDefaults} is false, the new instance
   * will not load resources from the default files. 
   * @param loadDefaults specifies whether to load from the default files
   */
  public Configuration(boolean loadDefaults) {
    if (LOG.isDebugEnabled()) {
      LOG.debug(StringUtils.stringifyException(new IOException("config()")));
    }
    if (loadDefaults) {
      resources.add("hadoop-default.xml");
      resources.add("hadoop-site.xml");
    }
  }

 

由上面代码可以看出,在创建Configuration对象的时候,会依次加载hadoop-default.xml和hadoop-site.xml这两个配置文件。所以Hadoop-site.xml中的配置会覆盖hadoop-default.xml中的配置。了解完Hadoop配置文件的加载,我们回到刚才的create()方法里面。创建了Configuration对象后,将调用addNutchResources(conf)方法。

 

/** Add the standard Nutch resources to {@link Configuration}. */
  public static Configuration addNutchResources(Configuration conf) {
    conf.addResource("nutch-default.xml");
    conf.addResource("nutch-site.xml");
    return conf;
  }

 

我们看到,先加载了nutch-default.xml文件,后加载了nutch-site.xml文件。所以nutch-site.xml中的配置会覆盖nutch-default.xml中的配置。下面我们回到crawl类的main方法,继续往下看。调用了conf.addResource("crawl-tool.xml");这表明crawl-tool.xml配置文件是最后加载。

 

通过上面简单的源码分析,我们不难看出Nutch配置文件的优先级。

Nutch自己的配置文件:crawl-tool.xml  >  nutch-site.xml  >  nutch-default.xml

hadoop的配置文件:hadoop-site.xml   >  hadoop-default.xml

 

当然,因为nutch的配置文件后于Hadoop的配置文件的加载,nutch的配置也会覆盖Hadoop配置文件中的配置。需要明白的是覆盖的不是整个配置文件,而是单独的Property。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.Net中有不少开源的爬虫工具,abot就是其中之一。Abot是一个开源的.net爬虫,速度快,易于使用和扩展。项目的地址是 https://github.com/sjdirect/abot 对于爬取的Html,使用的分析工具是CsQuery, CsQuery可以算是.net中实现的Jquery, 可以使用类似Jquery中的方法来处理html页面。CsQuery的项目地址是https://github.com/afeiship/CsQuery一. 对Abot爬虫配置1. 通过属性设置先创建config对象,然后设置config中的各项属性:CrawlConfiguration crawlConfig = new CrawlConfiguration();  crawlConfig.CrawlTimeoutSeconds = 100;  crawlConfig.MaxConcurrentThreads = 10;  crawlConfig.MaxPagesToCrawl = 1000;  crawlConfig.UserAgentString = "abot v1.0 http://code.google.com/p/abot";  crawlConfig.ConfigurationExtensions.Add("SomeCustomConfigValue1", "1111");  crawlConfig.ConfigurationExtensions.Add("SomeCustomConfigValue2", "2222");2. 通过App.config配置直接从配置文件中读取,但是也任然可以在修改各项属性:CrawlConfiguration crawlConfig = AbotConfigurationSectionHandler.LoadFromXml().Convert(); crawlConfig.CrawlTimeoutSeconds = 100;  crawlConfig.MaxConcurrentThreads = 10;3. 应用配置到爬虫对象PoliteWebCrawler crawler = new PoliteWebCrawler(); PoliteWebCrawler crawler = new PoliteWebCrawler(crawlConfig, null, null, null, null, null, null, null);二,使用爬虫,注册各种事件爬虫中主要是4个事件, 页面爬取开始、页面爬取失败、页面不允许爬取事件、页面中的链接不允许爬取事件。下面是示例代码:crawlergeCrawlStartingAsync  = crawler_ProcessPageCrawlStarting;//单个页面爬取开始  crawler.PageCrawlCompletedAsync  = crawler_ProcessPageCrawlCompleted;//单个页面爬取结束  crawler.PageCrawlDisallowedAsync  = crawler_PageCrawlDisallowed;//页面不允许爬取事件  crawler.PageLinksCrawlDisallowedAsync  = crawler_PageLinksCrawlDisallowed;//页面链接不允许爬取事件 void crawler_ProcessPageCrawlStarting(object sender, PageCrawlStartingArgs e) {   PageToCrawl pageToCrawl = e.PageToCrawl;   Console.WriteLine("About to crawl link {0} which was found on page {1}", pageToCrawl.Uri.AbsoluteUri, pageToCrawl.ParentUri.AbsoluteUri); } void crawler_ProcessPageCrawlCompleted(object sender, PageCrawlCompletedArgs e) {   CrawledPage crawledPage = e.CrawledPage;   if (crawledPage.WebException != null || crawledPage.HttpWebResponse.StatusCode != HttpStatusCode.OK)     Console.WriteLine("Crawl of page failed {0}", crawledPage.Uri.AbsoluteUri);   else     Console.WriteLine("Crawl of page succeeded {0}", crawledPage.Uri.AbsoluteUri);   if (string.IsNullOrEmpty(crawledPage.Content.Text))     Console.WriteLine("Page had no content {0}", crawledPage.Uri.AbsoluteUri); } void crawler_PageLinksCrawlDisallowed(object sender, PageLinksCrawlDisallowedArgs e) {   CrawledPage crawledPage = e.CrawledPage;   Console.WriteLine("Did not crawl the links on page {0} due to {1}", crawledPage.Uri.AbsoluteUri, e.DisallowedReason); } void crawler_PageCrawlDisallowed(object sender, PageCrawlDisallowedArgs e) {   PageToCrawl pageToCrawl = e.PageToCrawl;   Console.WriteLine("Did not crawl page {0} due to {1}", pageToCrawl.Uri.AbsoluteUri, e.DisallowedReason); }, 为爬虫添加多个附加对象Abot应该是借鉴了Asp.net MVC中的ViewBag, 也为爬虫对象设置了对象级别的CrwalBag和Page级别的ViewBag.PoliteWebCrawler crawler = new PoliteWebCrawler(); crawler.CrawlBag.MyFoo1 = new Foo();//对象级别的 CrwalBagcrawler.CrawlBag.MyFoo2 = new Foo(); crawler.PageCrawlStartingAsync  = crawler_ProcessPageCrawlStarting; ...void crawler_ProcessPageCrawlStarting(object sender, PageCrawlStartingArgs e) {   //获取CrwalBag中的对象   CrawlContext context = e.CrawlContext;    context.CrawlBag.MyFoo1.Bar();  //使用CrwalBag    context.CrawlBag.MyFoo2.Bar();      //使用页面级别的    PageBag  e.PageToCrawl.PageBag.Bar = new Bar(); }四,启动爬虫启动爬虫非常简单,调用Crawl方法,指定好开始页面,就可以了。CrawlResult result = crawler.Crawl(new Uri("  if (result.ErrorOccurred)         Console.WriteLine("Crawl of {0} completed with error: {1}",          result.RootUri.AbsoluteUri, result.ErrorException.Message         );          else         Console.WriteLine("Crawl of {0} completed without error.", result.RootUri.AbsoluteUri);五,介绍CsQuery在PageCrawlCompletedAsync事件中, e.CrawledPage.CsQueryDocument就是一个CsQuery对象。这里介绍一下CsQuery在分析Html上的优势:cqDocument.Select(".bigtitle > h1")这里的选择器的用法和Jquery完全相同,这里是取class为.bittitle下的h1标签。如果你能熟练的使用Jquery,那么上手CsQuery会非常快和容易。 标签:网络爬虫  网络蜘蛛
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值