然后去nutch官网下载nutch,不过2.3.1的版本编译时有问题,切换maven2库也没用,会一直卡在以下界面:
root@ubuntu:/opt/apache-nutch-2.3.1# ant runtime
Buildfile: /opt/apache-nutch-2.3.1/build.xml
ivy-probe-antlib:
ivy-download:
ivy-download-unchecked:
ivy-init-antlib:
ivy-init:
init:
[mkdir] Created dir: /opt/apache-nutch-2.3.1/build
[mkdir] Created dir: /opt/apache-nutch-2.3.1/build/classes
[mkdir] Created dir: /opt/apache-nutch-2.3.1/build/release
[mkdir] Created dir: /opt/apache-nutch-2.3.1/build/test
[mkdir] Created dir: /opt/apache-nutch-2.3.1/build/test/classes
clean-lib:
resolve-default:
[ivy:resolve] :: Apache Ivy 2.3.0 - 20130110142753 :: http://ant.apache.org/ivy/ ::
[ivy:resolve] :: loading settings :: file = /opt/apache-nutch-2.3.1/ivy/ivysettings.xml
于是我放弃了,决定采用nutch2.2.1版本进行安装,nutch2.2.1下载地址:http://archive.apache.org/dist/nutch/2.2.1/
一、MySQL配置
1、my.ini配置
找到 [mysqld]在下面添加 skip-grant-tables和character-set-server=utf8
找到[mysql]、[client]在下面添加default-character-set=utf8
重启mysql服务
(注:)如果已有的话就不需要添加
2、创建数据库与表
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;
注:表中的字段根据nutch的conf文件“gora-sql-mapping”进行设置。同时也可通过自动方式生成数据库和表:配置好“gora-sql-mapping”、“gora.properties”及其它文件后,首次通过运行”bin/nutch inject urls”即可自动生成数据库和表,不过或许在自动生成的时候你会遇到问题,不过没有关系,通过及时查看hadoop.log文件你便会发现问题所在。
二、Nutch的安装与配置以及使用
1、Nutch-2.2.X下载:http://apache.fayea.com/apache-mirror/nutch/下载,然后解压至本地安装目录,如本地根目录为${NUTCH_HOME};
2、配置nutch对mysql的支持,修改${APACHE_NUTCH_HOME}/ivy/ivy.xml文件,分别:
1)找到以下行取消注释
2)修改以下行
默认为
修改后为
3)取消注释以下行
注释:上2)、3)如果不修改会有异常异常信息为
Exception in thread “main” Java.lang.ClassNotFoundException:org.apache.gora.sql.store.SqlStore
3、数据库连接配置
编辑${NUTCH_HOME}/conf/gora.properties文件,注释掉默认的数据库连接配置,同时添加以下配置内容:
###############################
# MySQL properties
################################
gora.sqlstore.jdbc.driver=com.mysql.jdbc.Driver
gora.sqlstore.jdbc.url=jdbc:mysql://192.168.58.1:3306/nutch?createDatabaseIfNotExist=true
gora.sqlstore.jdbc.user=root
gora.sqlstore.jdbc.password=liuxun123
写上你需要连接的数据库地址以及用户名密码
4、修改nutch-site配置文件
将以下内容添加至${NUTCH_HOME}/conf/nutch-site.xml中的configuration节点中
http.agent.name
LiuXun Nutch Spider
http.accept.language
ja-jp, en-us,en-gb,en;q=0.7,*;q=0.3
Value of the “Accept-Language” request header field.
This allows selecting non-English language as default one to retrieve.
It is a useful setting for search engines build for certain national group.
parser.character.encoding.default
utf-8
The character encoding to fall back to when no other information
is available
storage.data.store.class
org.apache.gora.sql.store.SqlStore
The Gora DataStore class for storing and retrieving data.
Currently the following stores are available: ….
generate.batch.id
*
5、编译Nutch-2.2.*
1)首先安装Ant
2)进入${NUTCH_HOME}目录下执行ant命令既可
3)编译成功后${NUTCH_HOME}目录下会有runtime这个目录
6、网页抓取以及配置
1)进入${NUTCH_HOME}/runtime/local目录下
2)设置抓取的网站
执行命令
mkdir -p urls
echo 'http://www.oschina.net/' > urls/seed.txt
3)爬取操作
bin/nutch crawl urls -depth 3 -topN 5
nutch命令前面章节介绍到了
执行完在mysql中即查看到爬虫抓取的内容,如下图:
如果编译过程中有如下错误提示:
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.
下载 sonar-ant-task-2.1.jar,将其拷贝到 $NUTCH_HOME/lib 目录下面是因为缺少lib包,解决办法如下:
修改 $NUTCH_HOME/build.xml,引入上面添加的jar包: