首先感谢wilco的文章: http://www.cnblogs.com/e-life/p/3897881.html
一、环境准备
首先,配置开发环境
需要的环境有JDK1.7,myeclipse,svn,ant一级myeclipse下的两个插件subeclipse和ivyDe,
下载地址 http://subeclipse.trigris.org/update_1.8.x
http://www.apache.org/dist/ant/ivyde/updatesite
二、从SVN检出项目,地址
https://svn.apache.org/repos/asf/nutch/tags/release-2.2.1
Finish完成导入.
三、修改ivy目录下的ivysettings.xml地址http://mirrors.ibiblio.org/maven2/ (试了下,这个还能访问到)
四、修改ivy目录下的ivy.xml(增加mysql访问以来java包)
修改gora-core版本为0.2.1,并解除注释gora-sql和mysql-connector-java
五、到项目根目录执行 ant eclipse
一开始会有如下问题:
具体解决办法是到ivy官网:http://ant.apache.org/ivy/index.html下载ivy-*-rc2.jar,复制到ant的lib目录下,再次执行 ant eclipse,就不会报错了,执行时间比较长,这是由于ivy要下载jar包,执行结果如下
六、回到myeclipse,刷新项目,会发现目录结果已经发生变化
七、工程右键Build Path-->Config Build Path-->Order and Export下选中Conf文件置顶
八、修改Conf文件夹下gora.properties配置mysql
九、工程目录下新建文件夹urls,url目录下新建一文件url,里面输入要爬取的root_url,比如:http://www.163.com
十、配置conf目录下nutch-site.xml
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Put site-specific property overrides in this file. --> <configuration> <property> <name>http.agent.name</name> <value>YourNutchSpider</value> </property> <property> <name>http.accept.language</name> <value>ja-jp, en-us,en-gb,en,zh-cn,zh-tw;q=0.7,*;q=0.3</value> <description>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. </description> </property> <property> <name>parser.character.encoding.default</name> <value>utf-8</value> <description>The character encoding to fall back to when no other information is available </description> </property> <property> <name>plugin.folders</name> <value>src/plugin</value> <description>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. </description> </property> </pre> <pre name="code" class="html"><!-- 为解决utf-8类中的空指针问题 --> <property> <span> </span> <name>generate.batch.id</name> <span> </span> <value>*</value> </property> <property> <name>storage.data.store.class</name> <value>org.apache.gora.sql.store.SqlStore</value> <description>The Gora DataStore class for storing and retrieving data. Currently the following stores are available: …. </description> </property> </configuration>
十一、配置完上述步骤后配置命令运行Run Configuation选择org.apache.nutch.crawl.Crawler,参数设置urls-depth 3 -topN 5 和Dhadoop.log.dir=logs-Dhadoop.file=hadoop.log
这时候运行会遇到错误Exception in thread "main" java.io.IOException:Failed to set permissions of Path:\tmp\hadoop-Administrator\mapred\staging\Administrator238829409\.staging to 0700
十二、上述错误一般只是在windows下才会遇到的,所以我们一般会去修改Haddop-core-1.2.0源码中的org.apache.hadoop.hadoop.fs下的FileUtil.java修改其中的CheckReturnValue方法,注释掉其中的内容
private static void checkReturnValue(boolean rv, File p, FsPermission permission ) throws IOException {// if (!rv) {// throw new IOException("Failed to set permissions of path: " + p + // " to " + // String.format("%04o", permission.toShort()));// } }
然后编译成java包替换我们工程build/lib下的hadoop-core-1.2.0.jar.
另外一种方法是找到FileUtil.java编译过后的class文件,替换掉jar包中相应的class文件FileUtil.class和FileUtil$CygPathCommand.class(用压缩软件打开直接替换即可)
十三、接下来在运行就没有问题
你会发现mysql数据库中很多记录除了id,status这几个基本字段有数据外其他值都是null,这是正常情况,请注意status字段,
status 记录抓取状态
1 unfetched (links not yet fetched due to limits set in regex-urlfilter.txt, -TopN crawl parameters, etc.)
2 fetched (page was successfully fetched)
3 gone (that page no longer exists)
4 redir_temp (temporary redirection — see reprUrl below for more details)
5 redir_perm (permanent redirection — see reprUrl below for more details)
34 retry
38 not modified
这是因为我们的topN设置太小,所以很多url被限制抓取了(当然还有可能是url被regex-urllfilter过滤了),自己注意一下就好了。到此我们就完成了整个流程的配置和运行。