Nutch1.2增加插件例子

本文详细介绍了如何在Nutch 1.2中开发一个插件来实现推荐网站功能,包括创建插件结构、编写代码、配置插件、测试等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今尝试下给nutch1.2增加一个插件,于是到官网找了个例子,链接如下:

http://wiki.apache.org/nutch/WritingPluginExample-0.9

这个例子实现的的是推荐网站,就是写关键字在content里,当别人搜索这个关键字时,你推荐的网站在搜索结果中排前,要实现推荐必须在你的网页上加上

[xhtml]  view plain copy
  1. <meta name="recommended" content="plugins" />  

这条属性才能被插件识别。

由于它这个例子是用nutch0.9的,而且1.2和0.9有些区别,于是要修改一些代码。步骤如下:

1.插件开放

1.1在src/plugin中新建一个文件夹recommend

1.2.在recommend目录下新建Plugin.xml和Build.xml文件,内容如下:

 

Plugin.xml

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <plugin  
  3.    id="recommended"  
  4.    name="Recommended Parser/Filter"  
  5.    version="0.0.1"  
  6.    provider-name="nutch.org">  
  7.   
  8.    <runtime>  
  9.       <!-- As defined in build.xml this plugin will end up bundled as recommended.jar -->  
  10.       <library name="recommended.jar">  
  11.          <export name="*"/>  
  12.       </library>  
  13.    </runtime>  
  14.   
  15.    <!-- The RecommendedParser extends the HtmlParseFilter to grab the contents of  
  16.         any recommended meta tags -->  
  17.    <extension id="org.apache.nutch.parse.recommended.recommendedfilter"  
  18.               name="Recommended Parser"  
  19.               point="org.apache.nutch.parse.HtmlParseFilter">  
  20.       <implementation id="RecommendedParser"  
  21.                       class="org.apache.nutch.parse.recommended.RecommendedParser"/>  
  22.    </extension>  
  23.   
  24.    <!-- TheRecommendedIndexer extends the IndexingFilter in order to add the contents  
  25.         of the recommended meta tags (as found by the RecommendedParser) to the lucene  
  26.         index. -->  
  27.    <extension id="org.apache.nutch.parse.recommended.recommendedindexer"  
  28.               name="Recommended identifier filter"  
  29.               point="org.apache.nutch.indexer.IndexingFilter">  
  30.       <implementation id="RecommendedIndexer"  
  31.                       class="org.apache.nutch.parse.recommended.RecommendedIndexer"/>  
  32.    </extension>  
  33.   
  34.    <!-- The RecommendedQueryFilter gets called when you perform a search. It runs a  
  35.         search for the user's query against the recommended fields.  In order to get  
  36.         add this to the list of filters that gets run by default, you have to use  
  37.         "fields=DEFAULT". -->     
  38.    <extension id="org.apache.nutch.parse.recommended.recommendedSearcher"  
  39.               name="Recommended Search Query Filter"  
  40.               point="org.apache.nutch.searcher.QueryFilter">  
  41.       <implementation id="RecommendedQueryFilter"  
  42.                       class="org.apache.nutch.parse.recommended.RecommendedQueryFilter">  
  43.         <parameter name="fields" value="recommended"/>  
  44.         </implementation>  
  45.    </extension>  
  46.   
  47. </plugin>  

Build.xml

