Nutch1.3集成Solr网页快照功能实现(一)

2019-03-27 00:22|来源: 网路

Nutch1.3版本以后使用了Solr作为索引功能的提供者,在索引效率、集群功能方面做了很大改进,但与Nutch1.2版本相比,Solr缺失了网页快照的功能,按官方手册中集成配置后,每次查询返回的结果中仅包含解析处理过的HTML正文部分,如下图所示:

对于需要原网页快照功能的使用者来说,带来了巨大的麻烦。因此,需要对Nutch1.3做一些改动,使其支持集成后的网页快照功能。

参考Nutch1.2原来的实现方式,其自带的索引功能其实是将整个网页进行了索引,而1.3版本在调用Solr服务之前,Nutch主动将无用的Html标签信息去掉了(其内部机制在此不做探讨),结果Solr中仅获取了网页之中的“正文”部分,也就是上面图片中看到的Content标签中的内容。我们所要做的工作,其核心就是将整个网页的缓存信息也交给Solr,并在查询Solr时作为结果内容返回。

首先,需要下载Nutch1.3的开发环境,下载链接:http://www.apache.org/dist//nutch/。构建工程很麻烦,也可以直接下载我构建好的工程:http://download.csdn.net/detail/Nightbreeze/3667744JDK需要使用1.6版本。

在工程中找到“SolrIndexer”类,中的“indexSolr”方法,如下:

  public void indexSolr(String solrUrl, Path crawlDb, Path linkDb,

      List<Path> segments) throws IOException {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    long start = System.currentTimeMillis();

    LOG.info("SolrIndexer: starting at " + sdf.format(start));

 

    final JobConf job = new NutchJob(getConf());

    job.setJobName("index-solr " + solrUrl);

 

    IndexerMapReduce.initMRJob(crawlDb, linkDb, segments, job);

 

    job.set(SolrConstants.SERVER_URL, solrUrl);

 

    NutchIndexWriterFactory.addClassToConf(job, SolrWriter.class);

 

    job.setReduceSpeculativeExecution(false);

 

    final Path tmp = new Path("tmp_" + System.currentTimeMillis() + "-" +

                         new Random().nextInt());

 

    FileOutputFormat.setOutputPath(job, tmp);

    try {

      JobClient.runJob(job);

      // do the commits once and for all the reducers in one go

      SolrServer solr =  new CommonsHttpSolrServer(solrUrl);

      solr.commit();

      long end = System.currentTimeMillis();

      LOG.info("SolrIndexer: finished at " + sdf.format(end) + ", elapsed: " + TimingUtil.elapsedTime(start, end));

    }

    catch (Exception e){

      LOG.error(e);

    } finally {

      FileSystem.get(job).delete(tmp, true);

    }

  }

Nutch在这里使用了Hadoop的分布式计算机制,我们跳转到:“IndexerMapReduce.initMRJob(crawlDb, linkDb, segments, job)方法中看一下,如下:

  public static void initMRJob(Path crawlDb, Path linkDb,

                           Collection<Path> segments,

                           JobConf job) {

 

    LOG.info("IndexerMapReduce: crawldb: " + crawlDb);

    LOG.info("IndexerMapReduce: linkdb: " + linkDb);

 

    for (final Path segment : segments) {

      LOG.info("IndexerMapReduces: adding segment: " + segment);

      FileInputFormat.addInputPath(job, new Path(segment, CrawlDatum.FETCH_DIR_NAME));

      FileInputFormat.addInputPath(job, new Path(segment, CrawlDatum.PARSE_DIR_NAME));

      FileInputFormat.addInputPath(job, new Path(segment, ParseData.DIR_NAME));

      FileInputFormat.addInputPath(job, new Path(segment, ParseText.DIR_NAME));

    }

 

    FileInputFormat.addInputPath(job, new Path(crawlDb, CrawlDb.CURRENT_NAME));

    FileInputFormat.addInputPath(job, new Path(linkDb, LinkDb.CURRENT_NAME));

    job.setInputFormat(SequenceFileInputFormat.class);

 

    job.setMapperClass(IndexerMapReduce.class);

    job.setReducerClass(IndexerMapReduce.class);

 

    job.setOutputFormat(IndexerOutputFormat.class);

    job.setOutputKeyClass(Text.class);

    job.setMapOutputValueClass(NutchWritable.class);

    job.setOutputValueClass(NutchWritable.class);

  }

