现在的位置: 首页 > 综合 > 正文

Nutch 1.0 Fetcher 抓取模型解析

2013年09月06日 ⁄ 综合 ⁄ 共 5966字 ⁄ 字号 评论关闭
文章目录

Nutch 1.0 Fetcher 抓取模型解析

-----------------------------

 

1. 介绍

2. 抓取流程分析

3. 结束

---------------

1. 介绍

   Nutch是apache Lucene的一个子项目,它主要用来进行网页数据的收集和索引。它用结合apache的Hadoop和Lucene等子项目。Nutch的一般抓取流程如下:

   1. 把初始网址inject到crawlDb中进行准备抓取

   2. 用generate模块对crawlDb中的网址进行过滤

   3. 用fetcher 模块对generate产生的网页进行抓取

   4. 用parse后的网页进行解析

   5. 用crawlDb的update工具对抓取网页中的外链接进行更新,使之成为下一轮抓取的种子

 

    本文主要介绍一下Nutch中Fetch流程。流程一般介绍如下:

 

Nutch的抓取方法是通过基于多队列的生产者与消费者线程模型。

/**

 * A queue-based fetcher.

 * 

 * <p>

 * This fetcher uses a well-known model of one producer (a QueueFeeder) and many

 * consumers (FetcherThread-s).

 * 

 * <p>

 * QueueFeeder reads input fetchlists and populates a set of FetchItemQueue-s,

 * which hold FetchItem-s that describe the items to be fetched. There are as

 * many queues as there are unique hosts, but at any given time the total number

 * of fetch items in all queues is less than a fixed number (currently set to a

 * multiple of the number of threads).

 * 

 * <p>

 * As items are consumed from the queues, the QueueFeeder continues to add new

 * input items, so that their total count stays fixed (FetcherThread-s may also

 * add new items to the queues e.g. as a results of redirection) - until all

 * input items are exhausted, at which point the number of items in the queues

 * begins to decrease. When this number reaches 0 fetcher will finish.

 * 

 * <p>

 * This fetcher implementation handles per-host blocking itself, instead of

 * delegating this work to protocol-specific plugins. Each per-host queue

 * handles its own "politeness" settings, such as the maximum number of

 * concurrent requests and crawl delay between consecutive requests - and also a

 * list of requests in progress, and the time the last request was finished. As

 * FetcherThread-s ask for new items to be fetched, queues may return eligible

 * items or null if for "politeness" reasons this host's queue is not yet ready.

 * 

 * <p>

 * If there are still unfetched items in the queues, but none of the items are

 * ready, FetcherThread-s will spin-wait until either some items become

 * available, or a timeout is reached (at which point the Fetcher will abort,

 * assuming the task is hung).

 * 

 * @author Andrzej Bialecki

 */

 

 2. 抓取流程解析

 在Nutch包的org.apache.nutch中有一个Fetcher.java,它就是用来对generate产生的网页进行抓取的。

 2.1 Main函数

 其中有一个Mian方法,主要有三处参数,segment,threads和noParsing

 

 

 

 

  2.2 Fecther类中的fetch方法

  这个方法主要用于对MapReduce的一些初始设置和启动Fecther的run方法,主要代码如下:

  

2.3 Fetcher中的run方法

主要是用于启动生产者-消费者这个线程模型,这里的生产者是QueueFeeder,用于收集input得到的数据(网页地址元信息)放到多个队列中去,这里的队列ID是用queueID = proto + "://" + host;协议类型和host来组成唯一的队列ID。

 

 

2.4 QueueFeeder的run方法

主要代码如下:

 

2.5 FetcherThread的run方法

   从fetchQueues中读出一个Item,对其进行抓取,如果抓取成功,就从fetchQueue队列中删除,如果不成功,就做相应的处理,这里的判断条件都是根据抓取协议的返回状态来做判断的。具体的抓取协议类型都是从Nutch的插件库中读出来的。

还有一点要注意的是FetchItemQueue有两个队列,一个是queue用于存储Item项,另一个是inProgress列队,用于存储正在被抓取的项,当要从queue队列中得到一个抓取项时,它会从queue队列中把待抓取项移出后放入inProgress队列中,如果当inProgress列队中项大于最大线程数时,就停止返回数据项,这样可以防止很多数据项在等待抓取。在FetchItemQueue中还有一个要注意的是它有一个nextFetchTime,它是有来控制抓取间隔的。

2.5 FetcherThread中的output方法

把抓取的数据写到output中,这就是Map的输出,输出格式就是前面job中定义的job.setOutputKeyClass(Text.class);job.setOutputValueClass(NutchWritable.class);两个方法。

 

3. 结束

本文只是对Nutch流程的一个简单的介绍,其中一些细节还没有还得急展开。如FetcherOutputFormat等类的使用,这个会在下一次整理好后发出。也希望有兴趣的同学一起来讨论。

 

抱歉!评论已关闭.