使用Java Api访问弹性搜索

时间:2017-05-23 08:02:20

标签: java elasticsearch kibana

我正在尝试使用Elastic Search的文档来使用它的Java Api来获取和发布数据。但我经常遇到错误。

这是我现在尝试过的代码。

func setDefaultsFromSettingsBundle() {
    //Read PreferenceSpecifiers from Root.plist in Settings.Bundle
    if let settingsURL = Bundle.main.url(forResource: "Root", withExtension: "plist", subdirectory: "Settings.bundle"),
        let settingsPlist = NSDictionary(contentsOf: settingsURL),
        let preferences = settingsPlist["PreferenceSpecifiers"] as? [NSDictionary] {

        for prefSpecification in preferences {

            if let key = prefSpecification["Key"] as? String, let value = prefSpecification["DefaultValue"] {

                //If key doesn't exists in userDefaults then register it, else keep original value
                if UserDefaults.standard.value(forKey: key) == nil {

                    UserDefaults.standard.set(value, forKey: key)
                    NSLog("registerDefaultsFromSettingsBundle: Set following to UserDefaults - (key: \(key), value: \(value), type: \(type(of: value)))")
                }
            }
        }
    } else {
        NSLog("registerDefaultsFromSettingsBundle: Could not find Settings.bundle")
    }
}

堆栈跟踪

  

线程“main”中的异常java.lang.NoClassDefFoundError:   COM /谷歌/普通/收集/ ImmutableSortedMap         在com.c1x.entity.Test.main(Test.java:20)       引起: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个

请帮助,我是弹性搜索的新手,弹性搜索中存在数据,因为我在kibana中使用开发工具时可以看到它

添加google commons后

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();

添加lucene jar后

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

另一个错误

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)

1 个答案:

答案 0 :(得分:1)

您错过了类路径中的google-collections.jar。添加它,错误就会消失。

如果您使用maven作为依赖关系管理工具,请将以下行添加到您的pom.xml:

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

缺少下一个jar lucene-core-<version>.jar

maven依赖是:

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