可以看到,FileInputFormat.addInputPath(job, new Path(segment, ParseText.DIR_NAME));中仅处理了Segment文件夹下“parse_data”与“parse_text”中的内容。

本文出自 “果壳中的宇宙” 博客,转载请与作者联系!


转自:http://williamx.blog.51cto.com/3629295/722707

相关问答

更多
  • 3.1.Nutch安装 l 解压 tar -zxvf apache-nutch-1.4-bin.tar.gz l 终端下cd到目录 apache-nutch-1.4-bin/runtime/local,下面会有 bin conf lib ...
  • 3.1.Nutch安装 l 解压 tar -zxvf apache-nutch-1.4-bin.tar.gz l 终端下cd到目录 apache-nutch-1.4-bin/runtime/local,下面会有 bin conf lib ...
  • Nutch是构建网络爬虫和搜索引擎的框架。 Nutch可以完成从收集网页到建立倒排索引的整个过程。 它也可以将这些索引推送到Solr。 Solr主要是一个搜索引擎,支持分面搜索和许多其他简洁的功能。 但Solr不提取数据,你必须提供它。 因此,也许你必须要问的第一件事是在你是否有可用的索引数据(在XML中,在CMS或数据库中)。 在这种情况下,您应该只使用Solr并为其提供数据。 另一方面,如果你不得不从网络上获取数据,你可能更愿意使用Nutch。 Nutch is a framework to build ...
  • 这主要是Nutch使用的Solrj版本罐和您尝试集成的Solr 3.6之间的javabin不兼容性。 您需要更新Solrj罐并重新生成作业。 按照论坛中提到的步骤操作。 This is mainly the javabin incompatiblity between the Solrj version jars used by Nutch and the Solr 3.6 which you are trying to integrate. You would need to update the Sol ...
  • 问题是solr,nutch和hbase之间的版本不兼容。 这篇文章对我来说非常合适。 The problem was version incompatibility between solr, nutch and hbase. This article worked perfectly for me.
  • 您需要将以下Apache Commons库添加到类路径中: commons-httpclient.jar (您可以将它放在nutch安装所使用的其他JAR所在的文件夹中)。 你可以在这里找到当前版本的HttpClient http://hc.apache.org/httpcomponents-client-ga/ 请注意,您的Nutch版本可能使用较旧版本的HttpClient,而当前版本的HttpClient与旧版本不兼容。 在这种情况下,您需要下载旧版本的HttpClient,并在您的库中包含旧版本。 ...
  • nutch模式将id(= url)定义为teh唯一键。 如果你重新抓取url,当nutch将数据发布到solr时,文档将被替换为solr索引。 The nutch schema defines id (= url) as teh unique key. If you re-crawl the url teh document will be replaced in solr index when nutch posts the data to solr.
  • 使用cygwin,这是一个很好的指南,可以将它们组合在一起: http://amac4.blogspot.com/2013/07/setting-up-solr-with-apache-tomcat-be.html Use cygwin, heres an excellent guide to set them up together: http://amac4.blogspot.com/2013/07/setting-up-solr-with-apache-tomcat-be.html
  • 您可以尝试裁剪图像(我正在使用Python 3.5,因此如果您使用的是Python 2.X,则可能需要调整以使用StringIO): from io import BytesIO from selenium import webdriver from PIL import Image if __name__ == '__main__': driver = webdriver.PhantomJS('C:') driver.set_window_size( ...
  • 在目前阶段,Nutch只负责抓取网页,这意味着访问网页,提取内容,找到更多链接并重复这个过程(我正在跳过很多复杂的东西,但希望你能得到这个想法) 。 爬网过程的最后一步是将数据存储在后端(ES / Solr是1.x分支上支持的数据存储)。 因此,在这个步骤中,Solr开始发挥作用,在Nutch完成其工作之后,您需要将数据存储在某处以便能够在其上执行查询:这是Solr作业。 前段时间Nutch包含了编写倒排索引的能力(正如问题中所解释的那样),但是决定(也是前一段时间)是弃用这个以支持使用Solr / ES( ...