我正在尝试连接到这样的弹性搜索,
Transport client = new PreBuiltTransportClient(Settings.EMPTY).
addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9200));
但是我跑步时遇到以下错误: -
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)
我的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>
请提前帮助
答案 0 :(得分:3)
我猜这可能是因为在将弹性搜索版本从低于2.4的版本升级到当前版本时,您的实现方式发生了变化。
使用current version实现上述内容的正确方法是 -
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9200));
Version 2.3之前和之前的实施是:
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
的定义
当应用程序尝试调用抽象方法时抛出。一般, 编译器捕获此错误; 此错误只能在运行时发生 时间,如果某个类的定义自那以后不一致改变了 当前正在执行的方法是最后编译的。
我还建议您浏览工件mvn dependency:tree
的{{1}}和exclude
任何其他依赖项。
答案 1 :(得分:1)
9200
是Elasticsearch REST API的端口。要从Java客户端进行连接,应使用端口9300
。我不知道为什么堆栈跟踪和错误消息如此不清楚......
答案 2 :(得分:0)
你的初始化语法看起来是正确的,你的pom中没有任何东西尖叫JAR地狱或任何其他冲突,但是你有如何拥有它的问题:
使用TransportClient
对象而不是您尝试使用的Transport
对象。
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY).
addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
Transport
是Elasticsearch仓库中的一个接口,请参阅here。传输客户端依赖于Elasticsearch的其余部分,请参阅here,然后下载并将其添加到项目的依赖项中。这意味着,当你说Transport
时......
Transport
接口。答案 3 :(得分:0)
//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;