[xhtml]  view plain copy
  1. <?xml version="1.0"?>  
  2.   
  3. <project name="recommended" default="jar-core">  
  4.   
  5.   <import file="../build-plugin.xml"/>  
  6.     
  7.  <!-- Build compilation dependencies -->  
  8.  <target name="deps-jar">  
  9.    <ant target="jar" inheritall="false" dir="../lib-xml"/>  
  10.  </target>  
  11.   
  12.   <!-- Add compilation dependencies to classpath -->  
  13.  <path id="plugin.deps">  
  14.    <fileset dir="${nutch.root}/build">  
  15.      <include name="**/lib-xml/*.jar" />  
  16.    </fileset>  
  17.  </path>  
  18.   
  19.   <!-- Deploy Unit test dependencies -->  
  20.  <target name="deps-test">  
  21.    <ant target="deploy" inheritall="false" dir="../lib-xml"/>  
  22.    <ant target="deploy" inheritall="false" dir="../nutch-extensionpoints"/>  
  23.    <ant target="deploy" inheritall="false" dir="../protocol-file"/>  
  24.  </target>  
  25.   
  26.    
  27.   <!-- for junit test -->  
  28.   <mkdir dir="${build.test}/data"/>  
  29.   <copy file="data/recommended.html" todir="${build.test}/data"/>  
  30. </project>  

1.3.在recommended目录下建立/src/java/org/apache/nutch/parse/recommended目录。

1.4.增加RecommendedIndexer.java,RecommendedParser.java,RecommendedQueryFilter.java三个类,内容如下:

RecommendedIndexer.java

[java]  view plain copy
  1. package org.apache.nutch.parse.recommended;  
  2.   
  3. // JDK import  
  4. import java.util.logging.Logger;  
  5.   
  6. // Commons imports  
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9.   
  10.   
  11. // Nutch imports  
  12. import org.apache.nutch.util.LogUtil;  
  13. import org.apache.nutch.fetcher.FetcherOutput;  
  14. import org.apache.nutch.indexer.IndexingFilter;  
  15. import org.apache.nutch.indexer.IndexingException;  
  16. import org.apache.nutch.indexer.NutchDocument;  
  17. import org.apache.nutch.parse.Parse;  
  18.   
  19. import org.apache.hadoop.conf.Configuration;  
  20. import org.apache.hadoop.io.Text;  
  21. import org.apache.nutch.crawl.CrawlDatum;  
  22. import org.apache.nutch.crawl.Inlinks;  
  23.   
  24. // Lucene imports  
  25. import org.apache.lucene.document.Field;  
  26. import org.apache.lucene.document.Document;  
  27.   
  28. public class RecommendedIndexer implements IndexingFilter {  
  29.       
  30.   public static final Log LOG = LogFactory.getLog(RecommendedIndexer.class.getName());  
  31.     
  32.   private Configuration conf;  
  33.     
  34.   public RecommendedIndexer() {  
  35.   }  
  36.   @Override  
  37.   public NutchDocument filter(NutchDocument doc, Parse parse, Text url,   
  38.     CrawlDatum datum, Inlinks inlinks)  
  39.     throws IndexingException {  
  40.   
  41.     String recommendation = parse.getData().getMeta("recommended");  
  42.   
  43.         if (recommendation != null) {  
  44.             Field recommendedField =   
  45.                 new Field("recommended", recommendation,   
  46.                     Field.Store.YES, Field.Index.NOT_ANALYZED);  
  47.             recommendedField.setBoost(5.0f);  
  48.             doc.add("recommended",recommendedField);  
  49.             LOG.info("Added " + recommendation + " to the recommended Field");  
  50.         }  
  51.   
  52.     return doc;  
  53.   }  
  54.     
  55.   public void setConf(Configuration conf) {  
  56.     this.conf = conf;  
  57.   }  
  58.   
  59.   public Configuration getConf() {  
  60.     return this.conf;  
  61.   }  
  62.   
  63. @Override  
  64. public void addIndexBackendOptions(Configuration conf) {  
  65.     // TODO Auto-generated method stub  
  66. }  
  67. }  

RecommendedParser.java

