今天给大家分享一下如何使用Nutch搭建自己的搜索引擎,Nutch 是一个开源Java 实现的搜索引擎。它提供了我们运行自己的搜索引擎所需的全部工具。包括全文搜索和Web爬虫。
本指南设置了一个非集群Nutch爬虫程序,该程序通过HBase存储其数据。我们将不学习如何设置Hadoop等,只学习在一台机器上抓取和索引 站的最低要求。
使用软件
依赖项
首先需要安装上面列出的这几项 OpenJDK, ant 和 ElasticSearch (也可以通过下面的 址下载)
https://github.com/apache/nutch/archive/release-2.3.tar.gzhttp://mirror.cc.columbia.edu/pub/software/apache/hbase/hbase-0.94.26/hbase-0.94.26.tar.gzhttps://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.deb
把 Nutch 和 HBase 解压到自己定义的文件目录内,下面教程中我们使用 $NUTCH_ROOT 代表你 Nutch 软件解压后的存放目录, $HBASE_ROOT 表你 HBase 软件解压后的存放目录!
首先启动 HBase
<configuration> <property> <name>hbase.rootdir</name> <value>file:///full/path/to/where/the/data/should/be/stored</value> </property> <property> <name>hbase.cluster.distributed</name> <value>false</value> </property></configuration>
# export JAVA_HOME=/usr/java/jdk1.6.0/export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
这一步似乎是多余的,但即使在我的shell中设置了JAVA_HOME,HBase也无法识别它。
- 启动 HBase 命令:
$HBASE_ROOT/bin/start-hbase.sh
启动 Nutch
- 启用配置文件 $NUTCH_ROOT/ivy/ivy.xml 中的配置项。把这一行取消注释即可
<dependency org="org.apache.gora" name="gora-hbase" rev="0.5" conf="*->default" />
- 修改另一个配置文件来启用hbase, $NUTCH_ROOT/conf/gora.properties:
# gora.datastore.default=org.apache.gora.mock.store.MockDataStoregora.datastore.default=org.apache.gora.hbase.store.HBaseStore
- build 一下 Nutch
$ cd $NUTCH_ROOT$ ant clean$ ant runtime
第一次运行此命令会有点慢,请耐心等待! 命令完成后会创建出这个文件夹 $NUTCH_ROOT/runtime/local.
- 修改Nutch的配置文件 $NUTCH_ROOT/runtime/local/conf/nutch-site.xml:
<configuration> <property> <name>http.agent.name</name> <value>zhuangpenglong</value> <!-- this can be changed to something more sane if you like --> </property> <property> <name>http.robots.agents</name> <value>zhuangpenglong</value> <!-- this is the robot name we're looking for in robots.txt files --> </property> <property> <name>storage.data.store.class</name> <value>org.apache.gora.hbase.store.HBaseStore</value> </property> <property> <name>plugin.includes</name> <!-- do **NOT** enable the parse-html plugin, if you want proper HTML parsing. Use something like parse-tika! --> <value>protocol-httpclient|urlfilter-regex|parse-(text|tika|js)|index-(basic|anchor)|query-(basic|site|url)|response-(json|xml)|summary-basic|scoring-opic|urlnormalizer-(pass|regex|basic)|indexer-elastic</value> </property> <property> <name>db.ignore.external.links</name> <value>true</value> <!-- do not leave the seeded domains (optional) --> </property> <property> <name>elastic.host</name> <value>localhost</value> <!-- where is ElasticSearch listening --> </property></configuration>
- 配置HBase 的数据存储位置 $NUTCH_ROOT/runtime/local/conf/hbase-site.xml:
<configuration> <property> <name>hbase.rootdir</name> <value>file:///full/path/to/where/the/data/should/be/stored</value> <!-- same path as you've given for HBase above --> </property> <property> <name>hbase.cluster.distributed</name> <value>false</value> </property></configuration>
以上这些设置都是为了抓取 站
设置Nutch要抓取的 种子 站
$ mkdir seed$ echo "https://www.zhuangpenglong.com" >> seed/urls.txt
- 通过提供一个文件URL将它们注入Nutch
$ $NUTCH_ROOT/runtime/local/bin/nutch inject seed/urls.txt
开始执行爬虫程序
$ $NUTCH_ROOT/runtime/local/bin/nutch generate -topN 10
上述命令将为 10 个 URL 创建作业批次。
$ $NUTCH_ROOT/runtime/local/bin/nutch fetch -all
$ $NUTCH_ROOT/runtime/local/bin/nutch parse -all
$ $NUTCH_ROOT/runtime/local/bin/nutch updatedb -all
在第一次运行时,这只会抓取注入的 URL。 上面的过程应该定期重复以保持索引最新。
将Nutch抓取到的内容放入 ElasticSearch
很简单每一条命令搞定:
$ $NUTCH_ROOT/runtime/local/bin/nutch index -all
如何使用ElasticSearch 进行搜索
可以通过 ElasticSearch 官 指定的通用方式:
$ curl -X GET "http://localhost:9200/_search?query=庄朋龙"
返回结果时json格式, 可以对接任意前端页面!!!
更全面的视频教程正在录制中,大家关注我,后面更新后就能看到推荐!
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!