`

nutch插件编写

阅读更多
说说NUTCH插件


AntluceneXMLApacheSpring

每一个基本的非范围搜索都可以由NUTCH来实现,但当我们希望它可以实现范围搜索的时候,我们就需要自己实现插件来完成这个功能。
1)我们查看插件的组织目录,发现NUTCH把很多功能都作为插件来进行插入:

2)我们可以看到conf文件夹内的nutch-default.xml文件,打开它,我们可以看到如下代码:
Xml代码 
<property> 
  <name>plugin.includes</name> 
  <value>protocol-http|urlfilter-regex|parse-(text|html|js|tika)|index-(basic|anchor|more)|query-(basic|site|url|lastModified)|response-(json|xml)|summary-basic|scoring-opic|urlnormalizer-(pass|regex|basic)</value> 
  <description>Regular expression naming plugin directory names to 
  include.  Any plugin not matching this expression is excluded. 
  In any case you need at least include the nutch-extensionpoints plugin. By 
  default Nutch includes crawling just HTML and plain text via HTTP, 
  and basic indexing and search plugins. In order to use HTTPS please enable  
  protocol-httpclient, but be aware of possible intermittent problems with the  
  underlying commons-httpclient library. Nutch now also includes integration with Tika 
  to leverage Tika's parsing capabilities for multiple content types. The existing Nutch 
  parser implementations will likely be phased out in the next release or so, as such, it is 
  a good idea to begin migrating away from anything not provided by parse-tika. 
  </description> 
</property> 
   如果我们需要添加什么插件,只需要在此处进行添加即可。
3)下面我们进行正题,如何开发NUTCH插件:
就以我开发queryLastModified这个插件来说:
新建一个类,名字随便啦,我这时就命名为LastModifiedQueryFilter,代码如下:
Java代码 
private Configuration conf; 
 
    public LastModifiedQueryFilter() { 
        super("lastmodified"); 
    } 
 
    public Configuration getConf() { 
        return this.conf; 
    } 
 
    private static final String FIELD_NAME = "lastmodified"; 
 
    // query syntax is defined as date:yyyymmdd-yyyymmdd 
    private static final Pattern pattern = Pattern 
            .compile("(\\d{8})\\s(\\d{8})$"); 
 
    public BooleanQuery filter(Query input, BooleanQuery output) 
            throws QueryException { 
        // examine each clause in the Nutch query 
        Clause[] clauses = input.getClauses(); 
 
        for (int i = 0; i < clauses.length; i++) { 
            Clause c = clauses[i]; 
 
            // skip if not date clauses 
            if (!c.getField().equals(FIELD_NAME)) 
                continue; 
 
            String x = c.toString(); 
 
            x = x.substring(x.indexOf(":")+1).replaceAll("\"",""); 
             
            Matcher matcher = pattern.matcher(x); 
            if (!matcher.matches()) { 
                throw new QueryException("Wrong query syntax " + FIELD_NAME 
                        + ":" + x); 
            } 
             
            // inclusive 
            TermRangeQuery rangeQuery = new TermRangeQuery(FIELD_NAME, matcher 
                    .group(1), matcher.group(2), true, true); 
             
             
            rangeQuery.setBoost(0.0f); // trigger filterization 
 
            output.add(rangeQuery, BooleanClause.Occur.MUST); 
 
        } 
 
        return output; 
    } 
中间那几句代码,我们可以看到它实现上是调用了lucene的TermRangeQuery。类写完后当然就是要写plugin.xml,这是插件的必备文件,它通知NUTCH加载此插件。
文件内容大概如下:
Xml代码 
<plugin 
   id="query-lastModified" 
   name="LastModified Query Filter" 
   version="1.0.0" 
   provider-name="nutch.org"> 
 
   <runtime> 
      <library name="query-lastModified.jar"> 
         <export name="*"/> 
      </library> 
   </runtime> 
 
   <requires> 
      <import plugin="nutch-extensionpoints"/> 
   </requires> 
 
   <extension id="org.apache.nutch.searcher.lastModified" 
              name="Nutch LastModified Query Filter" 
              point="org.apache.nutch.searcher.QueryFilter"> 
      <implementation id="LastModifiedQueryFilter" 
                      class="org.apache.nutch.searcher.lastModified.LastModifiedQueryFilter"> 
        <parameter name="fields" value="lastmodified"/> 
      </implementation> 
       
   </extension> 
 
</plugin> 
id那些大家用过spring那些应该都清楚的,是插件的名字。extension中的point是指插件点,是指插件继承哪个类,name只是描述而已。implementation中的id也只是名字而已,而class表明类的具体类名,要包括包名,parameter表明它是针对哪个field,这有有raw-fields和fields两种。这时我暂时没有用到raw-fields,没怎么了解,一般情况下都用fields。
4)当写完配置文件和插件文件后就需要写编译文件build.xml,很简单,只有几行,内容如下:
Xml代码 
<project name="query-lastModified" default="jar-core"> 
  <import file="../build-plugin.xml"/> 
</project> 
这里的name表明生成的jar包的名称,jar-core表明生成主要的jar包,由于ant没怎么用过,就先不研究了。
import是指把文件导入,也可以理解成在编译buil-plugin.xml时,会自动到这里进行查找,然后进行编译生成JAR包。
5)这里的配置,我们知道,肯定还要再配置build-plugin.xml。
有两个地方需要添加,<target name="deploy"> 内添加<ant dir="query-lastModified" target="deploy"/>表明它要进行部署。
另外在<target name="clean">内添加<ant dir="query-lastModified" target="clean"/>表明在部署前进行清理。
6)这些东西配置完成之后就需要更改nutch-default,把我们添加的插件配置到前面所讲的plugins.include标签内。
7)这样完成之后我们就可以用ant进行编译生成jar包和job包,拷贝过去覆盖原来的即可。
这样,我们的插件开发就完成了。
分享到:
评论

相关推荐

    Nutch中文分词插件的编写与配置

    Nutch中文分词插件的编写与配置,由于Internet的迅猛发展,使得用户查找信息犹如大海捞针,而搜索引擎则能帮用户很好的解决这个问题。 Nutch是用java语言开发的,基于Lucene的完整的网络搜索引擎,并采用插件机制进行...

    nutch 初学文档教材

    8.2 插件机制---plugin........45 8.2.1 什么是plugin......45 8.2.2 使用plugin的好处.......45 8.2.3 plugin工作原理...46 8.2.4 编写plugin47 8.3 API接口.......53 8.3.1使用Nutch API....53 8.3.2使用Open...

    Nutch中文分词插件的编写与实现.pdf

    关于nutch的搜索引擎的中文分词的研究,包括了编写与实现

    Nutch入门.rar

    8.2 插件机制---plugin........45 8.2.1 什么是plugin......45 8.2.2 使用plugin的好处.......45 8.2.3 plugin工作原理...46 8.2.4 编写plugin47 8.3 API接口.......53 8.3.1使用Nutch API....53 8.3.2使用...

    sek:一个类似 Nutch 的, 基于 Hadoop 的并行式爬虫框架

    即程序提供接口, 然后编写实现该接口的插件程序,打包成 jar 文件放在 CLASSPATH 下, 通过配置文件的配置, 即可运行插件程序中的代码.这样易于应用的拓展.目前打算将 文本解析 部分以插件的机制实现. 这样就能实现...

    网络爬虫调研报告.docx

    另外很吸引人的一点在于,它提供了一种插件框架,使得其对各种网页内容的解析、各种数据的采集、查询、集群、过滤等功能能够方便的进行扩 展,正是由于有此框架,使得 Nutch 的插件开发非常容易,第三方的插件也...

    focused-ir-tool:重点信息检索工具

    它使用 Apache Nutch (Lucene) 进行爬网,使用一个插件来强制聚焦,并使用 Apache Solr 进行索引和布尔检索。 jar_dependencies 文件夹包含编译项目所需的 JAR 文件列表。 该项目可以使用ant(1.8.0及以上版本)...

    malaga-fi-开源

    Malaga-fi 是一个 Nutch 插件,用于索引用芬兰语编写的文档。 它从形态上分析单词并仅索引基本形式(您在字典中找到的),以便您只需搜索基本形式即可找到单词的所有变化。

    xmg-gecco-demo-master.zip

    Gecco如何运行 Gecco的初始化和启动通过GeccoEngine完成,GeccoEngine主要负责初始化配置、开始请求的配置和启动爬虫运行,最基本的启动方法: GeccoEngine.create() .classpath("com.geccocrawler.ge » ...

Global site tag (gtag.js) - Google Analytics