Nutch1.7二次开发培训讲义

来源:互联网 发布:nginx 录播保存record 编辑:程序博客网 时间:2024/04/29 23:46

1、下载并解压eclipse(集成开发环境)

    使用Standard版

    下载地址:Eclipse Standard 4.3.2 For Windows 64Bit

              Eclipse Standard 4.3.2 For Windows 32Bit

              其他操作系统版本

 

2、安装Subclipse插件(SVN客户端)

    Help >Install new software… > Add… > Name:subclipse > Location:http://subclipse.tigris.org/update_1.8.x > 选中 Subclipse and SVNKit > Next> Next >选中I accept … > Finish > continue? OK> restart? > Yes

 

3、安装IvyDE插件(下载依赖Jar

   Help > Installnew software… > Add… > Name:ivyde > Location:http://www.apache.org/dist/ant/ivyde/updatesite/ >选中Apache Ivy Library andApache IvyDE Eclipseplugins > Next > Next >选中I accept … > Finish> continue? OK > restart? > Yes

 

4、签出代码

    File >New > Project > SVN > 从SVN 检出项目 > Next >选中 创建新的资源库位置 > Next > URL:https://svn.apache.org/repos/asf/nutch/tags/release-1.7/> Next >选中URL > Finish > 弹出New Project向导,选择JavaProject > Next > 输入Projectname:nutch1.7 > Finish > 确认覆盖? OK

 

5、配置文件编码和环境变量

    在左部Package Explorer的 nutch1.7文件夹上单击右键 > Properties > 左边选中Resource> 右边Text file encoding > 选中other > 值为:UTF-8

    在左部Package Explorer的 nutch1.7文件夹上单击右键 > Build Path > Configure Build Path... > 选中Source选项 > 选择src > Remove > Add Folder... > 选择src/java, src/test 和src/testresources(可选:对于插件,需要选中src/plugin目录下的每一个插件目录下的src/java, src/test文件夹) > OK

    切换到Libraries选项 >

    Add ClassFolder... > 选中nutch1.7/conf > OK

    AddLibrary... > IvyDE Managed Dependencies > Next > Main > Ivy File:> Project… > 选中nutch1.7/ivy/ivy.xml > 0K >Finish(可选:对于插件,还需要:Add JARs... >  IvyDEManaged Dependencies > Next > Main > Ivy File: > Project… > 选中nutch1.7/src/plugin/xxx/ivy.xml > OK,把这里的xxx替换为相应的插件名称

    切换到Order and Export选项>

   选中conf > Top(重要!)

 

6、执行ANT下载依赖构建项目

    在左部Package Explorer的 nutch1.7文件夹下的build.xml文件上单击右键 > Run As > Ant Build> BUILD SUCCESSFUL > 生成nutch1.7/build/apache-nutch-1.7.job

    在nutch1.7\runtim目录下生成两个目录deploylocal,他们用于生产环境,跟二次开发没什么关系,deploy依赖Hadoop,local不依赖Hadoop

    在左部Package Explorer的 nutch1.7文件夹上单击右键 > Refresh

    在左部Package Explorer的 nutch1.7文件夹上单击右键 > Build Path > Configure Build Path... > 选中Libraries选项 > Add Class Folder... > 选中build > OK

 

7、修改配置文件

    如nutch1.7/conf下不存在nutch-site.xml,则将nutch1.7/conf/nutch-site.xml.template复制一份改名为nutch1.7/conf/nutch-site.xml

    如nutch1.7/conf下不存在regex-urlfilter.txt,则将nutch1.7/conf/regex-urlfilter.txt.template复制一份改名为nutch1.7/conf/regex-urlfilter.txt

    如新增了文件,则在左部Package Explorer的 nutch1.7文件夹上单击右键 > Refresh

    配置nutch-site.xml,新增:

<property>

         <name>http.agent.name</name>

         <value>nutch-crawler</value>

</property>

<property>

         <name>http.content.limit</name>

         <value>-1</value>

</property>

<property>

       <name>db.max.outlinks.per.page</name>

       <value>10000</value>

</property>

    配置regex-urlfilter.txt,将

# acceptanything else

+.

    替换为:

+^http://([a-z0-9]*\.)*news.163.com/

-.

8、开发运行调试

    在左部Package Explorer的 nutch1.7文件夹上单击右键 > New > Folder > Folder name: urls

    在刚新建的urls目录下新建一个文本文件url,文本内容为:http://news.163.com

    打开src/java下的org.apache.nutch.crawl.Crawl.java类,单击右键Run As > Java Application > Console显示:Usage:Crawl <urlDir> -solr <solrURL> [-dir d] [-threads n] [-depth i][-topN N]

    在Crawl类上重新单击右键Run As > RunConfigurations… > Arguments > 在Programarguments输入框中输入: urls -dir data -depth 3 > Run

   1. 在windows环境中如果抛出异常Failed to set permissions of path,则需要下载修改过的hadoop,替换nutch依赖的hadoop。下载地址:http://pan.baidu.com/s/1o60QtD4,因为nutch1.7依赖的hadoop版本为1.2.0,提供下载的是1.2.1,所以将下载的hadoop拷贝到ivy本地存储库C:/Users/ysc/.ivy2/cache/org.apache.hadoop/hadoop-core/jars目录,删除原来的1.2.0,将1.2.1改为1.2.0。

    2.在windows环境中如果抛出异常nutch 报Exception in thread "main"java.io.IOException: Job failed!

在nutch-site.xml 中添加

    <property>
 <name>plugin.folders</name>
 <value>runtime/local/plugins</value>
   </property>

3.在windows环境中如果抛出异常nutch 报java.lang.RuntimeException: x point org.apache.nutch.net.URLNormalizer not found.       原因:regex-urlfilter.txt中的正则表达式nutch不接受 或者 

 eclipse下Order and Export选项> 未选中conf > Top(重要!)


    在需要调试的地方打上断点Debug As > Java Applicaton

 

9、查看结果

    查看segments目录:

    打开src/java下的org.apache.nutch.segment.SegmentReader.java类

    单击右键Run As > Java Applicaton,控制台会输出该命令的使用方法

    单击右键Run As > Run Configurations > Arguments > 在Program arguments输入框中输入: -dumpdata/segments/*  data/segments/dump

    用文本编辑器打开文件data/segments/dump/dump查看segments中存储的信息

 

    查看crawldb目录:

    打开src/java下的org.apache.nutch.crawl.CrawlDbReader.java类

    单击右键Run As > Java Applicaton,控制台会输出该命令的使用方法

    单击右键Run As > Run Configurations > Arguments > 在Program arguments输入框中输入:data/crawldb -stats

    控制台会输出 crawldb统计信息

 

    查看linkdb目录:

    打开src/java下的org.apache.nutch.crawl.LinkDbReader.java类

    单击右键Run As > Java Applicaton,控制台会输出该命令的使用方法

    单击右键Run As > Run Configurations > Arguments > 在Program arguments输入框中输入:data/linkdb -dump data/linkdb_dump

    用文本编辑器打开文件data/linkdb_dump/part-00000查看linkdb中存储的信息

 

10、全网分步骤抓取(可选)

    在左部Package Explorer的 nutch1.7文件夹下的build.xml文件上单击右键 > Run As > Ant Build

    cd  /home/ysc/workspace/nutch1.7/runtime/local

    #准备URL列表

    wgethttp://rdf.dmoz.org/rdf/content.rdf.u8.gz

    gunzipcontent.rdf.u8.gz

    mkdir dmoz

    bin/nutchorg.apache.nutch.tools.DmozParser content.rdf.u8 -subset 5000 > dmoz/url

    #注入URL

    bin/nutchinject crawl/crawldb dmoz

    #生成抓取列表

    bin/nutchgenerate crawl/crawldb crawl/segments

    #第一次抓取,s1是最新产生的segment

    s1=`ls -d crawl/segments/2* | tail -1`

    echo $s1

    #抓取网页

    bin/nutchfetch $s1

    #解析网页

    bin/nutchparse $s1

    #更新URL状态

    bin/nutchupdatedb crawl/crawldb $s1

    #第二次抓取,生成抓取列表,s2是最新产生的segment

    bin/nutchgenerate crawl/crawldb crawl/segments -topN 1000

    s2=`ls -d crawl/segments/2* | tail -1`

    echo $s2

    bin/nutchfetch $s2

    bin/nutchparse $s2

    bin/nutchupdatedb crawl/crawldb $s2

    #第三次抓取,生成抓取列表,s3是最新产生的segment

    bin/nutchgenerate crawl/crawldb crawl/segments -topN 1000

    s3=`ls -d crawl/segments/2* | tail -1`

    echo $s3

    bin/nutchfetch $s3

    bin/nutchparse $s3

    bin/nutchupdatedb crawl/crawldb $s3

    #生成反向链接库

    bin/nutchinvertlinks crawl/linkdb -dir crawl/segments

    #建索引

    bin/nutchsolrindex http://localhost:8983/solr/collection1 data/crawldb -linkdbdata/linkdb -dir data/segments

 

    bin/crawl提供了更简单的增量抓取脚本

 

11、索引和搜索

       下载解压solr,为了查看索引文件的格式,使用solr-4.6.1

       下载地址:http://pan.baidu.com/s/1hqxEFxq

                   

    #配置solr core

    复制nutch的conf目录中的schema-solr4.xml文件到solr-4.6.1/example/solr/collection1/conf目录,覆盖名为schema.xml的文件 

    修改solr-4.6.1/example/solr/collection1/conf/schema.xml,在<fields>下增加:<fieldname="_version_" type="long" indexed="true"stored="true"/> 

    #配置中文分词

    下载中文分词依赖的Jar:http://pan.baidu.com/s/1i37gcg1

    创建目录solr-4.6.1/example/solr/lib,并将下载下来的压缩文件中的3个jar文件提取出来放到该目录

    修改文件solr-4.6.1/example/solr/collection1/conf/schema.xml        

         将字段类型text_generalanalyzerindexquerytokenizer分别改为:

       <tokenizerclass="org.ansj.solr.AnsjTokenizerFactory"conf="ansj.conf"/>

       

       <tokenizerclass="org.ansj.solr.AnsjTokenizerFactory"analysisType="1"/>

       创建文件solr-4.6.1/example/solr/collection1/conf/ansj.conf,输入:

lastupdate=123

files=dic/customDic.txt

    创建文件dic/customDic.txt,这就是自定义用户词典

         #启动SOLR服务器

       运行Jar文件:solr-4.6.1/example/start.jar

       #浏览器管理界面

       http://localhost:8983/solr

    http://localhost:8983/solr/#/colletion1

 

12、查看索引信息

    下载Luke(Lucene Index Toolbox)http://pan.baidu.com/s/1bn6CuQV  

    将索引文件solr-4.6.1/example/solr/collection1/data/index复制一份到其他目录,删除文件write.lock

    File >Open Lucene Index > 选择复制的索引文件路径

 

13、插件开发

    复制nutch1.7/src/plugin/parse-html,重命名为parse-jsoup,去掉对lib-nekohtml的依赖,修改build.xml、ivy.xml和plugin.xml,依赖的jar包jsoup配置到ivy.xml

    修改nutch1.7/src/plugin/build.xml,加入新的插件parse-jsoup的配置,以便构建的时候能编译新的插件,有3个配置项,参考parse-html

    将新插件加入Build Path(Source和Libraries)

    修改nutch1.7/conf/parse-plugins.xml,加入mimeType和alias

 

0 0
原创粉丝点击