荐 Nutch学习笔记4-Nutch 1.7 的 索引篇 ElasticSearch

2019-03-27 01:21|来源: 网路

上一篇讲解了爬取和分析的流程,很重要的收获就是:

解析过程中,会根据页面的ContentType获得一系列的注册解析器,

依次调用每个解析器,当其中一个解析成功后就返回,否则继续执行下一个解析器。

当然,返回之前还要经过注册过的所有HtmlParseFilter的过滤,至少对HtmlParser是这样的。

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·下面来看看索引过程。

当我们敲入

./bin/nutch index

发生了什么?

查看1.7版本的nutch脚本

elif [ "$COMMAND" = "index" ] ; then
  CLASS=org.apache.nutch.indexer.IndexingJob

那么我们就来看看IndexingJob这个类。

首先看函数

public int run(String[] args) throws Exception {

这个函数先解析参数。

然后调用

try {
            index(crawlDb, linkDb, segments, noCommit, deleteGone, params,
                    filter, normalize);
            return 0;
        } catch (final Exception e) {
            LOG.error("Indexer: " + StringUtils.stringifyException(e));
            return -1;
        }

那么我们继续看

public void index(Path crawlDb, Path linkDb, List<Path> segments,
            boolean noCommit, boolean deleteGone, String params,
            boolean filter, boolean normalize) throws IOException {

看代码的第100行

IndexWriters writers = new IndexWriters(getConf());
        LOG.info(writers.describe());

如果你查看IndexWriters的构造函数,会发现这仍然是通过插件机制获得所有可用的IndexWriter的。

PS:Nutch只是规定了一系列流程,至于每个流程,可以通过Plugin来介入。

为我们后续注入自己的东西比如定制化需求提供了很大的方便。

~~~~~~~~~~~~~

 如果你此时运行

./bin/nutch index ./data/crawldb/ -linkdb ./data/linkdb/  -dir ./data/segments/ -deleteGone -filter -normalize

系统会报错:

Indexer: starting at 2014-06-26 14:49:35
Indexer: deleting gone documents: true
Indexer: URL filtering: true
Indexer: URL normalizing: true
Indexer: java.lang.RuntimeException: Missing SOLR URL. Should be set via -D solr.server.url
SOLRIndexWriter
	solr.server.url : URL of the SOLR instance (mandatory)
	solr.commit.size : buffer size when sending to SOLR (default 1000)
	solr.mapping.file : name of the mapping file for fields (default solrindex-mapping.xml)
	solr.auth : use authentication (default false)
	solr.auth.username : use authentication (default false)
	solr.auth : username for authentication
	solr.auth.password : password for authentication

	at org.apache.nutch.indexwriter.solr.SolrIndexWriter.setConf(SolrIndexWriter.java:192)
	at org.apache.nutch.plugin.Extension.getExtensionInstance(Extension.java:160)
	at org.apache.nutch.indexer.IndexWriters.<init>(IndexWriters.java:57)
	at org.apache.nutch.indexer.IndexingJob.index(IndexingJob.java:100)
	at org.apache.nutch.indexer.IndexingJob.run(IndexingJob.java:185)
	at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:65)
	at org.apache.nutch.indexer.IndexingJob.main(IndexingJob.java:195)

这是为啥,如果你了解了Nutch, 其实自己都能找到原因。

学开源就是这点好处,你看了代码你就知道问题所在。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

之所以这里定位到了Solr,是因为我们在配置文件里启用了index-solr插件。

 修改前的nutch-default.xml关于插件是

<property>
  <name>plugin.includes</name>
  <value>protocol-http|urlfilter-regex|parse-(html|tika)|index-(basic|anchor)|indexer-solr|scoring-opic|urlnormalizer-(pass|regex|basic)</value>
  <description>Regular expression naming plugin directory names to
  include.  Any plugin not matching this expression is excluded.
  In any case you need at least include the nutch-extensionpoints plugin. By
  default Nutch includes crawling just HTML and plain text via HTTP,
  and basic indexing and search plugins. In order to use HTTPS please enable
  protocol-httpclient, but be aware of possible intermittent problems with the
  underlying commons-httpclient library.
  </description>
</property>

看到了吧,这里就有indexer-solr的配置。

复制到nutch-site.xml,然后去掉indexer-solr,替换成indexer-elastic.

重新执行下面的命令

./bin/nutch index ./data/crawldb/ -linkdb ./data/linkdb/  -dir ./data/segments/ -deleteGone -filter -normalize

系统会报错,表示缺少配置项,自己解决吧。

~~~~~~~~~~~~~~~~~~~~~~~~~~

关于IndexWriter和IndexingFilter的官方解释:

IndexWriter -- Writes crawled data to a specific indexing backends (Solr, ElasticSearch, a CVS file, etc.).
IndexingFilter -- Permits one to add metadata to the indexed fields. All plugins found which implement this extension point are run sequentially on the parse (from javadoc).

 ~~~~~~~~~~~~~~~~~~~~~~~~~~

关于INdexWriter和IndexingFilter的连接关系

IndexerMapReduce.java的272行

// run indexing filters
      doc = this.filters.filter(doc, parse, key, fetchDatum, inlinks);

其实也就是依次调用每个IndexingFilter,如果结果不是null,则继续调用下一个。

然后过滤后的最终结果通过

public void open(JobConf job, String name) throws IOException {
		for (int i = 0; i < this.indexWriters.length; i++) {
			try {
				this.indexWriters[i].open(job, name);
			} catch (IOException ioe) {
				throw ioe;
			}
		}
	}

	public void write(NutchDocument doc) throws IOException {
		for (int i = 0; i < this.indexWriters.length; i++) {
			try {
				this.indexWriters[i].write(doc);
			} catch (IOException ioe) {
				throw ioe;
			}
		}
	}

也是调用每一个IndexWriter写入到对应的比如说Solr,ElasticSearch等索引器中。

 

至此,我们已经把解析和索引的整个过程及原理都分析完毕。

 

 

 

 


转自:http://my.oschina.net/qiangzigege/blog/284443

相关问答

更多
  • 没有做到这一点,但这绝对是可行的,但需要搭载SOLR代码(src / java / org / apache / nutch / indexer / solr)并将其调整到ElasticSearch。 对Nutch BTW来说会是一个不错的贡献 Haven't done it but this is definitely doable but would require to piggyback the SOLR code (src/java/org/apache/nutch/indexer/solr) a ...
  • 在经历了很多麻烦之后,我开始工作了。 我最终使用ES 1.4.4,nutch 2.3.1,mongodb 3.10和JDK 8。 我经历的许多问题在许多其他方面仍未得到解答: (这是一个容易的,但...)确保一切正在运行。 确保elasticsearch在正确的端口上运行在正确的机器上。 确保你可以跟它说话。 确保MongoDB已启动并在正确的端口上运行,请确保可以与之通话。 使用正确的索引命令。 对于Nutch 3.2.1而言,它是: ./bin/nutch index -all (在您获取并解析之后)。 ...
  • 我没有使用Nutch和ES 1.5 / 1.6 / 1.7但是在indexer-elastic插件使用的API之间不应该有重大变化。 我刚刚按照https://github.com/apache/nutch/blob/master/src/plugin/indexer-elastic/howto_upgrade_es.txt中的说明进行操作并构建/测试( ant test )Nutch 1.11和ES 1.7.2没有任何麻烦。 这意味着,代码构建正常,但我还没有测试将实际数据索引到Elasticsearch ...
  • 如果你正在运行Nutch用于生产,那么坚持Nutch 1.x可能会更好,它有更多功能,正如你所说的那样表现优于2.x. 至于ES兼容性,我认为没有太大的区别。 Nutch 1.x实际上与ES 5.3兼容,这意味着如果您下载.zip文件(或直接从源代码构建),那么您将获得v5.3的ES客户端库。 有一些文档解释了如何升级https://github.com/apache/nutch/blob/master/src/plugin/indexer-elastic/howto_upgrade_es.txt 。 当然 ...
  • 你可以看一下StormCrawler基于Apache Storm,它不仅是一个功能齐全的爬虫,而且还专注于近实时爬行。 ES通常是非常新的,在撰写本文时,它支持ES v6.1.1( https://github.com/DigitalPebble/storm-crawler/blob/master/external/elasticsearch/pom.xml#L20 )所以这可能为你工作。 请记住,这是一种与Nutch不同的方法和技术,尽管它使用了Apache Nutch背后的一些想法。 此外,在https ...
  • 根据https://github.com/apache/nutch/blob/branch-1.10/src/plugin/indexer-elastic/ivy.xml#L39 ,正确的版本应该是elasticsearch 1.4.1。 According to https://github.com/apache/nutch/blob/branch-1.10/src/plugin/indexer-elastic/ivy.xml#L39 the correct version should be elast ...
  • 这里有两件事需要考虑: 索引的数据是什么? 如何正确索引到es 关于索引数据,您使用的索引插件会影响这一点。 例如,basic-index将为每个doc添加内容 , 主机 , URL 等 。 你可以检查插件的文档或只是看看输出是什么(就像你做的那样)。 在了解索引数据以及如何在es集群中处理它之后,可以使用正确/优化的映射在es中创建新索引,并确保Nutch将索引到该索引。 当然,您也可以重新索引已经抓取的内容(请参阅此文章 )。 There are two things to consider here: ...
  • 在Nutch 1.14发布之前,您需要应用此补丁https://github.com/apache/nutch/pull/156并重建: cd apache-nutch-1.13 wget https://raw.githubusercontent.com/apache/nutch/e040ace189aa0379b998c8852a09c1a1a2308d82/src/java/org/apache/nutch/indexer/CleaningJob.java mv CleaningJob.java s ...
  • 确保您在nutch弹性依赖项和本地服务器中运行相同的版本。 如果它们不相同,那么不要浪费你的时间,并使用http协议从nutch而不是Java api直接推送到elastic。 Make sure you are running the same versions in nutch elastic dependency and your local server. If they are not the same, then do not waste your time, and use the http ...
  • 是。 NEST 1.x应与任何ES 1.x兼容。 当你转移到ES 2.x时,你也应该升级到NEST 2.x. 当您转到ES 5.x时,您还应该升级到NEST 5.x. Yes. NEST 1.x should be compatible with any ES 1.x. When you move to ES 2.x you should also upgrade to NEST 2.x When you move to ES 5.x you should also upgrade to NEST 5.x