NUTCH 1.4+hadoop2.20.203.0使用指南
Nutch有三种抓取模式,分别是单机,伪分布,分布式。单机所用文件系统为本机文件系统,伪分布用的是hadoop文件系统,分布式是在其它机器上配置相同的环境,用hadoop文件系统存抓取结果。配置的时候最好先用单机,再伪分布,再加入其它机器,进行分布式抓取,因为配置比较多,这样循序渐近可以减少错误。下面分别介绍:
单机抓取:
从官网下载解压后,配置环境变量,在runtime/里有local和deploy两个目录,分别用于本地抓取和分布式抓取,用分布式抓取将利用hadoop作为文件系统,提高存取效率。
先保证装了jdk1.6以上,在etc目录profile文件最后一行加上
JAVA_HOME=/usr/local/(jdk文件夹名称)
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH
在“nutch安装目录”/runtime/local下,
• run"bin/nutch" - You can confirm a correct installation if you seeingthe following:
Usage: nutch[-core] COMMAND
如果显示permission denied,则运行
chmod +x bin/nutch,提高权限。
修改以下几个文件 :
在“nutch安装目录”/runtime/local /conf目录下,将nutch-default.xml的内容copy到nutch-site.xml 。然后,Add your agent name in the value field of the http.agent.name propertyin conf/nutch-site.xml, for example:
<property>
<name>http.agent.name</name>
<value>你取的名称(随便取个)</value>
</property>
在nutch/runtime/local/bin下建立urls目录
在这个目录下建立nutch.txt文件,名称一定是nutch
加入所需的url,一行一个,http://nutch.apache.org/
然后 Edit the file conf/regex-urlfilter.txt andreplace
# accept anythingelse
+.
with a regularexpression matching the domain you wish to crawl. For example, if you wished tolimit the crawl to the nutch.apache.org domain, the line should read:
+^http://([a-z0-9]*\.)*nutch.apache.org/
This will includeany URL in the domain nutch.apache.org.
Nutch安装步骤可参见如下网址:
http://wiki.apache.org/nutch/NutchTutorial
检索部分:
Nutch1.4使用solr作为索引和检索功能。Solr的配置文件主要有两个,在其安装文件目录的conf目录下,schema.xml,solrconfig.xml两个。前者用于配置和结果显示相关的,后者用于配置软件运行。值得注意的是,默认状态下schema.xml中,应将下行
<field name="content" type="text" stored="false" indexed="true"/>设置成<field name="content" type="text" stored="true" indexed="true"/>
才能显示检索结果的网页内容,否则只有网页的标题等元数据信息。
然后,Integrate Solr with Nutch,Wehave both Nutch and Solr installed and setup correctly. And Nutch alreadycreated crawl data from the seed URL(s). Below are the steps to delegatesearching to Solr for links to be searchable:
cp ${NUTCH_RUNTIME_HOME}/conf/schema.xml ${APACHE_SOLR_HOME}/example/solr/conf/
restart Solr with the command “java -jar start.jar” under ${APACHE_SOLR_HOME}/example
run the Solr Index command:
bin/nutch solrindex http://127.0.0.1:8983/solr/
solr配置可参见:
http://wiki.apache.org/nutch/NutchTutorial
配置完成后,
具体抓取步骤如下:
1:在命令行先启动solr
cd ${APACHE_SOLR_HOME}/example
java -jar start.jar 启动,
在启动后,可在下面网址看到搜索框,
http://localhost:8983/solr/admin/
2:cd $HOME/nutch-1.3/runtime/local ;
bin/nutch crawl urls -solr http://localhost:8983/solr/ -depth 3 -topN 5
即可抓取,在http://localhost:8983/solr/admin/下检索结果。
配置Nutch与Hadoop集群
准备工作
1每台机器上安装jdk,配置java_home,版本必须一样。
2修改计算机的名,用hostname可以查看计算机的名称。
修改要改etc/profile. Etc/sysconfig/network两个文件,将hostname改成想要的。改完后要reboot机器才能生效。
修改集群中各个主机 的 /etc/hosts 文件:将集群中各个主机的 IP 与主机名都列在此处,
注意要将第一行注释掉: #127.0.0.1 localhost.localdomain localhost
假设我们的集群由3台计算机组成,分别为:cluster01, cluster02, cluster03,将cluster01作为master节点,其它两台作为slave节点。Nutch的抓取工作也将在cluster01上进行。
创建目录
当我们在集群中所有的机器上部署完Nutch以后,我们需要通过运行master节点上的start-all.sh脚本来启动整个集群。因此必须在 所有的机器上建立完全相同的目录结构来发布Nutch。在每 台机器的root用户(可自己创建一个nutch用户,只要保证每台机器用户名相同)的主目录/home/nutch下建立如下目录结构:
/home/nutch/nutch (Nutch 安装目录)
/home/nutch/filesystem (Hadoop文件系统目录)
/home/nutch/local (存放用于搜索的本地文件目录)
创建ssh密钥
接下来,在master节点上创建ssh密钥并将公钥复制到所有slave节点上,这样我们就能不用输入口令的情况下在master节点上用nutch用户ssh登录到slave节点。
cd /home/nutch
ssh-keygen -t rsa
在之后所有询问的提示符时直接回车,直到完成创建密钥。将生成的公钥另存为authorized_keys文件。
cd /home/nutch/.ssh
cp id_rsa.pub authorized_keys
用scp命令将authorized_keys文件复制到所有的slave节点上
scp /home/nutch/.ssh/authorized_keysnutch@cluster02:/home/nutch/.ssh/authorized_keys
依次使用scp命令将公钥文件复制到cluster02及cluster03上。可以在master上通过ssh nutch@cluster02来尝试登录cluster02检查密钥是否生效,如果不需要输入口令即可登录,则配置成功。依次检查所有slave节点的 登录状态。当所有slave节点都配置完成。
Hadoop配置
接下来对Hadoop进行配置,新版本中原来hadoop-site.xml配置文件中的内容将分别放在core-site.xml,hdfs-site.xml,mapred-site.xml中进行配置。
另外,本次配置将hadoop安装在目录usr/hadoop下。
建立完目录结构后,登录到master节点,也就是cluster01上,编辑hadoop运行环境变量配置文件:
cd /usr/hadoop/conf
vi hadoop-env.sh
填入以下配置信息:
export HADOOP_HOME=/usr/hadoop
export JAVA_HOME=/usr/java
export HADOOP_LOG_DIR=${HADOOP_HOME}/logs
export HADOOP_SLAVES=${HADOOP_HOME}/conf/slaves
conf/core-site.xml
<?xml-stylesheet type="text/xsl"href="configuration.xsl"?>
<!-- Put site-specific property overrides in thisfile. -->
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://cluster01:9000</value>
<description>
Where tofind the Hadoop Filesystem through the network.
Note 9000 isnot the default port.
(This isslightly changed from previous versions which didnt have "hdfs")
</description>
</property>
</configuration>
fs.default.name参数让nutch识别所使用的文件系统,由于我们使用Hadoop,所以必须指向master节点或者hadoop的namenode。
然后配置hadoop的分布式文件系统
conf/hdfs-site.xml
<?xml-stylesheet type="text/xsl"href="configuration.xsl"?>
<!-- Put site-specific property overrides in thisfile. -->
<configuration>
<property>
<name>dfs.name.dir</name>
<value>/home/nutch/filesystem/name</value>
</property>
<property>
<name>dfs.data.dir</name>
<value>/home/nutch/filesystem/data</value>
</property>
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
</configuration>
需要注意的是dfs.replication参数,它的值不能多于集群中的节点数,我们的集群有3个节点,所以我们这里将它的值设为3。
conf/mapred-site.xml
<?xml-stylesheet type="text/xsl"href="configuration.xsl"?>
<!-- Put site-specific property overrides in thisfile. -->
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>cluster01:9001</value>
<description>
The host andport that the MapReduce job tracker runs at. If
"local", then jobs are run in-process as a single map and
reduce task.
Note 9001 isnot the default port.
</description>
</property>
<property>
<name>mapred.map.tasks</name>
<value>2</value>
<description>
definemapred.map tasks to be number of slave hosts
</description>
</property>
<property>
<name>mapred.reduce.tasks</name>
<value>2</value>
<description>
definemapred.reduce tasks to be number of slave hosts
</description>
</property>
<property>
<name>mapred.system.dir</name>
<value>/home/nutch/filesystem/mapreduce/system</value>
</property>
<property>
<name>mapred.local.dir</name>
<value>/home/nutch/filesystem/mapreduce/local</value>
</property>
<property>
<name>mapred.temp.dir</name>
<value>/home/nutch/local/tmp</value>
</property>
</configuration>
在启动hadoop服务器前必须对namenode进行格式化
cd /home/nutch/nutch
bin/hadoop namenode -format
接下来,修改conf/master文件,将namenode配置进去
vi conf/master
内容只有一行,
cluster01
修改conf/slaves文件:
vi conf/slaves
将所有slave节点名写入,每个slave节点占一行
cluster02
cluster03
nutch配置
需要注意,必须在nutch-site.xml中配置http.agent.name参数的值,这个参数是给抓取的站点识别的爬虫的名称,在http.robots.agents参数中必须将http.agent.name的值放在列表的第一项。
下一步,在nutch工作目录下创建urls文件,在其中添加一个文本文件,该文本文件为nutch抓取URL的种子列表
mkdir /home/nutch/nutch/urls
vi /home/nutch/nutch/urls/nutch.txt
我们先抓取nutch的主页作为测试,在nutch.txt文件中只加入一行
把nutch/runtime/deploy的bin文件夹考到nutch安装目录下,然后把hadoop脚本考到bin下。
配置Nutch抓取过程中的URL过滤规则可以通过编辑conf/regex-urlfilter.txt文件来实现
vi conf/ regex -urlfilter.txt
将
+^http://([a-z0-9]*\.)*MY.DOMAIN.NAME/
修改为
+^http://([a-z0-9]*\.)*apache.org/
把# skip URLs containing certain characters as probable queries, etc.
-[?*!@=]
修改为# skip URLs containing certain characters as probable queries, etc.
+[?*!@=]
至此,我们已在NameNode上部署完单个nutch的运行环境。
将Nutch发布到所有slave节点的工作非常简单,将master节点上部署的nutch复制到所有slave节点上
scp -r /home/nutch/nutch/* nutch@cluster02:/home/nutch/nutch
依次用scp命令部署到cluster02与cluster03上。此时,我们已经可以启动hadoop服务了,在此之前我们可以执行以下命令查看所有slave节点是否正常工作
将nutch/conf下文件都考到hadoop/conf下,每台机器都要考。
在主结点上:
bin/slaves.sh uptime
如果输出信息中包含了所有slave节点的运行状态信息。则之前配置正确,我们可以用以下命令启动hadoop服务
bin/start-all.sh
使用下述命令可以检查hadoop集群的启动状态,该命令会输出已经启动完毕的slave节点以及当前集群可用及已用的容量等信息
bin/hadoop dfsadmin -report
当集群启动完毕,我们需要将抓取的种子列表加入到hadoop的文件系统中,使用以下命令
bin/hadoop dfs -put urls urls
检查是否添加成功
bin/hadoop dfs -ls
当添加成功后,我们就可以开始让Nutch抓取网页了。
执行抓取网页任务
执行命令开始抓取任务
bin/nutch crawl urls -dir crawl -depth 3
以上命令告诉Nutch抓取的种子文件在urls目录下,抓取结果放在crawl目录中,抓取的深度为5,Nutch的其它可选参数与使用方法可以参考Nutch的使用文档。
通过访问http://cluster01:50070可以查看namenode的状态及相关信息。
通过访问http://cluster01:50030可以查看mapreduce任务执行的情况。
注意:对hadoop配置进行修改要重新启动hadoop.
Nutch有readdb命令,可以将抓取的文件写到一个文件中