2013-05-21 60 views
24

我無法通過Java API連接到vanilla ElasticSearch集羣。爲什麼我無法通過Java API連接到ElasticSearch?

重現:

#start elasticsearch 
elasticsearch -f 

#checking in a new window 
$ curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{\ 
    "user" : "kimchy",\ 
    "post_date" : "2009-11-15T14:12:12",\ 
    "message" : "trying out Elastic Search"\ 
}' 

結果:

{ 
    "ok": true, 
    "_index": "twitter", 
    "_type": "tweet", 
    "_id": "1", 
    "_version": 3 
} 

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy' 

結果:

{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 0.30685282, 
    "hits": [ 
     { 
     "_index": "twitter", 
     "_type": "tweet", 
     "_id": "1", 
     "_score": 0.30685282, 
     "_source": { 
      "user": "kimchy", 
      "post_date": "2009-11-15T14:12:12", 
      "message": "trying out Elastic Search" 
     } 
     } 
    ] 
    } 
} 

所以,一切通過HTTP工作。通過Java嘗試(每this page):

public static void main(String[] args) { 

    Client client = new TransportClient() 
    .addTransportAddress(new InetSocketTransportAddress("localhost", 9200)); 

    IndexResponse response = null; 
    try { 
     response = client.prepareIndex("twitter", "tweet", "1") 
      .setSource(XContentFactory.jsonBuilder() 
         .startObject() 
          .field("user", "john") 
          .field("postDate", new Date()) 
          .field("message", "who dont it work") 
         .endObject() 
        ) 
      .execute() 
      .actionGet(); 
    } catch (ElasticSearchException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    System.out.println(response); 
} 

而且我得到以下堆棧跟蹤:

May 21, 2013 8:27:42 AM org.elasticsearch.plugins 
INFO: [Bes] loaded [], sites [] 
May 21, 2013 8:27:49 AM org.elasticsearch.client.transport 
INFO: [Bes] failed to get node info for [#transport#-1][inet[localhost/127.0.0.1:9200]], disconnecting... 
org.elasticsearch.transport.ReceiveTimeoutTransportException: [][inet[localhost/127.0.0.1:9200]][cluster/nodes/info] request_id [0] timed out after [5002ms] 
    at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:342) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) 
    at java.lang.Thread.run(Thread.java:680) 
Exception in thread "main" org.elasticsearch.client.transport.NoNodeAvailableException: No node available 
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:202) 
    at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:106) 
    at org.elasticsearch.client.support.AbstractClient.index(AbstractClient.java:84) 
    at org.elasticsearch.client.transport.TransportClient.index(TransportClient.java:310) 
    at org.elasticsearch.action.index.IndexRequestBuilder.doExecute(IndexRequestBuilder.java:315) 
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:62) 
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:57) 
    at Scratch.main(Scratch.java:30) 

和最親密的事情,我至今對這個問題是here發現,但線程落後了沒有決議。

+0

而採用彈性搜索可以幫助@jnBrymn異常在線程我收到以下錯誤「主」 java.lang.VerifyError的:類org.elasticsearch.transport.Netty3Plugin重寫最後一個方法onModule(Lorg/elasticsearch/common/network/NetworkModule;)V –

回答

67

TransportClient默認端口是9300.您必須在Java代碼中使用它而不是9200。這可能是連接失敗的原因。

+1

而且......做到了! elasticsearch的例子有9300.他們應該改變我的猜測。 – JnBrymn

+0

@JohnBerryman你指的是什麼樣的例子? – javanna

+0

我顯然很困惑。我檢查了那個例子,實際上它是9200.所以...我想象它! – JnBrymn

0
import java.net.InetAddress; 
import java.net.UnknownHostException; 
import org.elasticsearch.action.get.GetResponse; 
import org.elasticsearch.client.transport.TransportClient; 
import org.elasticsearch.common.settings.Settings; 
import org.elasticsearch.common.transport.InetSocketTransportAddress; 
import org.elasticsearch.transport.client.PreBuiltTransportClient; 


public class ElasticsearchTest { 
    public static void main(String[] argv) throws UnknownHostException{ 

     /* //Set new cluester 
     Settings settings = Settings.builder() 
       .put("cluster.name", "newCluster") 
       .put("node.name","newNode").build();*/ 

     //create cliet !!!Make sure keep settings empty if your cluster is the 
     //same as the one you defined in your elasticsearch.yml file 
     //Plus, port here(9300)must be different from your http port(9200) 

     TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) 
       .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); 
     //get data 
     GetResponse response = client.prepareGet("twitter", "tweet", "1").execute().actionGet(); 

     //output 
     System.out.println(response.getSourceAsString()); 

     client.close(); 
     } 
    } 
+0

請向您的代碼解釋或添加一些註釋,這對學習者會更有幫助。 – HDJEMAI

相關問題