2017-05-24 2 views
0

Ich bin mit dem folgenden Testcode auf SolrCloud Solrj Bibliothek:SolrJ - NPE wenn zu SolrCloud Zugriff

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(); 
    } 
} 

Dieser Code schlägt fehl aufgrund der folgenden Ausnahme

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) 

Dies ist die vollständige Stacktrace der Ausnahme. Ich habe gerade von einer Solr-Standalone-Installation auf eine SolrCloud "aufgerüstet" (mit einer externen Instanz von Zookeeper, nicht der eingebetteten). Bei standalone Solr wurde der gleiche Code (mit nur wenigen Unterschieden, wie der Host-URL) verwendet, um perfekt zu funktionieren.

Die NPE schickt mich in die SolrJ-Bibliothek, die ich nicht kenne.

Jeder kann mir helfen zu verstehen, woher das Problem stammt und wie ich es überwinden kann? Aufgrund meiner Unerfahrenheit und der Kürze der Fehlermeldung kann ich nicht herausfinden, wo ich anfangen soll.

+0

Welche Version von SolrJ verwenden Sie? – freedev

+0

@freedev neuesten stabilen, 6.5.1 –

Antwort

0

Mit Blick auf Ihren Code, würde ich vorschlagen, die Standard-Sammlung als erste Sache zu spezifizieren.

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

In Bezug auf die NPE, die Sie erleben, ist sehr wahrscheinlich auf einen Netzwerkfehler zurückzuführen.

In diesen Zeilen Ihre Ausnahme von for-Schleife erhöht wird: 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); 
    } 
    }