Nutch 快速入门(Nutch 2.2.1)

本文详细介绍了如何利用Nutch2.x结合HBase进行网页数据抓取、存储与索引构建的过程,包括安装与配置HBase,编译与配置Nutch2.2.1,添加种子URL,设置过滤规则,安装Solr,以及使用Nutch的crawl脚本一键抓取网页,并通过HBaseshell查看抓取结果。

原文:http://cn.soulmachine.me/blog/20140201/


Nutch 2.x 与 Nutch 1.x 相比,剥离出了存储层,放到了gora中,可以使用多种数据库,例如HBase, Cassandra, MySql来存储数据了。Nutch 1.7 则是把数据直接存储在HDFS上。

1. 安装并运行HBase

为了简单起见,使用Standalone模式,参考 HBase Quick start

1.1 下载,解压

wget http://archive.apache.org/dist/hbase/hbase-0.90.4/hbase-0.90.4.tar.gz
tar zxf hbase-0.90.4.tar.gz

1.2 修改 conf/hbase-site.xml

内容如下

<configuration>
  <property>
    <name>hbase.rootdir</name>
    <value>file:///DIRECTORY/hbase</value>
  </property>
  <property>
    <name>hbase.zookeeper.property.dataDir</name>
    <value>/DIRECTORY/zookeeper</value>
  </property>
</configuration>

hbase.rootdir 目录是用来存放HBase的相关信息的,默认值是 /tmp/hbase-${user.name}/hbase ; hbase.zookeeper.property.dataDir 目录是用来存放zookeeper(HBase内置了zookeeper)的相关信息的,默认值是 /tmp/hbase-${user.name}/zookeeper 。

1.3 启动

$ ./bin/start-hbase.sh
starting Master, logging to logs/hbase-user-master-example.org.out

1.4 试用一下shell

$ ./bin/hbase shell HBase Shell; enter ‘help ’ for list of supported commands. Type “exit ” to leave the HBase Shell Version 0.90.4, r1150278, Sun Jul 24 15:53:29 PDT 2011

hbase(main):001:0>

创建一张名字为 test 的表,只有一个列,名为 cf 。为了验证创建是否成功,用list 命令查看所有的table,并用 put 命令插入一些值。

hbase(main):003:0> create 'test', 'cf'
0 row(s) in 1.2200 seconds
hbase(main):003:0> list 'test'
..
1 row(s) in 0.0550 seconds
hbase(main):004:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.0560 seconds
hbase(main):005:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0370 seconds
hbase(main):006:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0450 seconds

用 scan 命令扫描table,验证一下刚才的插入是否成功。

hbase(main):007:0> scan 'test'
ROW        COLUMN+CELL
row1       column=cf:a, timestamp=1288380727188, value=value1
row2       column=cf:b, timestamp=1288380738440, value=value2
row3       column=cf:c, timestamp=1288380747365, value=value3
3 row(s) in 0.0590 seconds

现在,disable并drop掉你的表,这会把上面的所有操作清零。

hbase(main):012:0> disable 'test'
0 row(s) in 1.0930 seconds
hbase(main):013:0> drop 'test'
0 row(s) in 0.0770 seconds 

退出shell,

hbase(main):014:0> exit

1.5 停止

$ ./bin/stop-hbase.sh
stopping hbase...............

1.6 再次启动

后面运行Nutch,需要把数据存储到HBase,因此需要启动HBase。

$ ./bin/start-hbase.sh
starting Master, logging to logs/hbase-user-master-example.org.out

2 编译Nutch 2.2.1

2.1 下载,解压

wget http://www.apache.org/dyn/closer.cgi/nutch/2.2.1/apache-nutch-2.2.1-src.tar.gz
tar zxf apache-nutch-2.2.1-src.tar.gz

2.2 修改配置文件

参考 Nutch 2.0 Tutorial

修改 conf/nutch-site.xml

<property>
  <name>storage.data.store.class</name>
  <value>org.apache.gora.hbase.store.HBaseStore</value>
  <description>Default class for storing data</description>
</property>

修改 ivy/ivy.xml

<!-- Uncomment this to use HBase as Gora backend. -->
<dependency org="org.apache.gora" name="gora-hbase" rev="0.3" conf="*->default" />

修改 conf/gora.properties ,确保 HBaseStore 被设置为默认的存储,

gora.datastore.default=org.apache.gora.hbase.store.HBaseStore

2.3 编译

ant runtime

刚开始会下载很多jar,需要等待一段时间。

有可能你会得到如下错误:

Trying to override old definition of task javac
  [taskdef] Could not load definitions from resource org/sonar/ant/antlib.xml. It could not be found.

ivy-probe-antlib:

ivy-download:
  [taskdef] Could not load definitions from resource org/sonar/ant/antlib.xml. It could not be found.

无所谓,不用管它。

要等一会儿才能编译结束。编译完后,多出来了 build 和 runtime两个文件夹。

第3、4、5、6步与另一篇博客 Nutch 快速入门(Nutch 1.7) 中的第3、4、5、6步骤一模一样。

3 添加种子URL

mkdir ~/urls
vim ~/urls/seed.txt
http://movie.douban.com/subject/5323968/

4 设置URL过滤规则

如果只想抓取某种类型的URL,可以在 conf/regex-urlfilter.txt 设置正则表达式,于是,只有匹配这些正则表达式的URL才会被抓取。

例如,我只想抓取豆瓣电影的数据,可以这样设置:

#注释掉这一行
# skip URLs containing certain characters as probable queries, etc.
#-[?*!@=]
# accept anything else
#注释掉这行
#+.
+^http:\/\/movie\.douban\.com\/subject\/[0-9]+\/(\?.+)?$

5 设置agent名字

conf/nutch-site.xml:

