Apache Nutch 1.3 学习笔记十(插件扩展)

 

1. 自己扩展一个简单的插件

   这里扩展一个NutchURLFilter插件,叫MyURLFilter

   1.1 生成一个Package

    首先生成一个与urlfilter-regex类似的包结构
org.apache.nutch.urlfilter.my

   1.2 在这个包中生成相应的扩展文件

    再生成一个MyURLFilter.java文件,内容如下:

 

  1. package org.apache.nutch.urlfilter.my;  
  2.     
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.     
  7.     
  8. import org.apache.hadoop.conf.Configuration;  
  9. import org.apache.nutch.net.URLFilter;  
  10. import org.apache.nutch.urlfilter.prefix.PrefixURLFilter;  
  11.     
  12.     
  13. public class MyURLFilter implements URLFilter{   // 这里的继承自NutchURLFilter扩展  
  14.     private Configuration conf;  
  15.         
  16.     public MyURLFilter()  
  17.     {}  
  18.     @Override  
  19.     public String filter(String urlString) {  // url字符串进行过滤  
  20.         // TODO Auto-generated method stub  
  21.         return "My Filter:"+ urlString;  
  22.     }  
  23.     
  24.     
  25.     @Override  
  26.     public Configuration getConf() {  
  27.         // TODO Auto-generated method stub  
  28.         return this.conf;  
  29.     }  
  30.     
  31.     
  32.     @Override  
  33.     public void setConf(Configuration conf) {  
  34.         // TODO Auto-generated method stub  
  35.         this.conf = conf;  
  36.     }  
  37.         
  38.     public static void main(String[] args) throws IOException  
  39.     {  
  40.              
  41.         MyURLFilter filter = new MyURLFilter();  
  42.             
  43.         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));  
  44.         String line;  
  45.         while((line=in.readLine())!=null) {  
  46.           String out=filter.filter(line);  
  47.           if(out!=null) {  
  48.             System.out.println(out);  
  49.           }  
  50.         }  
  51.     }  
  52.     
  53.     
  54. }  

 

    1.3 打包成jar包并生成相应的plugin.xml文件

打包可以用ivy或者是eclipse来打,每一个plugin都有一个描述文件plugin.xml,内容如下:

 

  1. <plugin  
  2.   id="urlfilter-my"  
  3.   name="My URL Filter"  
  4.   version="1.0.0"  
  5.   provider-name="nutch.org">  
  6.     
  7.     
  8.   <runtime>  
  9.      <library name="urlfilter-my.jar">  
  10.         <export name="*"/>  
  11.      </library>  
  12. <!-- 如果这里你的插件有依赖第三方库的话,可以这样写  
  13.      <library name="fontbox-1.4.0.jar"/>  
  14.      <library name="geronimo-stax-api_1.0_spec-1.jar"/>  
  15.   -->  
  16.   </runtime>  
  17.     
  18.     
  19.   <requires>  
  20.      <import plugin="nutch-extensionpoints"/>  
  21.   </requires>  
  22.     
  23.     
  24.   <extension id="org.apache.nutch.net.urlfilter.my"  
  25.              name="Nutch My URL Filter"  
  26.              point="org.apache.nutch.net.URLFilter">  
  27.      <implementation id="MyURLFilter"  
  28.                      class="org.apache.nutch.urlfilter.prefix.MyURLFilter"/>  
  29.      <!-- by default, attribute "file" is undefined, to keep classic behavior.  
  30.      <implementation id="PrefixURLFilter"  
  31.                      class="org.apache.nutch.net.PrefixURLFilter">  
  32.        <parameter name="file" value="urlfilter-prefix.txt"/>  
  33.      </implementation>  
  34.      -->  
  35.   </extension>  
  36.     
  37.     
  38. lt;/plugin>  

 

    1.4 把需要的包与配置文件放入plugins目录中

最后把打好的jar包与plugin.xml放到一个urlfilter-my文件夹中,再把这个文件夹到到nutchplugins目录下

 

2. 使用bin/nutch plugin来进行测试

在运行bin/nutch plugin命令之前你要修改一下nutch-site.xml这个配置文件,在下面加入我们写的插件,如下

 

  1. <property>  
  2.   <name>plugin.includes</name>  
  3.   <value>protocol-http|urlfilter-(regex|prefix|my)|parse-(html|tika)|index-(basic|anchor)|scoring-opic|urlnormalizer-(pass|regex|basic)</value>  
  4.   <description>Regular expression naming plugin directory names to  
  5.   include.  Any plugin not matching this expression is excluded.  
  6.   In any case you need at least include the nutch-extensionpoints plugin. By  
  7.   default Nutch includes crawling just HTML and plain text via HTTP,  
  8.   and basic indexing and search plugins. In order to use HTTPS please enable   
  9.   protocol-httpclient, but be aware of possible intermittent problems with the   
  10.   underlying commons-httpclient library.  
  11.   </description>  
  12. </property>  


在本机测试结果如下:

 

  1. lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ bin/nutch plugin urlfilter-my org.apache.nutch.urlfilter.my.MyURLFilter  
  2.        urlString1  
  3.        My Filter:urlString1  
  4.        urlString2  
  5.        My Filter:urlString2  


3.
总结
   
这里只是写了一个简单的插件,当然你可以根据你的需求写出更加复杂的插件
.

4. 参考

http://wiki.apache.org/nutch/WritingPluginExample#The_Example

 

作者:http://blog.csdn.net/amuseme_lu

 

 


 

 

 

 

 

相关文章阅读及免费下载:

 

 

 

Apache Nutch 1.3 学习笔记目录

 

 

 

Apache Nutch 1.3 学习笔记一

 

 

 

Apache Nutch 1.3 学习笔记二

 

 

 

Apache Nutch 1.3 学习笔记三(Inject)

 

 

 

Apache Nutch 1.3 学习笔记三(Inject CrawlDB Reader)

 

 

 

Apache Nutch 1.3 学习笔记四(Generate)

 

 

 

Apache Nutch 1.3 学习笔记四(SegmentReader分析)

 

 

 

Apache Nutch 1.3 学习笔记五(FetchThread)

 

 

 

Apache Nutch 1.3 学习笔记五(Fetcher流程)

 

 

 

Apache Nutch 1.3 学习笔记六(ParseSegment)

 

 

 

Apache Nutch 1.3 学习笔记七(CrawlDb - updatedb)

 

 

 

Apache Nutch 1.3 学习笔记八(LinkDb)

 

 

 

Apache Nutch 1.3 学习笔记九(SolrIndexer)

 

 

 

Apache Nutch 1.3 学习笔记十(Ntuch 插件机制简单介绍)

 

 

 

Apache Nutch 1.3 学习笔记十(插件扩展)

 

 

 

Apache Nutch 1.3 学习笔记十(插件机制分析)

 

 

 

Apache Nutch 1.3 学习笔记十一(页面评分机制 OPIC)

 

 

 

Apache Nutch 1.3 学习笔记十一(页面评分机制 LinkRank 介绍)

 

 

 

Apache Nutch 1.3 学习笔记十二(Nutch 2.0 的主要变化)

 

 

 

更多《Apache Nutch文档》,尽在开卷有益360 http://www.docin.com/book_360

 

转载于:https://www.cnblogs.com/ibook360/archive/2011/10/24/2222178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值