• 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

  • 相关阅读:
    自制凉皮
    牛人
    史记
    阅读detection
    最近的购书清单
    不要轻易挑战用户的习惯,否则会被用户打脸!
    INVEST原则的应用
    谈谈Backlog梳理活动
    要写封闭式的用户故事
    敏捷教练的八种失败角色
  • 原文地址:https://www.cnblogs.com/ibook360/p/2222178.html
Copyright © 2020-2023  润新知