2013-05-20 11 views
10

मैं लचीला खोजें में सूचकांक बनाने के लिए कोड का पालन करते थे, डिफ़ॉल्ट जावा एपीआई द्वारा लचीला खोजें में सूचकांक बनाएंजावा एपीआई

HttpURLConnection con = null; 
    try 
    { 
     String url = "http://localhost:9200/test2"; 

     URL resturl = new URL(url); 
     con = (HttpURLConnection) resturl.openConnection(); 

     con.setDoOutput(true); 
     con.setRequestMethod("PUT"); 
     BufferedReader in = null; 
     try 
     { 
      if (con.getInputStream() != null) 
      { 
       in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
      } 
     } 
     catch (IOException e) 
     { 
      if (con.getErrorStream() != null) 
      { 
       in = new BufferedReader(new InputStreamReader(con.getErrorStream())); 
      } 
     } 
     if (in == null) 
     { 
      throw new Exception("Unable to read response from server"); 
     } 
     StringBuffer decodedString = new StringBuffer(); 
     String line; 
     while ((line = in.readLine()) != null) 
     { 
      decodedString.append(line); 
     } 
     in.close(); 
     System.out.println("4"); 
     Integer responseCode = con.getResponseCode(); 
     System.out.println(responseCode); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
    finally 
    { 
     if (con != null) 
     { 
      con.disconnect(); 
     } 
    } 

REST API का उपयोग करके, मैं सूचकांक बनाने में सक्षम हूँ। डिफ़ॉल्ट रूप से जावा एपीआई, मुझे निम्नलिखित अपवाद मिल रहा है।

org.elasticsearch.client.transport.NoNodeAvailableException: No node available 
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:202) 
at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.java:85) 
at org.elasticsearch.client.support.AbstractIndicesAdminClient.create(AbstractIndicesAdminClient.java:200) 
at org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder.doExecute(CreateIndexRequestBuilder.java:206) 
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:62) 
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:57) 
at ElasticSearch.createIndex(ElasticSearch.java:121) 
at ElasticSearch.main(ElasticSearch.java:157) 

कृपया मुझे मार्गदर्शन करें जहां मैंने गलती की थी। अग्रिम

उत्तर

8

TransportClient के लिए बंदरगाह (जावा एपीआई के माध्यम से) धन्यवाद Http की तुलना में अलग डिफ़ॉल्ट रूप से है, transportClient पोर्ट 9300

+0

अपने जवाब के लिए धन्यवाद। फिर भी मुझे एक ही अपवाद मिल रहा है। –

+0

यह अभी ठीक काम करता है। मैंने एक छोटी सी गलती की क्योंकि मैंने 9300 का उपयोग किया था। लेकिन कॉन्फ़िगरेशन में मैंने पोर्ट नंबर अपडेट किया था। –

6

को देखते हुए क्या आप ग्राहक आप ऐसा करते हैं तो ऐसा करने में सक्षम नहीं होना चाहिए है कि है:

CreateIndexResponse createResponse = client.admin().indices().create(createIndexRequest("test1")).actionGet(); 
+2

मैं बस जोड़ूंगा कि 'createIndexRequest' विधि' Requests.createIndexRequest ("test1") '' –

5
प्रतिकृतियां और टुकड़े सेटिंग के साथ

:

Settings indexSettings = ImmutableSettings.settingsBuilder() 
       .put("number_of_shards", 1) 
       .put("number_of_replicas", 1) 
       .build(); 
CreateIndexRequest indexRequest = new CreateIndexRequest(index, indexSettings); 
client.admin().indices().create(indexRequest).actionGet(); 
संबंधित मुद्दे