无法使用Java API连接到ElasticSearch服务器

时间:2013-10-24 16:00:36

标签: java elasticsearch

我正在尝试使用Java API连接到ElasticSearch服务器。我使用elasticsearch服务来启动/停止和弹性搜索头来可视化集群。集群/节点处于活动状态,REST API在9200上通过curl工作正常。我几乎阅读了关于这个主题的所有帖子,但我无法使其工作,下面是我的详细信息:

我尝试了所有这些:

  1. 在TransportClient中使用localhost,devhost1,127.0.0.1或网络上的实际IP
  2. 取消注释config.yaml中的IP属性并放入127.0.0.1(服务器)
  3. 检查端口9300是否可用,它是。
  4. ... 9200 / _cluster /节点

    {
      "ok": true,
      "cluster_name": "test",
      "nodes": {
        "NLVBbJpJTZWefeI2kQt3Tg": {
          "name": "inventory_management",
          "transport_address": "inet[/127.0.0.1:9300]",
          "hostname": "devhost1",
          "version": "0.90.5",
          "http_address": "inet[/127.0.0.1:9200]"
        }
      }
    }
    

    ... 9200 / _cluster /健康?漂亮= TRUE

    {
      "cluster_name" : "test",
      "status" : "green",
      "timed_out" : false,
      "number_of_nodes" : 1,
      "number_of_data_nodes" : 1,
      "active_primary_shards" : 0,
      "active_shards" : 0,
      "relocating_shards" : 0,
      "initializing_shards" : 0,
      "unassigned_shards" : 0
    }
    

    Java代码:

    Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "test").put("node.name", "inventory_management").build();
    Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
    IndexResponse response = client.prepareIndex("twitter", "tweet", "1").setSource(json).execute().actionGet();
    

    例外:

    org.elasticsearch.transport.NodeDisconnectedException: [][inet[localhost/127.0.0.1:9300]][/cluster/nodes/info]
    org.elasticsearch.client.transport.NoNodeAvailableException: No node available
        at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:138)
        at org.elasticsearch.client.transport.support.InternalTransportClient.index(InternalTransportClient.java:124)
        at org.elasticsearch.client.transport.TransportClient.index(TransportClient.java:242)
        at org.elasticsearch.client.action.index.IndexRequestBuilder.doExecute(IndexRequestBuilder.java:219)
        at org.elasticsearch.client.action.support.BaseRequestBuilder.execute(BaseRequestBuilder.java:52)
        at org.elasticsearch.client.action.support.BaseRequestBuilder.execute(BaseRequestBuilder.java:47)
    

    从客户端登录(Java API):

    [2013-10-24 16:37:15,783][DEBUG][threadpool.cached        ] [Aragorn] Initializing cached thread pool with keep_alive[1m], scheduled_size[20]
    [2013-10-24 16:37:15,809][DEBUG][client.transport         ] [Aragorn] node_sampler_interval[1s]
    [2013-10-24 16:37:15,820][DEBUG][netty.channel.socket.nio.NioProviderMetadata] Using the autodetected NIO constraint level: 0
    [2013-10-24 16:37:15,872][DEBUG][transport.netty          ] [Aragorn] Connected to node [[#temp#-1][inet[localhost/127.0.0.1:9300]]]
    [2013-10-24 16:37:15,892][DEBUG][transport.netty          ] [Aragorn] Disconnected from [[#temp#-1][inet[localhost/127.0.0.1:9300]]]
    [2013-10-24 16:37:15,894][DEBUG][client.transport         ] [Aragorn] Failed to get node info from [#temp#-1][inet[localhost/127.0.0.1:9300]], removed from nodes list
    org.elasticsearch.transport.NodeDisconnectedException: [][inet[localhost/127.0.0.1:9300]][/cluster/nodes/info]
    

    +++++++++++++++++++++++

    在Java API中尝试使用Unicast相同的错误。

    discovery.zen.ping.multicast.enabled: false
    discovery.zen.ping.unicast.hosts: "localhost" # Tried with devhost1 127.0.0.1 as well and ["localhost"] too
    

    这是服务器日志:

    [2013-10-25 13:49:58,379][INFO ][node                     ] [inventory_management] version[0.90.5], pid[2426], build[c8714e8/2013-09-17T12:50:20Z]
    [2013-10-25 13:49:58,379][INFO ][node                     ] [inventory_management] initializing ...
    [2013-10-25 13:49:58,382][INFO ][plugins                  ] [inventory_management] loaded [], sites []
    [2013-10-25 13:49:59,853][INFO ][node                     ] [inventory_management] initialized
    [2013-10-25 13:49:59,853][INFO ][node                     ] [inventory_management] starting ...
    [2013-10-25 13:49:59,937][INFO ][transport                ] [inventory_management] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/192.168.1.50:9300]}
    [2013-10-25 13:50:02,960][INFO ][cluster.service          ] [inventory_management] new_master [inventory_management][2-uG1xVNSSiLo5RVRrjbGg][inet[/192.168.1.50:9300]], reason: zen-disco-join (elected_as_master)
    [2013-10-25 13:50:02,972][INFO ][discovery                ] [inventory_management] test/2-uG1xVNSSiLo5RVRrjbGg
    [2013-10-25 13:50:02,989][INFO ][http                     ] [inventory_management] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/192.168.1.50:9200]}
    [2013-10-25 13:50:02,989][INFO ][node                     ] [inventory_management] started
    [2013-10-25 13:50:03,024][INFO ][gateway                  ] [inventory_management] recovered [0] indices into cluster_state
    [2013-10-25 13:50:09,399][WARN ][transport.netty          ] [inventory_management] exception caught on transport layer [[id: 0x02649775, /127.0.0.1:50028 => /127.0.0.1:9300]], closing connection
    java.io.StreamCorruptedException: invalid internal transport message format
        at org.elasticsearch.transport.netty.SizeHeaderFrameDecoder.decode(SizeHeaderFrameDecoder.java:27)
        at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:425)
        at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303)
        at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
        at org.elasticsearch.common.netty.OpenChannelsHandler.handleUpstream(OpenChannelsHandler.java:74)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
        at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:268)
        at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:255)
        at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)
        at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:109)
        at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:312)
        at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:90)
        at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
        at org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
        at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:724)
    [2013-10-25 13:50:09,403][WARN ][transport.netty          ] [inventory_management] exception caught on transport layer [[id: 0x02649775, /127.0.0.1:50028 :> /127.0.0.1:9300]], closing connection
    java.io.StreamCorruptedException: invalid internal transport message format
        at org.elasticsearch.transport.netty.SizeHeaderFrameDecoder.decode(SizeHeaderFrameDecoder.java:27)
        at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:425)
        at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.cleanup(FrameDecoder.java:482)
        at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.channelDisconnected(FrameDecoder.java:365)
        at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:102)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
        at org.elasticsearch.common.netty.OpenChannelsHandler.handleUpstream(OpenChannelsHandler.java:74)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
        at org.elasticsearch.common.netty.channel.Channels.fireChannelDisconnected(Channels.java:396)
        at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.close(AbstractNioWorker.java:361)
        at org.elasticsearch.common.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:81)
        at org.elasticsearch.common.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:36)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendDownstream(DefaultChannelPipeline.java:574)
        at org.elasticsearch.common.netty.channel.Channels.close(Channels.java:812)
        at org.elasticsearch.common.netty.channel.AbstractChannel.close(AbstractChannel.java:197)
        at org.elasticsearch.transport.netty.NettyTransport.exceptionCaught(NettyTransport.java:501)
        at org.elasticsearch.transport.netty.MessageChannelHandler.exceptionCaught(MessageChannelHandler.java:228)
        at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:112)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
        at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.exceptionCaught(FrameDecoder.java:377)
        at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:112)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
        at org.elasticsearch.common.netty.OpenChannelsHandler.handleUpstream(OpenChannelsHandler.java:74)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
        at org.elasticsearch.common.netty.channel.Channels.fireExceptionCaught(Channels.java:525)
        at org.elasticsearch.common.netty.channel.AbstractChannelSink.exceptionCaught(AbstractChannelSink.java:48)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.notifyHandlerException(DefaultChannelPipeline.java:658)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:566)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
        at org.elasticsearch.common.netty.OpenChannelsHandler.handleUpstream(OpenChannelsHandler.java:74)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
        at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
        at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:268)
        at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:255)
        at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)
        at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:109)
        at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:312)
        at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:90)
        at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
        at org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
        at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:724)
    
    +++++++++++++++++++++++
    +++++++++++++++++++++++
    

    Java版本:

    java version "1.7.0_25"
    OpenJDK Runtime Environment (IcedTea 2.3.12) (7u25-2.3.12-4ubuntu3)
    OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
    
    +++++++++++++++++++++++
    +++++++++++++++++++++++
    +++++++++++++++++++++++
    

    问题已解决如下。确保您的客户端和服务器版本同步!

