Java客户端尝试连接到弹性搜索时出错
使用以下代码,我正在尝试连接并索引到弹性搜索:Java客户端尝试连接到弹性搜索时出错,java,
elasticsearch,netty,guava,guice,Java,
elasticsearch,Netty,Guava,Guice,使用以下代码,我正在尝试连接并索引到弹性搜索: package elasticSearchTest; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import java.net.InetAddress; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client;
package elasticSearchTest;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.net.InetAddress;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.testng.annotations.Test;
public class ES_Test_Class {
@Test
public void f() {
try{
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", "18/May/2011:01:48:10")
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
// Document ID (generated or not)
String _id = response.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
System.out.println("Document id is: "+_id);
System.out.println("Document version is: "+_version);
}
catch (Exception e){
e.printStackTrace();
}
}
}
具有以下依赖项:
- 列表项
我猜
WorkerPool
类自3.5版以来就与netty混合在一起了。因此,您需要将netty版本更新到至少3.5+。将netty更新到4+以下的版本。版本3.9之后不存在出错的类。当然,将尝试使用3.8获取更一般的注释错误NoClassDefFoundError指定在类路径中找不到相应的类。从包名中,您可以找到相关的库,在这种情况下,它显然很麻烦。通过谷歌搜索,不难发现哪些版本包含缺少的类。希望这将有助于您下次再次遇到这样的情况。