在Nutch源代码中启动Solr索引

时间:2013-08-08 01:51:24

标签: java solr nutch

我试图将我的Nutch抓取索引到solr中,但是在源代码内部,而不是命令行。

我创建了以下功能

public static int runInjectSolr(String[] args, Properties prop) throws Exception{       
    String solrUrl = "http://ec2-X-X-X-X.compute-1.amazonaws.com/solr/collection1";

    String crawldb = JobBase.getParam(args,"crawldb", null, true);
    String segments = JobBase.getParam(args,"segments", null, true);
    String args2[] = {crawldb, segments};

    Configuration conf = new Configuration();
    conf.set("-D solr.server.url",solrUrl);
    int code = ToolRunner.run(NutchConfiguration.create(),
            new IndexingJob(conf), args2);
    return code;
}

但我收到以下错误:

2013-08-07 19:37:13,338 ERROR org.apache.nutch.indexwriter.solr.SolrIndexWriter (main): Missing SOLR URL. Should be set via -D solr.server.url 
SOLRIndexWriter
solr.server.url : URL of the SOLR instance (mandatory)
solr.commit.size : buffer size when sending to SOLR (default 1000)
solr.mapping.file : name of the mapping file for fields (default solrindex-mapping.xml)
solr.auth : use authentication (default false)
solr.auth.username : use authentication (default false)
solr.auth : username for authentication
solr.auth.password : password for authentication

所以我假设我没有正确创建配置。有什么建议吗?

或者我应该以不同的方式传递我的配置字段?也许不使用

NutchConfiguration.create()

1 个答案:

答案 0 :(得分:1)

您的代码中存在两个问题:

  1. solr.server.url必须直接在配置对象中设置,而不是使用-D选项。 nutch给出的消息假设从命令行运行,这里有误导性。
  2. 正如您所提到的,您正在传递两个不同的配置实例。 NutchConfiguration.create()在内部创建一个hadoop配置,它会为它添加一些nutch特定资源,因此您不需要自己创建它。此外,ToolRunner将conf对象传递给IndexingJob,因此您无需通过其构造函数传递它。
  3. 所以正确的代码是:

    Configuration conf = NutchConfiguration.create();
    conf.set("solr.server.url", solrUrl);
    ToolRunner.run(conf, new IndexingJob(), args2);