Elastic Search Using Java

I am trying to connect to Elastic Search like this,

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

But I get the following error on startup: -

Exception in thread "main" java.lang.AbstractMethodError: io.netty.util.concurrent.MultithreadEventExecutorGroup.newChild(Ljava/util/concurrent/Executor;[Ljava/lang/Object;)Lio/netty/util/concurrent/EventExecutor;
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:84)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:58)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:47)
at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:49)

My pom.xml

<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.2.1</version>
</dependency>
 <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.6.2</version>
</dependency>

Thank you in advance

+4
source share
4 answers

I assume that this is probably due to a change in the implementation from your point of view in terms of updating the elastic search version from version below 2.4 to the current one.

The correct way to implement the above with the current version would be -

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

While the implementation from and to Version 2.3 was like this:

Client client = TransportClient.builder().build()
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));

And I get to the definition of AbstractMethodError here

, . , ; - .

mvn dependency:tree exclude org.elasticsearch.client.

+3

9200 - API REST API Elasticsearch. Java-, 9300. , stacktrace ...

+1

, pom JAR - , , :

TransportClient Transport, .

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

Transport - Elasticsearch, . . Elasticsearch, . . , Transport ...

  • , .
  • Transport Elasticsearch.
0
 //Try this method...

    public Client getConnection(){
    if (client == null) {
                Settings settings = Settings.builder().put("cluster.name", 
           "elasticsearch").put("client.transport.sniff", true).build();
    try {
        // preparing client object...
    client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300));
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }

            }

            return client;
0

Source: https://habr.com/ru/post/1671496/


All Articles