简体   繁体   中英

Elastic Search with 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 am getting following error when I run:-

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 is

<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>

Please help thanks in advance

I am guessing this could probably be because of the change in implementation at your end in terms of upgrading the version of elastic search from a version lower than 2.4 to current.

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

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

While the implementation with and before Version 2.3 was as :

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

And I am going by the definition of AbstractMethodError here

Thrown when an application tries to call an abstract method. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled.

For which I would also suggest you, to go through the mvn dependency:tree and exclude any other dependencies for the artifact org.elasticsearch.client .

9200 is the port of Elasticsearch REST API. To connect from a Java client, you should use port 9300 . I don't know why the stacktrace and error message are so unclear...

Your initialization syntax looks correct, and nothing in your pom screams JAR hell or any other conflicts, but there is one issue with how you have it:

Use the TransportClient object instead of a Transport object that you are attempting to use.

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

Transport is an Interface in the Elasticsearch repo, see here . The Transport Client depends on the rest of Elasticsearch, see here , and downloads it and adds it to your project's dependencies. What this means, is that when you say Transport it...

  1. Is not the TransportClient you're looking for.
  2. You are inadvertently referring to a Transport Interface in Elasticsearch's main code.
 //Try this method...
    TransportClient client = null;
    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;

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