作者:不详 来源:互联网 酷勤网收集 2007-08-09
摘要
网络上流行的Nutch 0.7.2 学习笔记。
我的jdk 是1.5.x ,Tomcat是5.0.x
1 下载0.7.2 版本的包:里面已经包含了war文件,所以不需要Ant编译了
2 安装Cygwin,这个没什么好说的。
3 把Nutch借压缩到D:/nutch
4 在D:/nutch下面建立一个文件 urls (没有后缀)
5 在D:\nutch\conf\crawl-urlfilter.txt里面,加入
+^http://wwwybu.edu.cn/
6 环境变量加入
NUTCH_JAVA_HOME = D:\jdk1.5.0_06
7 打开cygwin窗口,
cd D:\nutch
在D:\nutch下面执行
这个命令会在当前目录下建立一个crawled的文件夹,然后对刚才的www.xxx.edu.cn的网站开始检索。
层数是3层,一般最好10层。然后结果输出在crawl.log里面
8 将nutch-0.7.2.war拷贝到tomcat/webapps下面,改名nutch
9 在D:\tomcat\conf\Catalina\localhost\ 建立nutch.xml
10 启动tomcat,等war解开以后,打开
D:\tomcat\webapps\nutch\WEB-INF\classes\nutch-site.xml<!--
Context configuration file for the Tomcat Manager Web App
$Id: manager.xml 303123 2004-08-26 17:03:35Z remm $
-->
<Context docBase="${catalina.home}/webapps/nutch"
privileged="true" antiResourceLocking="false" antiJARLocking="false">
![]()
</Context>
修改如下
searcher.dir
D:\nutch\crawled\
11 D:\tomcat\webapps\nutch\zh\include 下面新建header.jsp,内容就是复制header.html,但是
前面加上
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
在D:\tomcat\webapps\nutch\search.jsp里面,找到并修改为
"/>
顺便把下面js注释掉
function queryfocus() {
//search.query.focus();
}
12 D:\tomcat\conf\server.xml 找到以下段,并修改
<maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="UTF-8" useBodyEncodingForURI="true" />
了,到此,重启tomcat,访问 http://localhost:8080 就可以看到搜索主页了,而且搜索支持中文
和分词,虽然分得不是很好。
13 接下来一个重要的功能就是增量更新索引。
在D:\nutch下建立recrawl.sh 。内容为
#!/bin/bash
# A simple script to run a Nutch re-crawl
if [ -n "$1" ]
then
crawl_dir=$1
else
echo "Usage: recrawl crawl_dir [depth] [adddays]"
exit 1
fi
if [ -n "$2" ]
then
depth=$2
else
depth=5
fi
if [ -n "$3" ]
then
adddays=$3
else
adddays=0
fi
webdb_dir=$crawl_dir/db
segments_dir=$crawl_dir/segments
index_dir=$crawl_dir/index
# The generate/fetch/update cycle
for ((i=1; i <= depth ; i++))
do
bin/nutch generate $webdb_dir $segments_dir -adddays $adddays
segment=`ls -d $segments_dir/* | tail -1`
bin/nutch fetch $segment
bin/nutch updatedb $webdb_dir $segment
done
# Update segments
mkdir tmp
bin/nutch updatesegs $webdb_dir $segments_dir tmp
rm -R tmp
# Index segments
for segment in `ls -d $segments_dir/* | tail -$depth`
do
bin/nutch index $segment
done
# De-duplicate indexes
# "bogus" argument is ignored but needed due to
# a bug in the number of args expected
bin/nutch dedup $segments_dir bogus
# Merge indexes
ls -d $segments_dir/* | xargs bin/nutch merge $index_dir在cygwin里面,执行
./recrawl crawled 8
意思是更新索引,目录是crawled,层数8 。还可以把天数跟在后面。执行完毕后要重启Tomcat,因为
Nutch.war里面对查询有缓存。
最后可以把这个脚本放到crontab里面,每天夜里2点执行,执行完毕后重新启动Tomcat。
bin/nutch crawl urls -dir crawled -depth 3 >& crawl.log

