【问题标题】:SolrJ - NPE when accessing to SolrCloudSolrJ - 访问 SolrCloud 时的 NPE
【发布时间】:2017-05-24 21:26:07
【问题描述】:

我正在使用 Solrj 库在 SolrCloud 上运行以下测试代码:

public static void main(String[] args) {
    String zkHostString = "192.168.56.99:2181";
    SolrClient solr = new CloudSolrClient.Builder().withZkHost(zkHostString).build();
    List<MyBean> beans = new ArrayList<>();
    for(int i = 0; i < 10000 ; i++) {
        // creating a bunch of MyBean to be indexed
        // and temporarily storing them in a List
        // no Solr operations performed here
    }

    System.out.println("Adding...");
    try {
        solr.addBeans("myCollection", beans);
    } catch (IOException | SolrServerException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    System.out.println("Committing...");
    try {
        solr.commit("myCollection");
    } catch (SolrServerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

由于以下异常,此代码失败

Exception in thread "main" java.lang.NullPointerException
    at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:1175)
    at org.apache.solr.client.solrj.impl.CloudSolrClient.request(CloudSolrClient.java:1057)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:160)
    at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:106)
    at org.apache.solr.client.solrj.SolrClient.addBeans(SolrClient.java:357)
    at org.apache.solr.client.solrj.SolrClient.addBeans(SolrClient.java:312)
    at com.togather.solr.testing.SolrIndexingTest.main(SolrIndexingTest.java:83)

这是异常的完整堆栈跟踪。我只是从 Solr 独立安装“升级”到 SolrCloud(使用外部 Zookeeper 单个实例,而不是嵌入式实例)。使用独立的 Solr,相同的代码(只有一些细微的差异,例如主机 URL)过去可以完美地工作。

NPE 将我发送到 SolrJ 库中,我不知道。

任何人都可以帮助我了解问题的根源以及如何克服它?由于我缺乏经验和错误信息的简洁,我无法弄清楚从哪里开始查询。

【问题讨论】:

  • 您使用的是哪个版本的 SolrJ?
  • @freedev 最新稳定版,6.5.1

标签: solrj solrcloud


【解决方案1】:

查看您的代码,我建议首先指定默认集合。

CloudSolrClient solr = new CloudSolrClient.Builder().withZkHost(zkHostString).build();
solr.setDefaultCollection("myCollection");

关于您遇到的 NPE,很可能是由于网络错误。

在这些行中,你的异常是由 for 循环引发的:for (DocCollection ext : requestedCollections)

  if (wasCommError) {
    // it was a communication error. it is likely that
    // the node to which the request to be sent is down . So , expire the state
    // so that the next attempt would fetch the fresh state
    // just re-read state for all of them, if it has not been retired
    // in retryExpiryTime time
    for (DocCollection ext : requestedCollections) {
      ExpiringCachedDocCollection cacheEntry = collectionStateCache.get(ext.getName());
      if (cacheEntry == null) continue;
      cacheEntry.maybeStale = true;
    }
    if (retryCount < MAX_STALE_RETRIES) {//if it is a communication error , we must try again
      //may be, we have a stale version of the collection state
      // and we could not get any information from the server
      //it is probably not worth trying again and again because
      // the state would not have been updated
      return requestWithRetryOnStaleState(request, retryCount + 1, collection);
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多