`

搜索引擎Nutch源代码研究之一 网页抓取(3)

阅读更多
今天我们看看Nutch网页抓取,所用的几种数据结构:
主要涉及到了这几个类:FetchListEntry,Page,
首先我们看看FetchListEntry类:
public final class FetchListEntry implements Writable, Cloneable
实现了Writable, Cloneable接口,Nutch许多类实现了Writable, Cloneable。
自己负责自己的读写操作其实是个很合理的设计方法,分离出来反倒有很琐碎
的感觉。
看看里面的成员变量:
Java代码 复制代码
  1. public static final String DIR_NAME = "fetchlist";//要写入磁盘的目录   
  2. private final static byte CUR_VERSION = 2;//当前的版本号   
  3. private boolean fetch;//是否抓取以便以后更新   
  4. private Page page;//当前抓取的页面   
  5. private String[] anchors;//抓取到的该页面包含的链接  
public static final String DIR_NAME = "fetchlist";//要写入磁盘的目录
private final static byte CUR_VERSION = 2;//当前的版本号
private boolean fetch;//是否抓取以便以后更新
private Page page;//当前抓取的页面
private String[] anchors;//抓取到的该页面包含的链接

我们看看如何读取各个字段的,也就是函数
public final void readFields(DataInput in) throws IOException
读取version 字段,并判断如果版本号是否大约当前的版本号,则抛出版本不匹配的异常,
然后读取fetch 和page 字段。
判断如果版本号大于1,说明anchors已经保存过了,读取anchors,否则直接赋值一个空的字符串
代码如下:
Java代码 复制代码
  1. byte version = in.readByte();                 // read version   
  2. if (version > CUR_VERSION)                    // check version   
  3.   throw new VersionMismatchException(CUR_VERSION, version);   
  4.   
  5. fetch = in.readByte() != 0;                   // read fetch flag   
  6.   
  7. page = Page.read(in);                         // read page   
  8.   
  9. if (version > 1) {                            // anchors added in version 2   
  10.   anchors = new String[in.readInt()];         // read anchors   
  11.   for (int i = 0; i < anchors.length; i++) {   
  12.     anchors[i] = UTF8.readString(in);   
  13.   }   
  14. else {   
  15.   anchors = new String[0];   
  16. }   
    byte version = in.readByte();                 // read version
    if (version > CUR_VERSION)                    // check version
      throw new VersionMismatchException(CUR_VERSION, version);

    fetch = in.readByte() != 0;                   // read fetch flag

    page = Page.read(in);                         // read page

    if (version > 1) {                            // anchors added in version 2
      anchors = new String[in.readInt()];         // read anchors
      for (int i = 0; i < anchors.length; i++) {
        anchors[i] = UTF8.readString(in);
      }
    } else {
      anchors = new String[0];
    }
 

同时还提供了一个静态的读取各个字段的函数,并构建出FetchListEntry对象返回:
Java代码 复制代码
  1. public static FetchListEntry read(DataInput in) throws IOException {   
  2.     FetchListEntry result = new FetchListEntry();   
  3.     result.readFields(in);   
  4.     return result;   
  5. }  
public static FetchListEntry read(DataInput in) throws IOException {
    FetchListEntry result = new FetchListEntry();
    result.readFields(in);
    return result;
}

写得代码则比较易看,分别写每个字段:
Java代码 复制代码
  1. public final void write(DataOutput out) throws IOException {   
  2.     out.writeByte(CUR_VERSION);                   // store current version   
  3.     out.writeByte((byte)(fetch ? 1 : 0));         // write fetch flag   
  4.     page.write(out);                              // write page   
  5.     out.writeInt(anchors.length);                 // write anchors   
  6.     for (int i = 0; i < anchors.length; i++) {   
  7.       UTF8.writeString(out, anchors[i]);   
  8.     }   
  9.   }  
public final void write(DataOutput out) throws IOException {
    out.writeByte(CUR_VERSION);                   // store current version
    out.writeByte((byte)(fetch ? 1 : 0));         // write fetch flag
    page.write(out);                              // write page
    out.writeInt(anchors.length);                 // write anchors
    for (int i = 0; i < anchors.length; i++) {
      UTF8.writeString(out, anchors[i]);
    }
  }

其他的clone和equals函数实现的也非常易懂。
下面我们看看Page类的代码:
public class Page implements WritableComparable, Cloneable
和FetchListEntry一样同样实现了Writable, Cloneable接口,我们看看Nutch的注释,我们就非常容易知道各个字段的意义了:
Java代码 复制代码
  1. /*********************************************  
  2.  * A row in the Page Database.  
  3.  * <pre>  
  4.  *   type   name    description  
  5.  * ---------------------------------------------------------------  
  6.  *   byte   VERSION  - A byte indicating the version of this entry.  
  7.  *   String URL      - The url of a page.  This is the primary key.  
  8.  *   128bit ID       - The MD5 hash of the contents of the page.  
  9.  *   64bit  DATE     - The date this page should be refetched.  
  10.  *   byte   RETRIES  - The number of times we've failed to fetch this page.  
  11.  *   byte   INTERVAL - Frequency, in days, this page should be refreshed.  
  12.  *   float  SCORE   - Multiplied into the score for hits on this page.  
  13.  *   float  NEXTSCORE   - Multiplied into the score for hits on this page.  
  14.  * </pre>  
  15.  *  
  16.  * @author Mike Cafarella  
  17.  * @author Doug Cutting  
  18.  *********************************************/  
/*********************************************
 * A row in the Page Database.
 * <pre>
 *   type   name    description
 * ---------------------------------------------------------------
 *   byte   VERSION  - A byte indicating the version of this entry.
 *   String URL      - The url of a page.  This is the primary key.
 *   128bit ID       - The MD5 hash of the contents of the page.
 *   64bit  DATE     - The date this page should be refetched.
 *   byte   RETRIES  - The number of times we've failed to fetch this page.
 *   byte   INTERVAL - Frequency, in days, this page should be refreshed.
 *   float  SCORE   - Multiplied into the score for hits on this page.
 *   float  NEXTSCORE   - Multiplied into the score for hits on this page.
 * </pre>
 *
 * @author Mike Cafarella
 * @author Doug Cutting
 *********************************************/

各个字段:
Java代码 复制代码
  1. private final static byte CUR_VERSION = 4;   
  2.   private static final byte DEFAULT_INTERVAL =   
  3.     (byte)NutchConf.get().getInt("db.default.fetch.interval"30);   
  4.   
  5.   private UTF8 url;   
  6.   private MD5Hash md5;   
  7.   private long nextFetch = System.currentTimeMillis();   
  8.   private byte retries;   
  9.   private byte fetchInterval = DEFAULT_INTERVAL;   
  10.   private int numOutlinks;   
  11.   private float score = 1.0f;   
  12.   private float nextScore = 1.0f;  
private final static byte CUR_VERSION = 4;
  private static final byte DEFAULT_INTERVAL =
    (byte)NutchConf.get().getInt("db.default.fetch.interval", 30);

  private UTF8 url;
  private MD5Hash md5;
  private long nextFetch = System.currentTimeMillis();
  private byte retries;
  private byte fetchInterval = DEFAULT_INTERVAL;
  private int numOutlinks;
  private float score = 1.0f;
  private float nextScore = 1.0f;

同样看看他是如何读取自己的各个字段的,其实代码加上本来提供的注释,使很容易看懂的,不再详述:
Java代码 复制代码
  1. ublic void readFields(DataInput in) throws IOException {   
  2.     byte version = in.readByte();                 // read version   
  3.     if (version > CUR_VERSION)                    // check version   
  4.       throw new VersionMismatchException(CUR_VERSION, version);   
  5.   
  6.     url.readFields(in);   
  7.     md5.readFields(in);   
  8.     nextFetch = in.readLong();   
  9.     retries = in.readByte();   
  10.     fetchInterval = in.readByte();   
  11.     numOutlinks = (version > 2) ? in.readInt() : 0// added in Version 3   
  12.     score = (version>1) ? in.readFloat() : 1.0f;  // score added in version 2   
  13.     nextScore = (version>3) ? in.readFloat() : 1.0f;  // 2nd score added in V4   
  14.   }  
ublic void readFields(DataInput in) throws IOException {
    byte version = in.readByte();                 // read version
    if (version > CUR_VERSION)                    // check version
      throw new VersionMismatchException(CUR_VERSION, version);

    url.readFields(in);
    md5.readFields(in);
    nextFetch = in.readLong();
    retries = in.readByte();
    fetchInterval = in.readByte();
    numOutlinks = (version > 2) ? in.readInt() : 0; // added in Version 3
    score = (version>1) ? in.readFloat() : 1.0f;  // score added in version 2
    nextScore = (version>3) ? in.readFloat() : 1.0f;  // 2nd score added in V4
  }

写各个字段也很直接:
Java代码 复制代码
  1. public void write(DataOutput out) throws IOException {   
  2.     out.writeByte(CUR_VERSION);                   // store current version   
  3.     url.write(out);   
  4.     md5.write(out);   
  5.     out.writeLong(nextFetch);   
  6.     out.write(retries);   
  7.     out.write(fetchInterval);   
  8.     out.writeInt(numOutlinks);   
  9.     out.writeFloat(score);   
  10.     out.writeFloat(nextScore);   
  11.   }  
public void write(DataOutput out) throws IOException {
    out.writeByte(CUR_VERSION);                   // store current version
    url.write(out);
    md5.write(out);
    out.writeLong(nextFetch);
    out.write(retries);
    out.write(fetchInterval);
    out.writeInt(numOutlinks);
    out.writeFloat(score);
    out.writeFloat(nextScore);
  }

我们顺便看看提供方便读写Fetch到的内容的类FetcherOutput:这个类通过委托前面介绍的两个类的读写,提供了Fetche到的
各种粒度结构的读写功能,代码都比较直接,不再详述。
下次我们看看parse-html插件,看看Nutch是如何提取html页面的。
评论
3 楼 sharong 2008-03-17   引用
或者是要安装一些nutch的插件?
2 楼 fuliang 2007-12-15   引用
另外补充一下Content类:
public final class Content extends VersionedWritable
我们看到继承了VersionedWritable类。VersionedWritable类实现了版本字段的读写功能。
我们先看看成员变量:
Java代码 复制代码
  1. public static final String DIR_NAME = "content";   
  2. private final static byte VERSION = 1;   
  3. private String url;   
  4. private String base;   
  5. private byte[] content;   
  6. private String contentType;   
  7. private Properties metadata;  
  public static final String DIR_NAME = "content";
  private final static byte VERSION = 1;
  private String url;
  private String base;
  private byte[] content;
  private String contentType;
  private Properties metadata;

DIR_NAME 为Content保存的目录,
VERSION 为版本常量
url为该Content所属页面的url
base为该Content所属页面的base url
contentType为该Content所属页面的contentType
metadata为该Content所属页面的meta信息

下面我们看看Content是如何读写自身的字段的:
public final void readFields(DataInput in) throws IOException
这个方法功能为读取自身的各个字段
Java代码 复制代码
  1. super.readFields(in);                         // check version   
  2.   
  3.     url = UTF8.readString(in);                    // read url   
  4.     base = UTF8.readString(in);                   // read base   
  5.   
  6.     content = WritableUtils.readCompressedByteArray(in);   
  7.   
  8.     contentType = UTF8.readString(in);            // read contentType   
  9.   
  10.     int propertyCount = in.readInt();             // read metadata   
  11.     metadata = new Properties();   
  12.     for (int i = 0; i < propertyCount; i++) {   
  13.       metadata.put(UTF8.readString(in), UTF8.readString(in));   
  14.     }  
super.readFields(in);                         // check version

    url = UTF8.readString(in);                    // read url
    base = UTF8.readString(in);                   // read base

    content = WritableUtils.readCompressedByteArray(in);

    contentType = UTF8.readString(in);            // read contentType

    int propertyCount = in.readInt();             // read metadata
    metadata = new Properties();
    for (int i = 0; i < propertyCount; i++) {
      metadata.put(UTF8.readString(in), UTF8.readString(in));
    }

代码加注释之后基本上比较清晰了.
super.readFields(in);       
这句调用父类VersionedWritable读取并验证版本号
写的代码也比较简单:
Java代码 复制代码
  1. public final void write(DataOutput out) throws IOException {   
  2.     super.write(out);                             // write version   
  3.   
  4.     UTF8.writeString(out, url);                   // write url   
  5.     UTF8.writeString(out, base);                  // write base   
  6.   
  7.     WritableUtils.writeCompressedByteArray(out, content); // write content   
  8.   
  9.     UTF8.writeString(out, contentType);           // write contentType   
  10.        
  11.     out.writeInt(metadata.size());                // write metadata   
  12.     Iterator i = metadata.entrySet().iterator();   
  13.     while (i.hasNext()) {   
  14.       Map.Entry e = (Map.Entry)i.next();   
  15.       UTF8.writeString(out, (String)e.getKey());   
  16.       UTF8.writeString(out, (String)e.getValue());   
  17.     }   
  18.   }  
public final void write(DataOutput out) throws IOException {
    super.write(out);                             // write version

    UTF8.writeString(out, url);                   // write url
    UTF8.writeString(out, base);                  // write base

    WritableUtils.writeCompressedByteArray(out, content); // write content

    UTF8.writeString(out, contentType);           // write contentType
    
    out.writeInt(metadata.size());                // write metadata
    Iterator i = metadata.entrySet().iterator();
    while (i.hasNext()) {
      Map.Entry e = (Map.Entry)i.next();
      UTF8.writeString(out, (String)e.getKey());
      UTF8.writeString(out, (String)e.getValue());
    }
  }
       
其实这些类主要是它的字段.以及怎样划分各个域模型的
分享到:
评论

相关推荐

    搜索引擎nutch配置

    从Apache官方网站下载Nutch的最新源代码,通常通过Git克隆仓库。解压后,进入Nutch的工作目录。 3. **配置Nutch** 打开`conf/nutch-site.xml`文件,这是Nutch的主要配置文件。以下是一些关键配置项: - `...

    Lucene+nutch搜索引擎开发(源代码)

    2. **Nutch源代码**:包括Nutch的爬虫模块、索引模块和搜索模块,可以帮助开发者学习如何配置和运行一个完整的网络爬虫,以及如何与Lucene集成进行全文检索。 3. **示例项目**:可能包含了一些示例应用,展示如何...

    nutch-2.1源代码

    Nutch不仅仅是一个搜索引擎,它还包含了一个Web爬虫,能够抓取互联网上的网页,并对抓取的数据进行索引和处理。 Nutch的源代码包含了整个项目的完整实现,包括爬虫、索引器、搜索器以及相关的配置和文档。这对于...

    nutch的源代码解析

    Nutch 的源代码解析对于深入理解搜索引擎的工作原理以及自定义搜索引擎的实现非常有帮助。下面我们将详细探讨 Nutch 的注入(Injector)过程,这是整个爬取流程的第一步。 Injector 类在 Nutch 中的作用是将输入的 ...

    Lucene+Nutch搜索引擎开发.王学松源代码

    总的来说,王学松的“Lucene+Nutch搜索引擎开发实例代码”是一份宝贵的教育资源,它可以帮助开发者快速入门搜索引擎开发,并深入了解这两个项目的内部工作机制。通过实践这些代码,不仅可以提升技术能力,还能为构建...

    分布式搜索引擎nutch开发

    分布式搜索引擎Nutch开发详解 Nutch是一款开源的、基于Java实现的全文搜索引擎,它主要用于构建大规模的网络爬虫系统,并提供了对抓取的网页进行索引和搜索的功能。Nutch与Hadoop紧密集成,能够充分利用分布式计算...

    lucene+nutch搜索引擎开发源码1

    《lucene+nutch搜索引擎开发源码1》是一个包含开源搜索引擎项目Lucene和Nutch源代码的压缩包,主要针对搜索引擎开发的学习和实践。这个压缩包是书籍《lucene+nutch搜索引擎开发》的一部分,由于源码量较大,因此分为...

    lucene+nutch开发自己的搜索引擎一书源代码

    《lucene+nutch开发自己的搜索引擎一书源代码》是一份专为初学者设计的资源,旨在教授如何利用Apache Lucene和Nutch构建自定义搜索引擎。Lucene是Java编写的一个高性能全文检索库,而Nutch则是一个开源的网络爬虫...

    基于Nutch的中文搜索引擎的构建

    2. **学习价值**:尽管没有谷歌这样的大型商业搜索引擎的源代码可供学习,但通过研究`Nutch`可以深入了解分布式搜索引擎的工作原理和技术细节。 3. **扩展性**:`Nutch`具有很高的灵活性和可扩展性,用户可以根据...

    nutch 1.5的源代码

    Nutch 1.5 是一个基于Java开发的开源搜索引擎项目,它主要负责网络抓取、索引和搜索等功能。这个源代码包包含了实现这些功能的所有模块和组件,为开发者提供了深入理解搜索引擎工作原理以及定制化搜索引擎的机会。接...

    nutch开发资料 搜索引擎

    Nutch是Apache软件基金会的一个开源项目,主要用于构建网络搜索引擎。这个开发资料压缩包包含了与Nutch相关的源代码和可能的配置文件,可以帮助开发者深入了解和学习Nutch的工作原理以及如何进行定制化开发。以下是...

    开发自己的搜索引擎 lunenc nutch

    Lucene 是一个全文搜索引擎库,而 Nutch 是一个完整的网络爬虫项目,两者结合可以提供从网页抓取到索引、搜索的一站式解决方案。 在开发自定义搜索引擎的过程中,首先我们需要了解 **Lucene** 的基本原理。Lucene ...

    Nutch 是一个开源Java 实现的搜索引擎(学习资料)----下载不扣分,回帖加1分,童叟无欺,欢迎下载 。不下也来看看啊!!

    1. **Java编程基础**:由于Nutch是用Java编写的,因此理解和修改Nutch源代码需要扎实的Java基础知识。 2. **搜索引擎原理**:理解搜索引擎的基本工作流程,包括爬虫、预处理、索引和查询处理。 3. **Hadoop和...

    Lucene.Nutch搜索引擎开发

    2. **Nutch**: Nutch是一个开放源代码的网络爬虫,主要用于抓取和索引网页内容。它基于Lucene,提供了完整的爬虫解决方案,包括网页抓取、预处理(如HTML解析、链接分析、去重等)、索引和搜索功能。Nutch的主要优势...

    nutch解决搜索结果高亮和网页快照链接无效及网页变形

    Nutch 是一个开源的搜索引擎项目,它提供了网络爬虫、索引和搜索的功能。在构建一个自定义的搜索引擎时,可能会遇到几个常见的问题,如搜索结果的关键词高亮、快照链接无效以及网页在预览时的变形。下面将详细讨论...

    Lucene+Nutch搜索引擎开发(配套光盘资源)

    《Lucene+Nutch搜索引擎开发》是一本专注于搜索引擎技术的书籍,配套光盘资源为学习者提供了丰富的实践材料,尤其对于想要深入理解Nutch搜索引擎开发的读者来说,这是一份不可多得的学习资料。Nutch是基于Apache ...

    Nutch:一个灵活可扩展的开源web搜索引擎

    总之,Nutch是一个强大的开源搜索引擎工具,它不仅适用于构建自己的搜索引擎,也是研究和学习搜索引擎技术的理想平台。通过熟悉其工作流程、分析源代码以及查阅相关文档,你将能够掌握搜索引擎的核心技术和实现细节...

    apache-nutch-1.4-src.tar.gz_nutch_搜索引擎

    Apache Nutch 是一款高度可扩展的开源全文搜索引擎框架,它为构建自定义的网络爬虫和搜索引擎提供了强大的工具集。Nutch 的设计目标是处理大量网页数据,进行高效的抓取、索引和搜索操作。在“apache-nutch-1.4-src....

    nutch使用&Nutch;入门教程

    在使用Nutch之前,你需要配置Nutch的运行环境,包括安装Java、设置Hadoop(如果需要分布式爬取)、下载和编译Nutch源代码。还需要配置Nutch的`conf/nutch-site.xml`文件,指定抓取策略、存储路径、爬虫范围等参数。 ...

Global site tag (gtag.js) - Google Analytics