简体   繁体   English

使用Java进行弹性搜索

[英]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 我的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>

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. 我猜这可能是因为在将弹性搜索版本从低于2.4的版本升级到当前版本时,您的实现发生了变化。

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 : 虽然2.3版本和之前的实现如下:

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 我在这里按照AbstractMethodError的定义

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 . 我也建议你,通过mvn dependency:treeexclude工件org.elasticsearch.client任何其他依赖org.elasticsearch.client

9200 is the port of Elasticsearch REST API. 9200是Elasticsearch REST API的端口。 To connect from a Java client, you should use port 9300 . 要从Java客户端连接,您应该使用端口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: 您的初始化语法看起来是正确的,并且您的pom中没有任何内容尖叫JAR地狱或任何其他冲突,但是如何拥有它有一个问题:

Use the TransportClient object instead of a Transport object that you are attempting to use. 使用TransportClient对象而不是您尝试使用的Transport对象。

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

Transport is an Interface in the Elasticsearch repo, see here . Transport是Elasticsearch仓库中的一个接口,请参见此处 The Transport Client depends on the rest of Elasticsearch, see here , and downloads it and adds it to your project's dependencies. 传输客户端依赖于Elasticsearch的其余部分,请参阅此处 ,并将其下载并将其添加到项目的依赖项中。 What this means, is that when you say Transport it... 这意味着什么,当你说Transport它...

  1. Is not the TransportClient you're looking for. 不是您正在寻找的TransportClient。
  2. You are inadvertently referring to a Transport Interface in Elasticsearch's main code. 您无意中在Elasticsearch的主代码中引用了Transport接口。
 //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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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