Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect java client to elasticsearch server

I'm trying to connect my java client to an elasticsearch server.

Elasticsearch 2.4.0 is installed on a distant server. To access to it, I have to use port 10700. (When I run "telnet xxxx 10700", it worked, so the port is open)

I have an error for 2 days now and I read it could be a conflict between netty4 dependencies, but I'm not able to fix it. (My netty dependencies are transitives, I don't import them by myself)

How can I resolve this problem?

The java code:

public void connexionToEs() throws UnknownHostException {
    String clusterName = "xxxx";
    String serverAddress = "xxxx";

    try{
        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(serverAddress), 10700));

        SearchResponse response = client.prepareSearch().execute().actionGet();
        String output = response.toString();
        System.out.println(output);
        client.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

The dependencies:

<!-- DEPENDENCIES FOR ELASTICSEARCH CLIENT -->
<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>5.2.0</version>
</dependency>

<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>transport</artifactId>
  <version>5.2.0</version>
</dependency>

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-web</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-to-slf4j</artifactId>
  <version>2.6.2</version>
</dependency>

The error:

Exception in thread "elasticsearch[_client_][management][T#1]" java.lang.NoSuchMethodError: io.netty.buffer.CompositeByteBuf.addComponents(ZLjava/lang/Iterable;)Lio/netty/buffer/CompositeByteBuf;
at org.elasticsearch.transport.netty4.Netty4Utils.toByteBuf(Netty4Utils.java:78)
at org.elasticsearch.transport.netty4.Netty4Transport.sendMessage(Netty4Transport.java:422)
at org.elasticsearch.transport.netty4.Netty4Transport.sendMessage(Netty4Transport.java:93)
at org.elasticsearch.transport.TcpTransport.internalSendMessage(TcpTransport.java:1058)
at org.elasticsearch.transport.TcpTransport.sendRequestToChannel(TcpTransport.java:1040)
at org.elasticsearch.transport.TcpTransport.executeHandshake(TcpTransport.java:1555)
at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:502)
at org.elasticsearch.transport.TcpTransport.connectToNode(TcpTransport.java:460)
at org.elasticsearch.transport.TransportService.connectToNode(TransportService.java:318)
at org.elasticsearch.client.transport.TransportClientNodesService$SniffNodesSampler$1.run(TransportClientNodesService.java:488)
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:527)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

The netty4 dependencies:

io.netty:netty:3.10.6.Final
io.netty:netty-buffer:4.0.28.Final
io.netty:netty-codec:4.0.28.Final
io.netty:netty-codec-http:4.1.7.Final
io.netty:netty-common:4.0.28.Final
io.netty:netty-handler:4.0.28.Final
io.netty:netty-resolver:4.1.7.Final
io.netty:netty-transport:4.0.28.Final
like image 400
Pierre-Louis Ollivier Avatar asked Sep 23 '26 08:09

Pierre-Louis Ollivier


2 Answers

I solved a similar problem, with as following:

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.3.0</version>
    <exclusions>
        <exclusion>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
        </exclusion>
    </exclusions>
</dependency>
like image 121
Casey Avatar answered Sep 25 '26 22:09

Casey


Please try using the matching Java dependency for your Elasticsearch server

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.4.0</version>
</dependency>
like image 40
OneCricketeer Avatar answered Sep 25 '26 22:09

OneCricketeer