为什么我无法通过Java API连接到ElasticSearch? [英] Why can't I connect to ElasticSearch through Java API?

查看:158
本文介绍了为什么我无法通过Java API连接到ElasticSearch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过Java API连接到香草ElasticSearch群集。



要复制:

  #start elasticsearch 
elasticsearch -f

#在新窗口中检查
$ curl -XPUT'http:// localhost:9200 / twitter / tweet / 1'-d'{\
user:kimchy,\
post_date:2009-11-15T14:12:12,\
message:尝试弹性搜索\
}'

结果:

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




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

结果:

  {
take:2,
timed_out:false,
_shards:{
total:5,
success:5,
failed:0
},
点击:{
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:尝试弹性搜索
}
}
]
}
}

所以,一切都通过HTTP工作。通过Java尝试(根据此页面):

  public static void main(String [] args){

客户端客户端=新的TransportClient()
.addTransportAddress(new InetSocketTransportAddress(localhost,9200));

IndexResponse response = null;
尝试{
response = client.prepareIndex(twitter,tweet,1)
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field(user,john)
.field(postDate,new Date())
.field(message,谁不工作)
.endObject()

.execute()
.actionGet();
} catch(ElasticSearchException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}

System.out.println(response);
}

我得到以下堆栈跟踪:

  2013年5月21日8:27:42 org.elasticsearch.plugins 
INFO:[Bes] loaded [],sites []
2013年5月21日8:27:49 AM org.elasticsearch.client.transport
INFO:[Bes]无法获取[#transport#-1]的节点信息[inet [localhost / 127.0.0.1:9200 ]],断开...
org.elasticsearch.transport.ReceiveTimeoutTransportException:[] [inet [localhost / 127.0.0.1:9200]] [cluster / nodes / info] request_id [0]在[5002ms]之后超时
在org.elasticsearch.transport.TransportService $ TimeoutHandler.run(TransportService.java:342)
在java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(ThreadPoolExecutor.java:895)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:918)
在java.lang.Thread.run(Thread.java:680)
线程mainorg中的异常。 elasticsearch.client.transport.NoNodeAvailableException:无节点
在org.ela sticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:202)
在org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:106)
在org.elasticsearch。 client.support.AbstractClient.index(AbstractClient.java:84)
在org.elasticsearch.client.transport.TransportClient.index(TransportClient.java:310)
在org.elasticsearch.action.index。 IndexRequestBuilder.doExecute(IndexRequestBuilder.java:315)
在org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:62)
在org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java: 57)
在Scratch.main(Scratch.java:30)

最近的事情我迄今为止发现的这个问题是 here ,但线程落后没有解决方案。

解决方案

TransportClient默认端口为9300.您必须在Java代码中使用它而不是9200。这可能是连接失败的原因。


I'm having trouble connecting to vanilla ElasticSearch cluster via the Java API.

To reproduce:

#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"\
}'

result:

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


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

result:

{
  "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"
        }
      }
    ]
  }
}

So, everything works via HTTP. Trying via Java (per 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);
}

And I get the following stack trace:

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)

And closest thing I've found so far to this problem is here, but the thread trailed off without resolution.

解决方案

The TransportClient default port is 9300. You have to use it instead of 9200 in your Java code. This is probably why the connection fails.

这篇关于为什么我无法通过Java API连接到ElasticSearch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