2017-05-23 98 views
0

我正在嘗試使用Elastic Search的文檔來從它的java Api中獲取和發佈數據。但我不斷收到錯誤。使用Java Api訪問彈性搜索

這是我現在試過的代碼。

Settings settings = Settings.builder().put("twitter", "tweet").build(); 
TransportClient client = new PreBuiltTransportClient(settings) 
       .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300)) 
       .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300)); 

GetResponse response = client.prepareGet("twitter", "tweet", "1").get(); 

堆棧跟蹤

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableSortedMap at com.c1x.entity.Test.main(Test.java:20) Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableSortedMap at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more

請幫幫忙,我是新來的彈性搜索,該數據存在於彈性搜索,因爲我可以看到它時,我在kibana使用開發工具

添加谷歌共享後

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/util/SetOnce 
    at org.elasticsearch.common.settings.Settings$Builder.<init>(Settings.java:676) 
    at org.elasticsearch.common.settings.Settings$Builder.<clinit>(Settings.java:671) 
    at org.elasticsearch.common.settings.Settings.<clinit>(Settings.java:84) 
    at com.c1x.entity.Test.main(Test.java:20) 
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.util.SetOnce 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 4 more 

添加lucene jar後

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.of(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet; 
    at org.elasticsearch.common.Strings.<clinit>(Strings.java:459) 
    at org.elasticsearch.common.settings.Settings.<init>(Settings.java:90) 
    at org.elasticsearch.common.settings.Settings$Builder.build(Settings.java:1215) 
    at com.c1x.entity.Test.main(Test.java:21) 

另一個錯誤

Exception in thread "main" java.lang.VerifyError: class org.elasticsearch.transport.Netty3Plugin overrides final method onModule.(Lorg/elasticsearch/common/network/NetworkModule;)V 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) 
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<clinit>(PreBuiltTransportClient.java:84) 
    at com.c1x.entity.Test.main(Test.java:22) 

回答

1

你在你的classpath錯過google-collections.jar。添加它,錯誤將消失。

如果您正在使用Maven作爲依賴關係管理工具,該follwing線添加到您的pom.xml:

<dependency> 
    <groupId>com.google.collections</groupId> 
    <artifactId>google-collections</artifactId> 
    <version>1.0</version> 
</dependency> 

下一頁罐子缺少添加lucene-core-<version>.jar

Maven的依賴是:

<dependency> 
    <groupId>org.apache.lucene</groupId> 
    <artifactId>lucene-core</artifactId> 
    <version>6.5.1</version> 
</dependency> 
+0

抱歉再次打擾你@Jens,我是新的彈性搜索再次與代碼的一些問題,請檢查更新的問題。 – INFOSYS

+0

@INFOSYS看起來像'elasticsearch'和'google-collections'的不兼容版本 – Jens

+0

我如何檢查我的彈性搜索版本是5.4.0,並且google收藏與您提供的相同 – INFOSYS