Pierre-Louis Ollivier
Pierre-Louis Ollivier

Reputation: 43

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

Upvotes: 3

Views: 2353

Answers (5)

Swapan Shaw
Swapan Shaw

Reputation: 233

Exclude these netty-all dependency. This will fix your issue.

compile.exclude group: "io.netty", module:"netty-all"
compile.exclude group: "org.jboss.netty", module:"netty"

Upvotes: 0

Casey
Casey

Reputation: 56

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>

Upvotes: 2

SebM
SebM

Reputation: 51

Use the right Elasticsearch dependency version - the same as on your server.

<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>2.4.0</version>
</dependency>

Also, 2.x doesn't require the elasticsearch transport dependency. You can remove

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

Finally, 2.x does not use the PreBuiltTransportClient. See the documentation here.

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 192013

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>

Upvotes: 1

M. le Rutte
M. le Rutte

Reputation: 3563

The error is at the top of the stacktrace: Exception in thread "elasticsearch[client][management][T#1]" java.lang.NoSuchMethodError: io.netty.buffer.CompositeByteBuf.addComponents(ZLjava/lang/Iterable;)Lio/netty/buffer/CompositeByteBuf;

Your code is calling the addComponents(...) method of the io.netty.buffer.ComposityByteBuff class, but at runtime this method is not available.

Very likely this is due to a different compilation and runtime environment. The versions of the libraries you use to compile your source and the versions of the libraries that are used at runtime are probably not the same.

Upvotes: 0

Related Questions