简体   繁体   中英

Using Elasticsearch in java through TransportClient

I am trying to use elasticsearch in java through TransportClient. I have used logstash to integrate mysql with elasticsearch. This is how i have initialized and used transport client

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

I am unable to connect to the node and i get a handshake timeout. I get the following error:

TransportClientNodesService - failed to connect to node 
[{#transport#-1}{vrvVU4MGTAC7_5NCOiBseg}{localhost}{127.0.0.1:9200}], 
ignoring...
org.elasticsearch.transport.ConnectTransportException: [][127.0.0.1:9200] handshake_timeout[30s]
at org.elasticsearch.transport.TcpTransport.executeHandshake(TcpTransport.java:1614) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:555) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:116) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.transport.TransportService.openConnection(TransportService.java:351) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler.doSample(TransportClientNodesService.java:407) [elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.client.transport.TransportClientNodesService$NodeSampler.sample(TransportClientNodesService.java:357) [elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.client.transport.TransportClientNodesService$ScheduledNodeSampler.run(TransportClientNodesService.java:390) [elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:569) [elasticsearch-5.5.2.jar:5.5.2]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_131]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_131]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]

Elasticsearch transport port is 9300 so change your client creation line by replacing 9200 with 9300 as shown below:

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

Find the related documentation here .

If you are using different cluster.name other than default( elasticsearch ) then you must set that while creating TransportClient as shown below:

Settings settings = Settings.builder()
        .put("cluster.name", "myClusterName").build();
TransportClient client = new PreBuiltTransportClient(settings);
//Add transport addresses and do something with the client...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM