首先,参考笨小葱之前的博客,完成mysql的安装和nutch2.x的安装编译。然后,修改一些配置就可以完成集成工作啦
MySQL配置
1.my.cnf配置
分别在[client]、[mysql]下添加“default-character-set=utf8”;
在[mysqld]下添加:character-set-server=utf8
权限授予
mysql –u root –p xxxx
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "xxxx";
创建数据库与表
手动创建数据库nutch和数据表webpage【如果不想用默认的库名和表名也可在nutch安装后的相关配置文件中进行修改,见后续说明】,其中webpage的表结构如下:
CREATE TABLE `webpage` (
`id` varchar(767) CHARACTER SET latin1 NOT NULL,
`headers` blob,
`text` mediumtext,
`status` int(11) DEFAULT NULL,
`markers` blob,
`parseStatus` blob,
`modifiedTime` bigint(20) DEFAULT NULL,
`score` float DEFAULT NULL,
`typ` varchar(32) CHARACTER SET latin1 DEFAULT NULL,
`baseUrl` varchar(512) CHARACTER SET latin1 DEFAULT NULL,
`content` mediumblob,
`title` varchar(2048) DEFAULT NULL,
`reprUrl` varchar(512) CHARACTER SET latin1 DEFAULT NULL,
`fetchInterval` int(11) DEFAULT NULL,
`prevFetchTime` bigint(20) DEFAULT NULL,
`inlinks` mediumblob,
`prevSignature` blob,
`outlinks` mediumblob,
`fetchTime` bigint(20) DEFAULT NULL,
`retriesSinceFetch` int(11) DEFAULT NULL,
`protocolStatus` blob,
`signature` blob,
`metadata` blob,
`batchId` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这里如果报错字段指定的值过大,那么把大于255的值改成255就可以。这是因为utf8编码的字符大小限制。
NUTCH2.x配置
注意如果你集成的是nutch2.3,那么你在测试数据爬取的时候可能会碰到这个错误。
这是由于avro这个架包冲突了。2.3中对gora-sql的支持需要用到avro-1.3.3.jar而gora需要avro-1.7.6.jar的支持。所以在配置完nutch对mysql的支持后,运行测试网站爬取时会出这个错。你可以在$nutch_home/runtime/lib下面看看是否同时存在这两个架包。(这里笨小葱试着去掉一个架包,但是会报其他错误。试了好久也没有解决这个问题。希望大牛能够指点一下该如何做0.0)
由于工作任务要尽快完成,所以暂时先用了nutch2.2.1版本。这里如果不清楚怎么下载之前的版本的话,请看下面的截图。登陆到nutch官网下载页面:http://nutch.apache.org/downloads.html。
往下拉,找到这个超链接,里面就是历届nutch版本啦。
配置nutch对mysql的支持,修改${APACHE_NUTCH_HOME}/ivy/ivy.xml文件,分别:
l将以下行的注释取消
default”/>
l修改以下行
从默认的,改成
l将以下行的注释取消
注:上述第2和第3项,如果按默认的不做修改,将会在抓取网页时遇到以下错误。
Exception in thread “main” Java.lang.ClassNotFoundException:org.apache.gora.sql.store.SqlStore
3) 数据库连接配置
编辑${NUTCH_HOME}/conf/gora.properties文件,注释掉默认的数据库连接配置,同时添加以下配置内容:
###############################
#Default MySQL properties #
###############################
gora.sqlstore.jdbc.driver=com.mysql.jdbc.Driver
gora.sqlstore.jdbc.url=jdbc:mysql://localhost:3306/nutch?createDatabaseIfNotExist=true
gora.sqlstore.jdbc.user=xxxx(MySQL用户名)
gora.sqlstore.jdbc.password=xxxx(MySQL密码)
修改nutch-site配置文件
我的做法是直接将nutch-default文件另存为nutch-site,然后修改nutch-site内容,包括:
l添加http.agent.name的值
http.agent.name
YourNutchSpider
l在文件末尾添加以下内容
http.accept.language
ja-jp,en-us,en-gb,en;q=0.7,*;q=0.3
Value of theAccept-Language request header field.
This allows selecting non-Englishlanguage as default one to retrieve.
It is a useful setting for search enginesbuild for certain national group.
storage.data.store.class
org.apache.gora.sql.store.SqlStore
TheGora DataStore class for storing and retrieving data.
Currently the following stores areavailable:.
parser.character.encoding.default
utf-8
Thecharacter encoding to fall back to when no other information
isavailable
l特别添加以下内容
generate.batch.id
*
如果不添加此项内容,则通过”bin/nutch crawl urls –threadsn –depths n”爬取网页时,在日志中会看到以下错误:
java.lang.NullPointerExceptionatorg.apache.avro.util.Utf8.(Utf8.java:37)
atorg.apache.nutch.crawl.GeneratorReducer.setup(GeneratorReducer.java:100)
atorg.apache.hadoop.mapreduce.Reducer.run(Reducer.java:174)
atorg.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:649)
atorg.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:418)
atorg.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:398)
并且“nutch-site”文件需要保存为utf-8格式,否则在执行nutch命令时会出现以下错误。
Exception in thread “main”java.lang.RuntimeException:com.sun.org.apache.xerces.internal.impl.io.malformedByteSequenceException: 1字节的UTF-8序列的字节1无效。
网页抓取配置
1) 设置抓取的网站
cd ${NUTCH_HOME}/runtime/local
mkdir -p urls
echo 'http://www.tianya.cn' >urls/seed.txt
2) 执行爬取操作
bin/nutch crawl urls -depth 3 -topN 5
执行完在mysql中即可查看到爬虫抓取的内容,如下图: