孤独海岸线 发表于 2015-11-12 09:10:38

Nutch + Solr + Hadoop 搭建分布式搜索引擎详细教程

Nutch + Solr + Hadoop 搭建分布式搜索引擎详细文档

经过几天的摸索,终于完全成功地把环境搭建好了,这个过程遇到各种问题,并且目前网上没有一个完全可行的详细环境搭建教程,所以这里专门写一个详细的文档作为记录,这里主要记录nutch 的配置。





环境:Linux 3.14.2 x86_64, JDK 1.7.0.51 x86_64
  准备:hadoop 2.4.1 ,
  apache-nutch-2.2.1-src.tar.gz (http://www.apache.org/dyn/closer.cgi/nutch/2.2.1/apache-nutch-2.2.1-src.tar.gz),
  solr-4.10.2.tgz(http://apache.petsads.us/lucene/solr/4.10.2),
  apache-tomcat-6.0.43.tar.gz(http://tomcat.apache.org/download-60.cgi#6.0.43),
  apache-ant-1.9.4-bin.zip(http://www.webhostingjams.com/mirror/apache//ant/binaries/apache-ant-1.9.4-bin.zip)

          IK Analyzer 2012FF_hf1(IK Analyzer 2012FF_hf1http://ik-analyzer.googlecode.com/files/IK%20Analyzer%202012FF_hf1.zip



先安装好JDK, Hadoop,配置好环境变量,可以参考网上教程,这里不写了。保证java 和 Hadoop 环境没有问题, 接下来就先安装Solr:

首先要安装tomcat,解压apache-tomcat-6.0.43.tar.gz到一个目录下,这样就可以了,如果需要可以修改相应的配置,默认配置可以的。
  解压solr-4.10.2.tgz 到一个目录下,将dist目录下的solr-4.10.2.war拷贝到TOMCAT_HOME/webapps 下,更名为solr.war。接着配置solr.home (基于JNDI的方式),将example 目录下的solr文件夹拷贝到自己想放置solr.home的目录下(我的是solrhome目录)。在apache-tomcat-6.0.43/conf/Catalina/localhost 目录(如果这个目录不存在,启动一些tomcat就自动生成)下,建立solr.xml:
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>                                                                                                                                          
<Context docBase=&quot;/home/hduser/chenkang/software/apache-tomcat-6.0.43/webapps/solr.war&quot; debug=&quot;0&quot; crossContext=&quot;true&quot; >
<Environment name=&quot;solr/home&quot; type=&quot;java.lang.String&quot; value=&quot;<strong>path to solrhome/solr</strong>&quot; override=&quot;true&quot; />
</Context>
<span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 26px;&quot;><span style=&quot;font-size:12px;&quot;>将solr目录下example/lib/ext中的jar包copy到tomcat的solr/WEB-INF/lib/下。这时候虽然添加jar包,但是没有对应的日志配置,需要将example/resources/下的log4j.properties也放到solr/WEB-INF/lib/下。(这步非常关键,solr4.3的war包中不包含任何日志的jar包,需要自己手动添加)</span></span>
  这样,solr就配好了,重启tomcat,访问http://localhost:8080/solr/ 就可以看到主界面。
  最好配置中文分词器IK Analyzer 2012FF_hf1:
  将IK_Analyzer_2012FF_hf1目录下IKAnalyzer2012FF_u1.jar,IKAnalyzer.cfg.xml,stopword.dic拷贝到apache-tomcat-6.0.43/webapps/solr/WEB-INF/lib/目录下,然后需要配置solr.home目录下的schema.xml文件,位于solrhome/solr/collection1/conf目录下,在其中合适的位置增加:
  

<fieldType name=&quot;text_ik&quot; class=&quot;solr.TextField&quot;>
<analyzer type=&quot;index&quot; isMaxWordLength=&quot;false&quot; class=&quot;org.wltea.analyzer.lucene.IKAnalyzer&quot;/>
<analyzer type=&quot;query&quot; isMaxWordLength=&quot;true&quot; class=&quot;org.wltea.analyzer.lucene.IKAnalyzer&quot;/>
</fieldType>
  
  这里要注意,IK的fieldType 属性中不像其他的filedType有positionIncrementGap=&quot;100&quot;, 如果加上了这个就会出错(这个问题纠结了好久!)

可以将 text field 的type改成 text_ik,这样由于下面有copyfield的原因,可以对任何text做中文分词了。

<field name=&quot;text&quot; type=&quot;text_ik&quot; stored=&quot;false&quot; indexed=&quot;true&quot; multiValued=&quot;true&quot; />  solr就配置好了。
  接下来配置nutch1.9。我们先确保单机上可以用,然后在试下分布式。
  我们解压apache-nutch-2.2.1-src.tar.gz到一个目录,因为要修改配置文件,需要编译源码,因此需要用ant, 将ant安装配置好(http://blog.iyunv.com/uid-20694808-id-57725.html)。
  1)进入到nutch目录下,修改conf目录下nutch-site.xml ,加入:
  

<property>
<name>http.agent.name</name>
<value>My Nutch Spider</value>
</property>
<property>
<name>http.robots.agents</name>
<value>My Nutch Spider,*</value>
</property>这个非常重要,没有这个跑不了。  
  如果需要限制抓取的域名,可以在regex-urlfilter.txt中将最后一行 &#43;. 改成 &#43;^http://(*\.)*xxx.xxx.com(例如:&#43;^http://(*\.)*software.xmu.edu.cn)
  2)修改nutch-default.xml, 配置正确的plugin.folders,如果是local模式,最好将plugin.folders设为绝对路径,如: /home/spark/software/apache-nutch-1.9-new/runtime/local/plugins, 如果是分布式, plugin.folders 设为默认的plugins即可。
  3)将apache-nutch-2.2.1-src/conf/下schema-solr4.xml拷贝到solr服务器下的solr.home 目录中solrhome/solr/collection1/conf 目录下,更名为schema.xml,并在其中加入IK的配置,还必须加入以下配置,否则会出错:
  

   <field name=&quot;_version_&quot; type=&quot;long&quot; indexed=&quot;true&quot; stored=&quot;true&quot;/>
<field name=&quot;_root_&quot; type=&quot;string&quot; indexed=&quot;true&quot; stored=&quot;false&quot;/>附件中有我的完整schema.xml配置。  
  配置好后,进入apache-nutch-2.2.1-src目录下,用ant编译,编译后生成runtime目录,有两个子目录,local 对应单机模式,deploy对应分布式模式,编译过程将对应的配置放到两个目录的相应位置中,在local目录下是放在conf等目录下,而在deploy目录下,会生成一个job的文件,这个文件包含了抓取过程中用到的各种配置文件和jar包等,跑hadoop任务的时候将这些一同放到集群中。
  这样nutch就配置好了,可以用nutch抓取网页为solr建立索引。
  单台上跑到runtime/local目录下,到bin目录下(要到bin目录下,否则可能会出现路径的问题),运行
  ./crawl urls crawld http://localhost:8080/solr 2
  crawl <seedDir> <crawlDir> <solrURL> <numberOfRounds>


  urls 为包含种子链接文件的文件夹,crawld为抓取结果存放目录, solrURL为solr服务器地址, numberOfRounds为迭代次数,这里设小点是为了测试,具体含义可以看crawl脚本。
  结束之后solr中就有文档了,可以搜索。
  分布式抓取要先将urls 传到 hdfs上,并且在hdfs上新建crawld目录
  hadoop fs -put urls /
  hadoop fs -mkdir /crawld
  然后到deploy/bin目录下,调用
  crawl /urls //crawld http://172.XX.XX.XX:8080/solr 2


  注意这个时候localhost要改成相应的ip地址了,但是可能会出现java.net.noroutetohostexception no route to host, 这是由于solr服务器防火墙没有关闭的原因,关闭方法:
  http://www.iyunv.com/Linux/2013-08/89215.htm


  如果是使用solrcloud的方式建索引,上面的方式会报Not Found错误,要用
  ./crawl /wjj/urls /wjj/crawld http://172.16.13.190:8080/solr/primary_shard2_replica1 5


  这样就可以分布式的方式进行网络爬虫并交给solr建立索引了。
  


  环境终于搭建好了,剩下的工作就是好好研究以下nutch 和solr了,为开发和优化做准备:)
  



版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Nutch + Solr + Hadoop 搭建分布式搜索引擎详细教程