4 个答案:

答案 0 :(得分:25)

对于可能遇到同样问题的其他人来说,这个问题已被标记为已回答(答案实际上在问题的最后)。

我遇到了同样的问题,结果发现Java客户端使用的JAR版本与服务器运行的版本之间存在差异。您最好的选择是确保完全匹配,然后给出的here指令不需要任何调整。

要检查的另一件事是您的Java客户端使用的是正确的端口,不是 http客户端使用的端口。正确的端口默认为9300而不是后者使用的9200。

答案 1 :(得分:11)

如果有其他人在这个帖子上遇到麻烦,我发现你可能会收到此错误的另一个原因是因为cluster_name已被更改为默认值以外的其他内容。

查看您当前的cluser_name

curl -XGET localhost:9200/_cluster/nodes?pretty=true

使用类似于OP上面的代码:

Settings settings = ImmutableSettings.settingsBuilder()
    .put("cluster.name", "test").build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));

来源:http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/client.html

答案 2 :(得分:0)

这适用于使用spring-boot spring-data elasticsearch的用户。请确保您的客户端版本与服务器相同。

<library name="Gradle: org.elasticsearch:elasticsearch:1.5.2">
  <CLASSES>
    <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.elasticsearch/elasticsearch/1.5.2/47aafc6bf8f23ed8dcbf6a1db174fb0b8e44a8db/elasticsearch-1.5.2.jar!/" />
  </CLASSES>
  <JAVADOC />
  <SOURCES>
    <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.elasticsearch/elasticsearch/1.5.2/d1e0b7b758ce7bd5d6e3757896054b09f28f372d/elasticsearch-1.5.2-sources.jar!/" />
  </SOURCES>
</library>

答案 3 :(得分:0)

首先,版本非常重要,Java API与您使用的版本密切相关,

此解决方案适用于带有java 8的elasticSearch v 5.4.1

确保您获得所有依赖项,并且子依赖项(不仅仅是elasticSearch jar),使用maven,ivy等来获取依赖项树。

例如,在常春藤中

 <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
  <dependency org="org.elasticsearch.client" name="transport" rev="5.4.1"  conf="default"/>

// --------------------- 然后在课堂上你可以打电话:

 TransportClient client;

    client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));