Java APIを使用してElasticSearchサーバーに接続しようとしています。 Elasticsearchサービスを使用して、クラスターを視覚化するためにelasticsearchヘッドを開始/停止します。クラスター/ノードはアクティブです。REST APIは9200のcurlを介して正常に動作します。このトピックに関するほとんどすべての投稿を読んでいますが、機能しません。以下に詳細を示します。
これらすべてを試しました:
... 9200/_cluster/nodes
{
"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/health?pretty = 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でユニキャストと同じエラーを試みました。
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)
+++++++++++++++++++++++
+++++++++++++++++++++++
+++++++++++++++++++++++
問題は以下のように解決されました。クライアントとサーバーのバージョンが同期していることを確認してください!
同様に、同じ問題が発生する可能性のある他の人のために、この質問に回答済みのマークが付けられています(回答は実際には質問の最後にあります)。
私は同じ問題にぶつかり、Javaクライアントとサーバーが実行していたバージョンによって使用されるJARのバージョンの間の不一致であることが判明しました。一致してから、指定された指示 here は調整なしで機能します。
もう1つ確認するのは、Javaクライアントが正しいポートを使用していることです。これは、nothttpによって使用されるポートです。クライアント:正しいポートは、9200ではなく9300にデフォルト設定されています。
他の誰かがこのスレッドでつまずいた場合、このエラーを受け取る可能性のあるもう1つの理由は、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
最初に、バージョンは非常に重要です。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));
これは、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>