[java]  view plain copy
  1. package org.apache.nutch.parse.recommended;  
  2.   
  3. // JDK imports  
  4. import java.util.Enumeration;  
  5. import java.util.Properties;  
  6. import java.util.logging.Logger;  
  7.   
  8. // Nutch imports  
  9. import org.apache.hadoop.conf.Configuration;  
  10. import org.apache.nutch.metadata.Metadata;  
  11. import org.apache.nutch.parse.HTMLMetaTags;  
  12. import org.apache.nutch.parse.Parse;  
  13. import org.apache.nutch.parse.HtmlParseFilter;  
  14. import org.apache.nutch.parse.ParseResult;  
  15. import org.apache.nutch.protocol.Content;  
  16.   
  17. // Commons imports  
  18. import org.apache.commons.logging.Log;  
  19. import org.apache.commons.logging.LogFactory;  
  20.   
  21. // W3C imports  
  22. import org.w3c.dom.DocumentFragment;  
  23.   
  24. public class RecommendedParser implements HtmlParseFilter {  
  25.   
  26.   private static final Log LOG = LogFactory.getLog(RecommendedParser.class.getName());  
  27.     
  28.   private Configuration conf;  
  29.   
  30.   /** The Recommended meta data attribute name */  
  31.   public static final String META_RECOMMENDED_NAME="recommended";  
  32.   
  33.   /** 
  34.    * Scan the HTML document looking for a recommended meta tag. 
  35.    */  
  36.     
  37.   @Override  
  38.   public ParseResult filter(Content content, ParseResult parseResult,  
  39.     HTMLMetaTags metaTags, DocumentFragment doc) {  
  40.     // Trying to find the document's recommended term  
  41.     String recommendation = null;  
  42.   
  43.     Properties generalMetaTags = metaTags.getGeneralTags();  
  44.   
  45.     for (Enumeration tagNames = generalMetaTags.propertyNames(); tagNames.hasMoreElements(); ) {  
  46.         if (tagNames.nextElement().equals("recommended")) {  
  47.             System.out.println(generalMetaTags.getProperty("recommended"));  
  48.             recommendation = generalMetaTags.getProperty("recommended");  
  49.            LOG.info("Found a Recommendation for " + recommendation);  
  50.         }  
  51.     }  
  52.   
  53.     if (recommendation == null) {  
  54.         LOG.info("No Recommendation");  
  55.     } else {  
  56.         LOG.info("Adding Recommendation for " + recommendation);  
  57.         Parse parse = parseResult.get(content.getUrl());  
  58.           
  59.         parse.getData().getContentMeta().set(META_RECOMMENDED_NAME, recommendation);  
  60.     }  
  61.   
  62.     return parseResult;  
  63.   }  
  64.     
  65.   public void setConf(Configuration conf) {  
  66.     this.conf = conf;  
  67.   }  
  68.   
  69.   public Configuration getConf() {  
  70.     return this.conf;  
  71.   }  
  72.   
  73.   
  74.   
  75. }  

RecommendedQueryFilter.java

[java]  view plain copy
  1. package org.apache.nutch.parse.recommended;  
  2.   
  3. import org.apache.nutch.searcher.FieldQueryFilter;  
  4.   
  5. import java.util.logging.Logger;  
  6.   
  7. // Commons imports  
  8. import org.apache.commons.logging.Log;  
  9. import org.apache.commons.logging.LogFactory;  
  10.   
  11.   
  12. public class RecommendedQueryFilter extends FieldQueryFilter {  
  13.     private static final Log LOG = LogFactory.getLog(RecommendedParser.class.getName());  
  14.   
  15.     public RecommendedQueryFilter() {  
  16.         super("recommended", 5f);  
  17.         LOG.info("Added a recommended query");  
  18.     }  
  19.     
  20. }  

1.5.在 src/plugin/build.xml 中的<target name="deploy"></target>中增加一行:

[xhtml]  view plain copy
  1. <ant dir="recommended" target="deploy" />  

1.6.运行cmd,切换到recommend目录,运行ant命令编译,插件开发完成。

 

1.7 让nutch识别你的插件

      在conf/nutch-site.xml 中增加一下属性

