ElasticSearch 5.0 客户端访问常见问题

0 概述

本文主要阐述 ElasticSearch 5.0 客户端模式进行开发的时候出现的问题~依赖 maven pom

      <dependency>
            <groupId>org.elasticsearch.clientgroupId>
            <artifactId>transportartifactId>
            <version>5.0.0version>
      dependency>

1 常见问题以及处理

1.1 jdk版本问题

出现unsorportedClassVersion等类似问题。由于elasticsearch 5.0 以及以后版本 使用了java lambda表达式等相JDK相关特性,因此jdk 版本必须是1.8 以上版本(含1.8)

1.2 出现log4j相关异常

    at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:105)
    at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:72)
    at org.elasticsearch.common.component.AbstractComponent.(AbstractComponent.java:37)
    at org.elasticsearch.plugins.PluginsService.(PluginsService.java:110)
    at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:92)
    at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:117)
    at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:241)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:92)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:81)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:71)
    at com.hsc.study.ESClient.(ESClient.java:21)
    at com.hsc.study.ESClient.main(ESClient.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

项目中缺少log4j2 依赖,这个是es5.0 强依赖~
解决办法:添加如下依赖

       <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-apiartifactId>
            <version>2.6.2version>
        dependency>
        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-coreartifactId>
            <version>2.6.2version>
        dependency>

然后再添加log4j2.xml 配置文件 (具体可以参考网上)

1.3 修改配置中集群名字后启动失败

Exception in thread "main" ElasticsearchParseException[malformed, expected settings to start with 'object', instead was [VALUE_STRING]]
    at org.elasticsearch.common.settings.loader.XContentSettingsLoader.load(XContentSettingsLoader.java:73)
    at org.elasticsearch.common.settings.loader.XContentSettingsLoader.load(XContentSettingsLoader.java:52)
    at org.elasticsearch.common.settings.loader.YamlSettingsLoader.load(YamlSettingsLoader.java:50)
    at org.elasticsearch.common.settings.Settings$Builder.loadFromStream(Settings.java:925)
    at org.elasticsearch.common.settings.Settings$Builder.loadFromPath(Settings.java:914)
    at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:102)
    at org.elasticsearch.bootstrap.Bootstrap.initialEnvironment(Bootstrap.java:257)
    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:297)
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121)
    at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112)
    at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54)
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122)
    at org.elasticsearch.cli.Command.main(Command.java:88)
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:89)
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:82)

yml的配置文件中 冒号后边要跟有 一个空格,不要出现tab符,如下:
cluster.name: test

你可能感兴趣的:(ElasticSearch)