<property>
  <name>http.agent.name</name>
  <value>My Nutch Spider</value>
</property>

这一步是从这本书上看到的, Web Crawling and Data Mining with Apache Nutch ,第14页。

6 安装Solr

由于建索引的时候需要使用Solr,因此我们需要安装并启动一个Solr服务器。

参考 Nutch Tutorial 第4、5、6步,以及 Solr Tutorial 。

6.1 下载,解压

wget http://mirrors.cnnic.cn/apache/lucene/solr/4.6.1/solr-4.6.1.tgz tar -zxf solr-4.6.1.tgz

6.2 运行Solr

cd example
java -jar start.jar

验证是否启动成功

用浏览器打开 http://localhost:8983/solr/admin/ ,如果能看到页面,说明启动成功。

6.3 将Nutch与Solr集成在一起

将 NUTCH_DIR/conf/schema-solr4.xml 拷贝到SOLR_DIR/solr/collection1/conf/ ,重命名为schema.xml,并在<fields>...</fields> 最后添加一行(具体解释见 Solr 4.2 - what is _version_field? ),

<field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>

重启Solr,

# Ctrl+C to stop Solr
java -jar start.jar

第7步和第8步也和Nutch 1.7那篇博客中的7、8步很类似。主要区别在于,Nutch 2.x的所有数据,不再以文件和目录的形式存放在硬盘上,而是存放到HBase里。

7 一步一步使用单个命令抓取网页

TODO

8 使用crawl脚本一键抓取

刚才我们是手工敲入多个命令,一个一个步骤,来完成抓取的,其实Nutch自带了一个脚本, ./bin/crawl ,把抓取的各个步骤合并成一个命令,看一下它的用法

$ ./bin/crawl 
Missing seedDir : crawl <seedDir> <crawlID> <solrURL> <numberOfRounds>

注意 ,这里是 crawlId ,不再是 crawlDir 。

先删除第7节产生的数据,使用HBase shell,用 disable 删除表。

8.1 抓取网页

$ ./bin/crawl ~/urls/ TestCrawl http://localhost:8983/solr/ 2
  • ~/urls 是存放了种子url的目录
  • TestCrawl 是crawlId,这会在HBase中创建一张以crawlId为前缀的表,例如TestCrawl_Webpage。
  • http://localhost:8983/solr/ , 这是Solr服务器
  • 2,numberOfRounds,迭代的次数

过了一会儿,屏幕上出现了一大堆url,可以看到爬虫正在抓取!

fetching http://music.douban.com/subject/25811077/ (queue crawl delay=5000ms)
fetching http://read.douban.com/ebook/1919781 (queue crawl delay=5000ms)
fetching http://www.douban.com/online/11670861/ (queue crawl delay=5000ms)
fetching http://book.douban.com/tag/绘本 (queue crawl delay=5000ms)
fetching http://movie.douban.com/tag/科幻 (queue crawl delay=5000ms)
49/50 spinwaiting/active, 56 pages, 0 errors, 0.9 1 pages/s, 332 245 kb/s, 131 URLs in 5 queues
fetching http://music.douban.com/subject/25762454/ (queue crawl delay=5000ms)
fetching http://read.douban.com/reader/ebook/1951242/ (queue crawl delay=5000ms)
fetching http://www.douban.com/mobile/read-notes (queue crawl delay=5000ms)
fetching http://book.douban.com/tag/诗歌 (queue crawl delay=5000ms)
50/50 spinwaiting/active, 61 pages, 0 errors, 0.9 1 pages/s, 334 366 kb/s, 127 URLs in 5 queues

8.2 查看结果

./bin/nutch readdb -crawlId TestCrawl -stats

也可以进HBase shell 查看,

cd ~/hbase-0.90.4
./bin/hbase shell
hbase(main):001:0> scan 'TestCrawl_webpage'

屏幕开始不断输出内容,可以用Ctrl+C 结束。

在运行scan查看表中内容时,对于列的含义不确定时可以查看 conf/gora-hbase-mapping.xml 文件,该文件定义了列族及列的含义。


<p>Nutch的创始人是Doug Cutting,他同时也是Lucene、HadoopAvro开源项目的创始人。</p><p>Nutch诞生于2002年8月,是Apache旗下的一个用Java实现的开源搜索引擎项目,自Nutch1.2版本之后,Nutch已经从搜索引擎演化为网络爬虫,接着Nutch进一步演化为两大分支版本:1.X2.X,这两大分支最大的区别在于2.X对底层的数据存储进行了抽象以支持各种底层存储技术。</p><p>在Nutch的进化过程中,产生了Hadoop、Tika、GoraCrawler Commons四个Java开源项目。如今这四个项目都发展迅速,极其火爆,尤其是Hadoop,其已成为大规模数据处理的事实上的标准。Tika使用多种现有的开源内容解析项目来实现从多种格式的文件中提取元数据结构化文本,Gora支持把大数据持久化到多种存储实现,Crawler Commons是一个通用的网络爬虫组件。</p><p>大数据这个术语最早的引用可追溯到Nutch。当时,大数据用来描述为更新网络搜索索引需要同时进行批量处理或分析的大量数据集。现在,大数据的含义已经被极大地发展了,业界将大数据的特性归纳为4个“V”。Volume数据体量巨大,Variety数据类型繁多,Value价值密度低,商业价值高,Velocity处理速度快。</p><p>Hadoop是大数据的核心技术之一,而Nutch集Hadoop之大成,是Hadoop的源头。学习Hadoop,没有数据怎么办?用Nutch抓!学了Hadoop的Map Reduce以及HDFS,没有实用案例怎么办?学习NutchNutch的很多代码是用Map ReduceHDFS写的,哪里还能找到比Nutch更好的Hadoop应用案例呢?</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值