在Nutch源代码中启动Solr索引



我正试图将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

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

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

NutchConfiguration.create()

您的代码中有两个问题:

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

所以正确的代码是:

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

最新更新