Nutch[1] 是一个开源Java实现的搜索引擎,它提供了咱们运行本身的搜索引擎所需的所有工具,包括全文搜索和Web爬虫。
Solr[2]是一个基于Lucene的全文搜索服务器,它对外提供相似于Web-service的API查询接口,是一款很是优秀的全文搜索引擎。html
简单地讲,nutch重在提供数据源采集(Web爬虫)能力,轻全文搜索(lucene)能力;solr是lucene的扩展,亦是nutch的全文搜索的扩展。重在将nutch的爬取结果,经过其对外提供检索服务。java
支持hadoop,能够经过hadoop,得到分布式爬虫的能力。本文重点介绍nutch的原力,关于分布式爬虫,将在后续章节中介绍。另外,nutch-2.x系列支持hbase,能够根据自身的须要灵活选择。须要说明的是两版的用法是不一样的,nutch-2.x要更为复杂。在使用nutch-2.x以前,最好具有nutch-1.x的基础。node
截止发稿时是最新版本,可参考官网的解释,这里没什么要说的。web
Ubuntu14.04x64 或 Centos6.5x64, 应用程序采用二进制安装,不要求编译环境数据库
vim /etc/profile # set for java export JAVA_HOME=/opt/jdk1.8.0_111 #二进制包已经解压安装到该路径 export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib export _JAVA_OPTIONS="Xmx2048m XX:MaxPermSize=512m Djava.awt.headless=true"
注:java的安装方法选择二进制包便可,本文再也不赘述apache
vim /etc/profile export NUTCH_RUNTIME_HOME='/opt/apache-nutch-1.13' export PATH=$NUTCH_RUNTIME_HOME/bin:$PATH export APACHE_SOLR_HOME='/opt/solr-6.6.0' #单引号不能少 export PATH=$APACHE_SOLR_HOME/bin:$PATH export CLASSPATH=.:$CLASSPATH:$APACHE_SOLR_HOME/server/lib
source /etc/profile #加载到环境
vim
wget http://mirror.bit.edu.cn/apache/lucene/solr/6.6.0/solr-6.6.0.tgz cat solr-6.6.0.tgz |(cd /opt; tar xzfp -) solr status #注:若是执行结果不正常,执行`source /etc/profile`和检查该文件的内容 No Solr nodes are running. #启动solr服务 solr start -force #-force:强制以root身份执行,生产环境请勿使用该参数 #中止solr服务 solr stop
安装完毕。浏览器
cd ${APACHE_SOLR_HOME} cp -r server/solr/configsets/basic_configs server/solr/configsets/nutch cp ${NUTCH_RUNTIME_HOME}/conf/schema.xml server/solr/configsets/nutch/conf mv server/solr/configsets/nutch/conf/managed-schema server/solr/configsets/nutch/conf/managed-schema.backup #启动solr服务 solr start #建立nutch core solr create -c nutch -d server/solr/configsets/nutch/conf/ -force #-force:强制以root身份执行,生产环境请勿使用该参数 建立过程并不是一路顺风,整个过程充满了各类bug,从这个角度考虑,生产环境中有必要更换到solr的稳定版,好在这些坑已经趟过: 问题1:Caused by: Unknown parameters: {enablePositionInc rements=true} 具体信息: Copying configuration to new core instance directory: /opt/solr-6.6.0/server/solr/nutch Creating new core 'nutch' using command: http://localhost:8983/solr/admin/cores?action=CREATE&name=nutch&instanceDir=nutch ERROR: Error CREATEing SolrCore 'nutch': Unable to create core [nutch] Caused by: Unknown parameters: {enablePositionIncrements=true} 解决办法: vim server/solr/configsets/nutch/conf/schema.xml 找到并去掉enablePositionIncrements=true 问题2:ERROR: Error CREATEing SolrCore 'nutch': Unable to create core [nutch] Caused by: defaultSearchField has been deprecated and is incompatible with configs with luceneMatchVersion >= 6.6.0. Use 'df' on requests instead. 解决办法: vim server/solr/configsets/nutch/conf/solrconfig.xml 将luceneMatchVersion版本修改成6.2.0 问题3:org.apache.solr.common.SolrException: fieldType 'booleans' not found in the schema 解决办法: vim /opt/solr-6.6.0/server/solr/configsets/nutch/conf/solrconfig.xml 找到booleans,替换成boolean,以下: <lst name="typeMapping"> <str name="valueClass">java.lang.Boolean</str> <str name="fieldType">boolean</str> </lst> Then it will work.. 问题3之后,会发生多起相似事件,以下: ERROR: Error CREATEing SolrCore 'nutch': Unable to create core [nutch] Caused by: fieldType 'tdates' not found in the schema ERROR: Error CREATEing SolrCore 'nutch': Unable to create core [nutch] Caused by: fieldType 'tlongs' not found in the schema ERROR: Error CREATEing SolrCore 'nutch': Unable to create core [nutch] Caused by: fieldType 'tdoubles' not f ound in the schema 参照问题3的方法,一次性去掉''中关键字的复数形式便可。 问题4:ERROR: Core 'nutch' already exists! Checked core existence using Core API command: http://localhost:8983/solr/admin/cores?action=STATUS&core=nutch 解决办法: solr delete -c nutch #删除core 'nutch' 若是删除完,还提示这个错误,这是因为每次修改完配置文件,须要重启下solr服务,更新下状态。 最终的执行结果: solr create -c nutch -d server/solr/configsets/nutch/conf/ -force Copying configuration to new core instance directory: /opt/solr-6.6.0/server/solr/nutch Creating new core 'nutch' using command: http://localhost:8983/solr/admin/cores?action=CREATE&name=nutch&instanceDir=nutch { "responseHeader":{ "status":0, "QTime":3408}, "core":"nutch"} 执行成功! 在浏览器中访问 http://localhost:8983/solr/#/ 能够看到名称为nutch的core
cd /opt/solr-6.6.0/server cat etc/realm.properties # # 这个文件定义用户名,密码和角色 # # 格式以下 # <username>: <password>[,<rolename> ...] # #userName: password,role yourname: yourpass,admin
cat contexts/solr-jetty-context.xml <?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath"><Property name="hostContext" default="/solr"/></Set> <Set name="war"><Property name="jetty.base"/>/solr-webapp/webapp</Set> <Set name="defaultsDescriptor"><Property name="jetty.base"/>/etc/webdefault.xml</Set> <Set name="extractWAR">false</Set> <Get name="securityHandler"> <Set name="loginService"> <New class="org.eclipse.jetty.security.HashLoginService"> <Set name="name">Solr Admin Access</Set> <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set> </New> </Set> </Get> </Configure>
vim solr-webapp/webapp/WEB-INF/web.xml <!-- Get rid of error message --> <security-constraint> ... </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>Solr auth enticated application</web-resource-name> <!--描述--> <url-pattern>/</url-pattern> <!-- 验证的网页的位置--> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> <!-- 验证的角色,别写成用户名,若有多个角色能够写多个role-name 标签--> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <!-- 关键--> <realm-name>Solr Admin Access</realm-name> </login-config> </web-app> 重启solr服务 solr stop && solr start -force 在浏览器中访问solr http://localhost:8983/solr/nutch/ 能够看到要求登陆的界面
wget http://mirrors.hust.edu.cn/apache/nutch/1.13/apache-nutch-1.13-bin.tar.gz cat apache-nutch-1.13-bin.tar.gz |(cd /opt; tar xzfp -) nutch -help #注:若是执行结果不正常,执行`source /etc/profile`和检查该文件的内容
安装完毕。安全
以爬取http://nutch.apache.org站点为例,配置以下:服务器
vim $NUTCH_RUNTIME_HOME/conf/nutch-site.xml <property> <name>http.agent.name</name> <value>My Nutch Spider</value> </property>
#配置indexer-solr插件 #个人方法是替换indexer-elastic为indexer-solr插件 sed -i 's/indexer-elastic/indexer-solr/g' $NUTCH_RUNTIME_HOME/conf/nutch-site.xml #注意:官方文档不是像我这样作的,请按照个人方法配置,或者注释掉indexer-elastic,不然会深受其害,踩坑过程后面会说
#为方便修改配置文件,选择conf文件夹做为数据的存储路径 cd $NUTCH_RUNTIME_HOME/conf mkdir -p urls #存储要爬取的URLS列表,每行只写一个url,能够多行 cd urls echo 'http://nutch.apache.org/' > seed.txt #地址能够是静态的连接,也能够是动态的连接
vim regex-urlfilter.txt 将光标移到文件末尾,将下列内容: # accept anything else +. 替换为: # accept anything else +^http://([a-z0-9]*\.)*nutch.apache.org/ #这将包含带有域名前缀的url,好比,http://3w.nutch.apache.org
vim crawl-urlfilter.txt regex-urlfilter.txt
替换: # skip URLs containing certain characters as probable queries, etc. -[?*!@=] 为: # accept URLs containing certain characters as probable queries, etc. +[?=&]
注: conf下有各类配置文件,涉及各类爬取规则和正则过滤器。将在后续的文章中详细说明
抓取程序自动在用户指定目录下面创建爬取目录,其目录下能够看到crawldb,segments,linkdb子目录
crawldb目录下面存放下载的URL,以及下载的日期、过时时间
linkdb目录存放URL的关联关系,是下载完成后分析时建立的,经过这个关联关系能够实现相似google的pagerank功能
segments目录存储抓取的页面,这些页面是根据层级关系分片的。既segments下面子目录的个数与获取页面的层数有关系,若是指定“-depth”参数是10层,这个目录下就有10层,结构清晰并防止文件过大。
segments目录里面有6个子目录,分别是:
“crawl_generate” 生成要获取的一组URL的名字,既生成待下载的URL的集合 “crawl_fetch” 包含获取每一个UR L的状态 ”content“ 包含从每一个URL检索的原始内容 “parse_text” 包含每一个URL的解析文本(存放每一个解析过的URL的文本内容) “parse_data” 包含从每一个URL分析的外部连接和元数据 “crawl_parse” 包含用于更新crawldb的outlink URL(外部连接库)
爬取过程包括
injector -> generator -> fetcher -> parseSegment -> updateCrawleDB -> Invert links -> Index -> DeleteDuplicates -> IndexMerger 1. 根据以前建好的URL列表文件,将URL集注入crawldb数据库---inject 2. 根据crawldb数据库建立抓取列表---generate 3. 执行抓取,获取网页信息---fetch 4. 执行解析,解析网页信息---parse 5. 更新数据库,把获取到的页面信息存入数据库中---updatedb 6. 重复进行2~4的步骤,直到预先设定的抓取深度。---这个循环过程被称为“产生/抓取/更新”循环 7. 根据sengments的内容更新linkdb数据库---invertlinks 8. 创建索引---index
nutch inject crawl/crawldb urls
nutch generate crawl/crawldb crawl/segments
s1=`ls -d crawl/segments/2* | tail -1` echo $s1 nutch fetch $s1
nutch parse $s1
nutch updatedb crawl/crawldb $s1
重复2-4的过程,抓取下一层页面
演示过程当中,为了节约时间,咱们约定一个参数,只抓取前 top 1000 的页面
nutch generate crawl/crawldb crawl/segments -topN 1000 s2=`ls -d crawl/segments/2* | tail -1` echo $s2 nutch fetch $s2 nutch parse $s2
updatedb:
nutch updatedb crawl/crawldb $s2
重复2-4的过程,抓取下下层的页面
一样只取前1000个页面进行抓取
nutch generate crawl/crawldb crawl/segments -topN 1000 s3=`ls -d crawl/segments/2* | tail -1` echo $s3 nutch fetch $s3 nutch parse $s3
updatedb:
nutch updatedb crawl/crawldb $s3
这样咱们总共抓取了三个层级深度的页面
ls crawl/segments/ 20170816191100 20170816191415 20170816192100
nutch invertlinks crawl/linkdb -dir crawl/segments
nutch index -Dsolr.server.url=http://用户名:密码@localhost:8983/solr/nutch crawl/crawldb/ -linkdb crawl/linkdb/ crawl/segments/20170816191100/ -filter -normalize -deleteGone #这里的“用户名:密码”是solr的jetty下的 值得一提的是,若是按照官方标准语法,上面命令会变为: nutch index -Dsolr.auth.username="yourname" -Dsolr.auth.password="yourpassword" -Dsolr.server.url=http://localhost:8983/solr/nutch crawl/crawldb/ -linkdb crawl/linkdb/ crawl/segments/20170816191100/ -filter -normalize -deleteGone 这里会提示语法错误,在官网和google上尚未更好的解决办法。我已经把上面的方法更新到 http://lucene.472066.n3.nabble.com/Nutch-authentication-problem-to-solr-td4251336.html#a4351038 可能其它版本没有这个问题。 最终的推送结果以下: nutch index -Dsolr.server.url=http://xxx:xxx@localhost:8983/solr/nutch crawl/crawldb/ -linkdb crawl/linkdb/ crawl/segments/20170816191100/ -filter -normalize -deleteGone Segment dir is co mplete: crawl/segments/20170816191100. Indexer: starting at 2017-08-17 18:22:26 Indexer: deleting gone documents: true Indexer: URL filtering: true Indexer: URL normalizing: true Active IndexWriters : SOLRIndexWriter solr.server.url : URL of the SOLR instance solr.zookeeper.hosts : URL of the Zookeeper quorum 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 : username for authentication solr.auth.password : password for authentication Indexing 1/1 documents Deleting 0 documents Indexer: number of documents indexed, deleted, or skipped: Indexer: 1 indexed (add/update) Indexer: finished at 2017-08-17 18:22:32, elapsed: 00:00:05
在浏览器中访问solr
http://localhost:8983/solr/
crawl -i -D solr.server.url=http://用户名:密码@localhost:8983/solr/ urls/ crawl/ 2
https://wiki.apache.org/nutch/NutchTutorial#Install_Nutch [1]https://wiki.apache.org/nutch/ [2]https://wiki.apache.org/solr/