Java客户端尝试连接到Elastic Search

时间:2016-11-07 10:02:44

标签: 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;
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();
      }

  }
}

以下依赖关系:

  • 列表项

enter image description here

但是我一直低于错误:

  

com.google.common.util.concurrent.ExecutionError:java.lang.NoClassDefFoundError:org / jboss / netty / channel / socket / nio / WorkerPool       在com.google.common.cache.LocalCache $ Segment.get(LocalCache.java:2201)       在com.google.common.cache.LocalCache.get(LocalCache.java:3937)       在com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)       在com.google.common.cache.LocalCache $ LocalLoadingCache.get(LocalCache.java:4824)       在org.elasticsearch.common.inject.internal.FailableCache.get(FailableCache.java:51)       在org.elasticsearch.common.inject.ConstructorInjectorStore.get(ConstructorInjectorStore.java:51)       在org.elasticsearch.common.inject.ConstructorBindingImpl.initialize(ConstructorBindingImpl.java:50)       在org.elasticsearch.common.inject.InjectorImpl.initializeBinding(InjectorImpl.java:405)       在org.elasticsearch.common.inject.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:680)

我尝试过更改JAR文件的顺序以及不同版本的JARS降级并更改为更高版本,但提到的问题很少here,但问题未得到解决

更新后出现错误" netty"到" netty-4.0.0.Alpha8"和番石榴到" guava-20.0-hal":

  

com.google.common.util.concurrent.ExecutionError:java.lang.NoClassDefFoundError:org / jboss / netty / channel / ReceiveBufferSizePredictorFactory       在com.google.common.cache.LocalCache $ Segment.get(LocalCache.java:2212)       在com.google.common.cache.LocalCache.get(LocalCache.java:4054)       在com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4058)       在com.google.common.cache.LocalCache $ LocalLoadingCache.get(LocalCache.java:4985)       在org.elasticsearch.common.inject.internal.FailableCache.get(FailableCache.java:51)       在org.elasticsearch.common.inject.ConstructorInjectorStore.get(ConstructorInjectorStore.java:51)

1 个答案:

答案 0 :(得分:2)

我想,WorkerPool类从版本3.5开始使用netty。所以你需要将你的netty版本更新到至少3.5 +。