nutch eclipse mysql_搭建eclipse环境下 Nutch+Mysql 二次开发环境

最近看了下Nutch,目前Nutch最新版本2.3.1,支持Hbase、MongoDB等存储,但在搭建和测试过程中发现对Mysql 的支持好像有点问题。

后来将Nutch版本改为2.2.1。基于Nutch2.2.1+Mysql 的环境配置过程如下:

1.下载Nutch2.2.1 源码:SVN:https://svn.apache.org/repos/asf/nutch/branches/branch-2.2.1

2.修改Nutch2.2.1 源码中的ivy/ivysetings.xml

添加一个源:

value="http://maven.restlet.org"

override="false"/>

增加以下红色部分代码

经过测试,没有增加这个有些包下载不了,可能和网络有关系。

3.修改ivy/ivy.xml

启用以下两个依赖

4.进入命令行,并定位到Nutch目录

执行:

ant eclipse -verbose

由于网络带宽问题,整个过程执行了半个小时

执行完成之后如下图所示

54475f845dccf5c8792c780eae83cce9.png

发现build文件夹比原来多了很多内容。

5. 打开Eclipse

使用Import 导入Nutch工程

4397e596aea7d6e3ec50ff213ac5f1a9.png

096f8acaa216b8bc6fa2ab744b5244a6.png

6.配置conf/nutch-site.xml

cfb28d34f2d0215d17fdc50d413310dc.gif

http.agent.name

YourNutchSpider

http.accept.language

ja-jp, en-us,en-gb,en,zh-cn,zh-tw;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

plugin.folders

src/plugin

Directories where nutch plugins are located. Each

element may be a relative or absolute path. If absolute, it is used

as is. If relative, it is searched for on the classpath.

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

*

7.配置 gora.properties

gora.datastore.default=org.apache.gora.sql.store.SqlStore

gora.datastore.autocreateschema=true

gora.sqlstore.jdbc.driver=com.mysql.jdbc.Driver

gora.sqlstore.jdbc.url=jdbc:mysql://localhost:3306/nutch?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull

gora.sqlstore.jdbc.user=root

gora.sqlstore.jdbc.password=

8.创建mysql数据库和表结构

CREATE TABLE webpage (

id varchar(256) NOT NULL,

headers blob,

text longtext DEFAULT NULL,

status int(11) DEFAULT NULL,

markers blob,

parseStatus blob,

modifiedTime bigint(20) DEFAULT NULL,

prevModifiedTime bigint(20) DEFAULT NULL,

score float DEFAULT NULL,

typ varchar(32) CHARACTER SET latin1 DEFAULT NULL,

batchId varchar(32) CHARACTER SET latin1 DEFAULT NULL,

baseUrl varchar(256) DEFAULT NULL,

content longblob,

title text DEFAULT NULL,

reprUrl varchar(256) 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,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

9. 配置Crawler.java 的执行参数

在run configuration 中配置java application  main class org.apache.nutch.crawl.Crawler

args配置:urls -depth 3 -topN 5

vm参数配:-Xms64m -Xmx512m    这项如果不配,会报内存溢出。

这时候就可以debug或者run了。按道理是可以出来了

be1242fd3a57487aea6250bd2b9035af.png

10. 修改Hadoop的FileUtil.java

由于windows平台问题,需要修改FileUtil.java 代码,将红色部分注释掉。否则在执行Crawl 过程中会报Hadoop的路径权限错误

1 private static void checkReturnValue(boolean rv, File p, FsPermission permission)

2 throws IOException

3 {

4 //if (!rv)

5 // throw new IOException(new StringBuilder().append("Failed to set permissions of path: ").append(p).append(" to ").append(String.format("%04o", new Object[] { Short.valueOf(permission.toShort()) })).toString());

6 }

11. 在工程目录创建urls 文件夹,并在文件夹中创建seed.txt文件

添加需要爬取的网站URL路径,如: http://www.cnblogs.com/

注意:这个urls文件夹与Crawler执行参数的urls 对应。

12.执行Crawler.java 观察Mysql 数据

13、按道理讲,源码应该不用修改。事实证明,源码也有问题,运行的时候会出错。

GeneratorJob类在运行的时候会出现空指针异常,需要在163行run 方法内,增加:

// generate batchId

int randomSeed = Math.abs(new Random().nextInt());

String batchId = (System.currentTimeMillis() / 1000) + "-" + randomSeed;

getConf().set(BATCH_ID, batchId);

14.在大多数情况下,网站可能配置了反爬虫的功能robots.txt

Nutch也遵守了该协议,但可以通过修改Nutch的源码绕过反爬虫。

只需要将类FetcherReducer 的以下这个代码注释掉即可

/*

if (!rules.isAllowed(fit.u.toString())) {

// unblock

fetchQueues.finishFetchItem(fit, true);

if (LOG.isDebugEnabled()) {

LOG.debug("Denied by robots.txt: " + fit.url);

}

output(fit, null, ProtocolStatusUtils.STATUS_ROBOTS_DENIED,

CrawlStatus.STATUS_GONE);

continue;

}

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值