`
chengqianl
  • 浏览: 52470 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

nutch1.2 index 详解

阅读更多
首先如果存在crawl/index ,crawl/indexes目录则删除


map:IndexerMapReduce
     map输入目录为  所有的segment的crawl_fetch  crawl_parse parse_data parse_text , crawl/crawldb/current, crawl/linkdb/current
     1 map的任务就是为了合并目录代码如下
       output.collect(key, new NutchWritable(value));
reduce: IndexerMapReduce
     1 循环 解析出路 四个对象 就是抓取和解析成功
   if (fetchDatum == null || dbDatum == null
        || parseText == null || parseData == null) {
      return;                                     // only have inlinks
    }

   2 如果抓取成功和解析成功 往下执行
   if (!parseData.getStatus().isSuccess() ||
        fetchDatum.getStatus() != CrawlDatum.STATUS_FETCH_SUCCESS) {
      return;
    }
   3 创建NutchDocument 加入segment ,签名,field
    4 通过IndexingFilters,这个filters,会调用配置的BasicIndexingFilter和AnchorIndexingFilter,filter方法,
    5  BasicIndexingFilter设置host ,site ,url ,content,title 长度超过indexer.max.title.length会最title做截取,设置tstamp,
   6 AnchorIndexingFilter设置anchor
   7 如果doc不为空掉用ScoringFilters 设置boost,weight
   8 写入,这里的    job.setOutputFormat(IndexerOutputFormat.class);
IndexerOutputFormat 的方法如下

  @Override
  public RecordWriter<Text, NutchDocument> getRecordWriter(FileSystem ignored,
      JobConf job, String name, Progressable progress) throws IOException {
   
    // populate JobConf with field indexing options
    IndexingFilters filters = new IndexingFilters(job);
   
    final NutchIndexWriter[] writers =
      NutchIndexWriterFactory.getNutchIndexWriters(job);
    for (final NutchIndexWriter writer : writers) {
      writer.open(job, name);
    }
    return new RecordWriter<Text, NutchDocument>() {

      public void close(Reporter reporter) throws IOException {
        for (final NutchIndexWriter writer : writers) {
          writer.close();
        }
      }

      public void write(Text key, NutchDocument doc) throws IOException {
        for (final NutchIndexWriter writer : writers) {
          writer.write(doc);
        }
      }
    };
  }
如果粗体所示 他会使用 LuceneWriter 如下代码加入到
@SuppressWarnings("unchecked")
  public static NutchIndexWriter[] getNutchIndexWriters(Configuration conf) {
    final String[] classes = conf.getStrings("indexer.writer.classes");
    final NutchIndexWriter[] writers = new NutchIndexWriter[classes.length];
    for (int i = 0; i < classes.length; i++) {
      final String clazz = classes[i];
      try {
        final Class<NutchIndexWriter> implClass =
          (Class<NutchIndexWriter>) Class.forName(clazz);
        writers[i] = implClass.newInstance();
      } catch (final Exception e) {
        throw new RuntimeException("Couldn't create " + clazz, e);
      }
    }
    return writers;
  }

  public static void addClassToConf(Configuration conf,
                                    Class<? extends NutchIndexWriter> clazz) {
    final String classes = conf.get("indexer.writer.classes");
    final String newClass = clazz.getName();

    if (classes == null) {
      conf.set("indexer.writer.classes", newClass);
    } else {
      conf.set("indexer.writer.classes", classes + "," + newClass);
    }

  }

NutchIndexWriterFactory.addClassToConf(job, LuceneWriter.class);
打开indexwriter的方法
for (final NutchIndexWriter writer : writers) {
      writer.open(job, name);
    }


代码如下
public void open(JobConf job, String name)
  throws IOException {
    this.fs = FileSystem.get(job);
    perm = new Path(FileOutputFormat.getOutputPath(job), name);
    temp = job.getLocalPath("index/_"  +
                      Integer.toString(new Random().nextInt()));

    fs.delete(perm, true); // delete old, if any
    analyzerFactory = new AnalyzerFactory(job);
    writer = new IndexWriter(
        FSDirectory.open(new File(fs.startLocalOutput(perm, temp).toString())),
        new NutchDocumentAnalyzer(job), true, MaxFieldLength.UNLIMITED);

    writer.setMergeFactor(job.getInt("indexer.mergeFactor", 10));
    writer.setMaxBufferedDocs(job.getInt("indexer.minMergeDocs", 100));
    writer.setMaxMergeDocs(job
        .getInt("indexer.maxMergeDocs", Integer.MAX_VALUE));
    writer.setTermIndexInterval(job.getInt("indexer.termIndexInterval", 128));
    writer.setMaxFieldLength(job.getInt("indexer.max.tokens", 10000));
    writer.setInfoStream(LogUtil.getDebugStream(Indexer.LOG));
    writer.setUseCompoundFile(false);
    writer.setSimilarity(new NutchSimilarity());

    processOptions(job);
  }
写入代码如下

  public void write(NutchDocument doc) throws IOException {
    final Document luceneDoc = createLuceneDoc(doc);
    final NutchAnalyzer analyzer = analyzerFactory.get(luceneDoc.get("lang"));
    if (Indexer.LOG.isDebugEnabled()) {
      Indexer.LOG.debug("Indexing [" + luceneDoc.get("url")
          + "] with analyzer " + analyzer + " (" + luceneDoc.get("lang")
          + ")");
    }
    writer.addDocument(luceneDoc, analyzer);

  }
通过上面的流程就把索引写好了
  • 大小: 42.3 KB
  • 大小: 50.1 KB
分享到:
评论

相关推荐

    nutch1.2 java的project

    1. **导入项目**:在Eclipse中选择“File” &gt; “Import” &gt; “Existing Projects into Workspace”,然后浏览到下载的`nutch1.2+Project`目录,导入项目。 2. **添加库**:确保你的Eclipse环境中已经安装了Apache ...

    nutch1.2 java project

    Nutch 1.2 是一个开源的网络爬虫项目,基于 Java 编写,用于抓取互联网上的网页并建立索引。这个项目是 Apache Software Foundation 的一部分,它为大规模的数据采集提供了强大的工具。Nutch 1.2 版本相对于早期版本...

    Nutch 1.2源码阅读

    ### Nutch 1.2 源码阅读深入解析 #### Crawl类核心作用与流程概览 在深入了解Nutch 1.2源码之前,我们先明确Nutch的架构和工作流程。Nutch作为一款开源搜索引擎框架,其功能涵盖网页抓取、索引构建以及查询处理。...

    nutch1.2源码

    Nutch 1.2是该项目的一个稳定版本,提供了许多改进和优化,使得它在搜索引擎构建、数据分析等领域具有广泛应用。 一、Nutch概述 Nutch是由Apache软件基金会开发的开源Web爬虫项目,主要用于抓取互联网上的网页并...

    myeclipse8.5导入nutch1.2源码

    - 在 Default output folder 设置中,将输出目录更改为 `nutch1.2/bin/tmp_nutch`。 - 转到 Libraries 标签页,点击 Add Class Folder,选择 `nutch1.2/conf` 目录。 3. **调整库顺序**: - 在 Order and Export...

    nutch1.2测试文档

    nutch1.2测试文档

    nutch-1.2.war

    nutch官方简单案例,请版本是nutch-1.2.war

    Windows下cygwin+MyEclipse 8.5+Nutch1.2+Tomcat 6.0

    ### Windows下cygwin+MyEclipse 8.5+Nutch1.2+Tomcat 6.0 本文旨在详细介绍如何在Windows环境下搭建基于cygwin、MyEclipse 8.5、Nutch 1.2及Tomcat 6.0的开发环境,并对每个步骤进行深入解析。 #### 一、Cygwin的...

    nutch-1.2.part02

    nutch Nutch是一个由Java实现的,刚刚诞生开放源代码(open-source)的web搜索引擎。 尽管Web搜索是漫游Internet的基本要求, 但是现有web搜索引擎的数目却在下降。 并且这很有可能进一步演变成为一个公司垄断了几乎...

    实验报告(利用Nutch和IKanalyzer构造中文分词搜索引擎)

    尝试使用Nutch 0.9和IKAnalyzer 3.1.6GA组合,但由于版本兼容性问题导致失败,因此改用Nutch 1.2和IKAnalyzer 3.2.8,并将Tomcat升级到6.0.35版本。 在Nutch 1.2中集成IKAnalyzer,需要修改NutchAnalysis.jj文件,...

    Nutch搜索引擎培训讲义

    - 选择“Source”选项卡,将默认输出目录从`nutch1.2/bin`修改为`nutch1.2/_bin`。 - 对于bin文件夹,可以通过右键点击“Team” &gt; “Restore”来恢复其内容。 3. **添加JAR包** - 通过“Add JARs”功能,将`...

    nutch-1.2.part06

    nutch Nutch是一个由Java实现的,刚刚诞生开放源代码(open-source)的web搜索引擎。 尽管Web搜索是漫游Internet的基本要求, 但是现有web搜索引擎的数目却在下降。 并且这很有可能进一步演变成为一个公司垄断了几乎...

    nutch部分网页乱码BUG修正

    Nutch是Apache开发的一款开源网络爬虫项目,用于抓取互联网上的网页并建立索引,以便于搜索引擎进行数据处理。然而,在实际使用过程中,由于编码问题,Nutch可能会出现部分网页乱码的情况。本篇文章将深入探讨这个...

    搭建nutch web开发环境

    本教程将详细介绍如何搭建Nutch 1.2的Web开发环境,因为从Nutch 1.3版本开始,Web界面部分已被移除。 首先,我们需要理解Nutch的工作流程,它主要包括五个主要步骤:抓取、解析、索引、查询和排名。Nutch提供了强大...

    nutch-1.2.part07

    nutch Nutch是一个由Java实现的,刚刚诞生开放源代码(open-source)的web搜索引擎。 尽管Web搜索是漫游Internet的基本要求, 但是现有web搜索引擎的数目却在下降。 并且这很有可能进一步演变成为一个公司垄断了几乎...

    nutch-1.2.part05

    nutch Nutch是一个由Java实现的,刚刚诞生开放源代码(open-source)的web搜索引擎。 尽管Web搜索是漫游Internet的基本要求, 但是现有web搜索引擎的数目却在下降。 并且这很有可能进一步演变成为一个公司垄断了几乎...

    nutch-1.2.part03

    nutch Nutch是一个由Java实现的,刚刚诞生开放源代码(open-source)的web搜索引擎。 尽管Web搜索是漫游Internet的基本要求, 但是现有web搜索引擎的数目却在下降。 并且这很有可能进一步演变成为一个公司垄断了几乎...

Global site tag (gtag.js) - Google Analytics