[c-sharp]  view plain copy
  1. <property>  
  2.   <name>plugin.includes</name>  
  3.   <value>recommended|protocol-http|urlfilter-regex|parse-(text|html|js)|index-basic|query-(basic|site|url)|summary-basic|scoring-opic|urlnormalizer-(pass|regex|basic)</value>  <description>Regular expression naming plugin id names to  
  4.   include.  Any plugin not matching this expression is excluded.  
  5.   In any case you need at least include the nutch-extensionpoints plugin. By  
  6.   default Nutch includes crawling just HTML and plain text via HTTP,  
  7.   and basic indexing and search plugins.  
  8.   </description>  
  9. </property>  

 

2.编写插件测试类

 

2.1 在src/plugin中/recommend目录下新建一个data目录,在data目录下新建一个html文件recommended.html内容如下:

[xhtml]  view plain copy
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">  
  2.   
  3. <html lang="en">  
  4. <head>  
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  6.     <title>recommended</title>  
  7.     <meta name="generator" content="TextMate http://macromates.com/">  
  8.     <meta name="author" content="Ricardo J. Méndez">  
  9.     <meta name="recommended" content="recommended-content"/>  
  10.     <!-- Date: 2007-02-12 -->  
  11. </head>  
  12. <body>  
  13.     Recommended meta tag test.  
  14. </body>  
  15. </html>  

2.2 在src/plugin中/recommend目录下新建src/test/org/apache/nutch/parse/recommended目录,增加TestRecommendedParser.java类,内容如下:

[xhtml]  view plain copy
  1. package org.apache.nutch.parse.recommended;  
  2.   
  3.   
  4. import org.apache.nutch.metadata.Metadata;  
  5. import org.apache.nutch.parse.Parse;  
  6. import org.apache.nutch.parse.ParseResult;  
  7. import org.apache.nutch.parse.ParseUtil;  
  8. import org.apache.nutch.protocol.Content;  
  9. import org.apache.hadoop.conf.Configuration;  
  10. import org.apache.nutch.util.NutchConfiguration;  
  11.   
  12. import java.util.Properties;  
  13. import java.io.*;  
  14. import java.net.URL;  
  15.   
  16. import junit.framework.TestCase;  
  17.   
  18. /*  
  19.  * Loads test page recommended.html and verifies that the recommended   
  20.  * meta tag has recommended-content as its value.  
  21.  *  
  22.  */  
  23. public class TestRecommendedParser extends TestCase {  
  24.   
  25.   private static final File testDir =  
  26.     new File("H:/project/SearchEngine/Nutch1.2/src/plugin/recommended/data");  
  27.   
  28.   public void testPages() throws Exception {  
  29.     pageTest(new File(testDir, "recommended.html"), "http://foo.com/",  
  30.              "recommended-content");  
  31.   
  32.   }  
  33.   
  34.   
  35.   public void pageTest(File file, String url, String recommendation)  
  36.     throws Exception {  
  37.   
  38.     String contentType = "text/html";  
  39.     InputStream in = new FileInputStream(file);  
  40.       
  41.     ByteArrayOutputStream out = new ByteArrayOutputStream((int)file.length());  
  42.     byte[] buffer = new byte[1024];  
  43.     int i;  
  44.     while ((i = in.read(buffer)) != -1) {  
  45.       out.write(buffer, 0, i);  
  46.     }  
  47.     in.close();  
  48.     byte[] bytes = out.toByteArray();  
  49.     Configuration conf = NutchConfiguration.create();  
  50.   
  51.     Content content =  
  52.       new Content(url, url, bytes, contentType, new Metadata(), conf);  
  53.       
  54.     Parse parse = new ParseUtil(conf).parseByExtensionId("parse-html",content).get(content.getUrl());  
  55.       
  56.     Metadata metadata = parse.getData().getContentMeta();  
  57.     
  58.     assertEquals(recommendation, metadata.get("recommended"));  
  59.     assertTrue("somesillycontent" != metadata.get("recommended"));  
  60.   }  
  61.     
  62. }  

2.3 用junit运行TestRecommendedParser.java测试。

转自http://blog.youkuaiyun.com/laigood/article/details/5929388

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值