天天看点

转 nutch 插件开发[资料整理]

plugin(插件)为nutch提供了一些功能强大的部件,nutch中很多功能都是使用插件实现的,而使用者也可以自行开发更多适合自已的插件。

1:可扩展性       通过plugin,nutch允许任何人扩展它的功能,而我们要做的只是对给定的接口做简单的实现,举个例子:我们在nutch里使用loadbalance进行下载的插件,它就是一个对protocol这个接口的实现。

2:灵活性      因为每个人都可以根据自己的需求而写自己的plugin,这样plugin就会有一个很强大的资源库。这样对应用nutch程序员来说,他可以在自己的搜索引擎上安装符合自己需求的插件,而这些插件就在nutch的plugins中。这对于正在应用nutch的开发者来说应该是一个巨大的福音,因为你有了更多的关于内容抽取的算法来选择,很容易就增加了各种过滤规则、下载方式、解析文件类型等。

3:可维护性

每个开发者只要关注自己的问题。对于内核的开发者在为引擎内核扩展的同时,为plugin添加一个描述它的接口就可以了。一个plugin的开发者只要关注这个plugin所要实现的功能,而不需要知道整个系统是怎么工作的。它们仅仅需要知道的是plugin和plug之间交换的数据类型。这使得内核更加简单,更容易维护。

nutch的plugin系统是基于eclipse 2.x中对插件的使用。plugins对nutch的工作是很重要的。nutch中的fetch(下载)、parsing(分析),indexing(索引),searching(查询)等都是通过不同的plugins来实现的。

在编写一个plugin的时候,要为一个扩展点添加一个或者更多的扩展项。这些nutch的扩展点是nutch在一个plugin中已经定义好了,这个plugin是nutchextensionpoints(所有的扩展点都会在nutchextensionpoints 的plugin.xml这个文件中列出)。每一个扩展点都定义了一个接口,这个接口在扩展时必须被实现。

转 nutch 插件开发[资料整理]

这些扩展点如下:

indexingfiltering:

org.apache.nutch.indexer.indexingfilter

允许为所索引中的field添加元数据。所有的实现了这个接口plugin会在分析的过程中顺序的逐个运行.

parser:

org.apache.nutch.parse.parser    如果你要在nutch中扩展分析一个新内容类型或者从现有的可分析的内容摘取更多的数据。可实现接口parser,读取所抓取的document,摘取将被索引的数据。

htmlparsefilter:

org.apache.nutch.parse.htmlparsefilter

为html parser添加额外的元数据

这个接口是对以dom树为基础的html文档的分析器的扩展点,它允许你向这个htmlparsers添加metadata.

protocol:

org.apache.nutch.protocol.protocol

实现protocol的plugin可以使得nutch能使用更多的网络协议(ftp,http)去抓取数据

urlfileter:

org.apache.nutch.net.urlfilter

实现这个扩展点的plugin会对nutch要抓取的网页的urls进行限制,regexurlfilter提供了通过正则表达式来对nutch爬行网页的urls的控制。如果你对urls还有更加复杂的控制要求,你可以编写对这个urlfilter的实现

urlnormalizer:

org.apache.nutch.net.urlnormalizer

url规范化

scoringfilter:

org.apache.nutch.scoring.scoringfilter

nutch分数计算接口,可通过实现该接口以影响nutch分数的计算方式,其默认的opic分数计算方法由类 opicscoringfilter 提供。此外,参与计算分数的类还有 scoringfilters,parseoutputformat。其中 scoringfilters 负责将各个计分插件加载到系统中,并实现链式计分的过程;而parseoutputformat在解析结果输出之前,将页面的分数分配到该页面的各个子页面中,使得nutch的更新模块可以使用这些数据更新crawldb数据库。

segmentmergefilter:

org.apache.nutch.segment.segmentmergefilter

segment合并过滤接口。按一定的规则将多个segmnet进行合并、过滤。

转 nutch 插件开发[资料整理]

以protocol扩展点为例,我们对nutch中的数据下载方式进行改变,使用我们的方式进行下载。

首先,新建源码目录,目录中包含3个xml,一个源程序目录。

plugin.xml :向nutch描述这个plugin的信息

build.xml :告诉ant怎样编译这个plugin

ivy.xml:这个plugin的ivy配置信息

源程序有两个java类:

http.java:实现protocol接口 ,这里为简便,继承自httpbase类,httpbase类是一个api意义的类,实现了protocol接口,这样我们可减少很多工作。

转 nutch 插件开发[资料整理]

httpreponse.java:用于实现下载并返回信息。

图1:

转 nutch 插件开发[资料整理]

图2,接上图:

转 nutch 插件开发[资料整理]

plugin.xml说明

<plugin

   id="protocol-http-netty"  插件id

   name="protocol http netty plug-in" 插件名称

   version="1.0.0" 插件版本

   provider-name="pycredit"> 插件提供者的id

   <runtime>

      <library name="protocol-http-netty.jar"> 发布的jar包

         <export name="*"/> 

      </library>

   </runtime>

   <requires>

      <import plugin="nutch-extensionpoints"/> 依赖的插件

 <import plugin="lib-http">

   </requires>

   <extension id="org.apache.nutch.protocol.netty.http" 扩展的插件id

              name="httpprotocol" 扩展的插件名

              point="org.apache.nutch.protocol.protocol"> 插件的扩展点id

      <implementation id="org.apache.nutch.protocol.netty.http.http" 插件实现id

                      class="org.apache.nutch.protocol.netty.http.http"> 实现类

        <parameter name="protocolname" value="http"/>插件的参数

      </implementation>

      -->

   </extension>

</plugin>

实现接口编写代码完毕后修改配置步骤如下:

1、src/plugin/build.xml中,在

<target name="deploy">;

<target name="test">;

<target name="clean">;

分别增加相应配置,将我们新开发的插件添加进去,如:

<ant dir="protocol-http-netty" target="deploy">

 2、修改nutch/build.xml文件在

<target name="release" depends="compile-core" description="generate the release distribution">

中增加配置如:

<packageset dir="${plugins.dir}/protocol-http-netty/src/java"/>

 3、 注意检查${plugins.dir}/protocol-http-netty/src/build.xml,其project标签name值要为"protocol-http-netty"

如果要在nutch使用一个给定的plugin,你需要对conf/nutch-site.xml进行编辑并且把plugin的名字添加到plugin.includes中

<property>

<name>searcher.dir</name>

<value>i:/nutch-0.7.1/crawled</value>

</property>

<name>plugin.includes</name>

<value>nutch-extensionpoints|protocol-http|urlfilter-regex|parse-(text|html)|index-basic|query-(basic|site|url)|recommended

</value>