With below code am trying to connect and index to Elastic Search:
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();
}
}
}
With below dependencies:
However I keep getting below error:
com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: org/jboss/netty/channel/socket/nio/WorkerPool at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201) at com.google.common.cache.LocalCache.get(LocalCache.java:3937) at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941) at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824) at org.elasticsearch.common.inject.internal.FailableCache.get(FailableCache.java:51) at org.elasticsearch.common.inject.ConstructorInjectorStore.get(ConstructorInjectorStore.java:51) at org.elasticsearch.common.inject.ConstructorBindingImpl.initialize(ConstructorBindingImpl.java:50) at org.elasticsearch.common.inject.InjectorImpl.initializeBinding(InjectorImpl.java:405) at org.elasticsearch.common.inject.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:680)
I have tried by changing the order of the JAR files and different versions of JARS lowering and changing to higher version by few of the suggestions as mentioned here but issue is not resolved
Error after updating "netty" to "netty-4.0.0.Alpha8" and guava to "guava-20.0-hal":
com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: org/jboss/netty/channel/ReceiveBufferSizePredictorFactory at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2212) at com.google.common.cache.LocalCache.get(LocalCache.java:4054) at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4058) at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4985) at org.elasticsearch.common.inject.internal.FailableCache.get(FailableCache.java:51) at org.elasticsearch.common.inject.ConstructorInjectorStore.get(ConstructorInjectorStore.java:51)
The WorkerPool
class is comming with netty since version 3.5
I guess. So you need to update your netty version to at least 3.5+.