couldn't resolve error: java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW- elasticsearch java client api

couldn't resolve error: java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW- elasticsearch java client api

我正在尝试使用 elaticsearch 6.2.4 及其 java rest 客户端 API 在我的 maven web 应用程序上实现自动完成功能,该应用程序 运行ning 在 glassfish 4.1 下 我面临以下错误:

java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW

我已经阅读了一些相关内容,并基本上对项目依赖项(在 pom.xml 内)进行了相应的更改,因为我遇到的所有响应都在 Jackson 库版本上发生了冲突。

这是我尝试使用的代码 运行:

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("localhost", 9200, "http")
                ));

         SearchRequest searchRequest = new SearchRequest( "trustiser_suggest" );
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String[] includeFields = new String[] {"member_mname","member_pic","member_fname","member_lname","topic_label"};
        String[] excludeFields = null;
        searchSourceBuilder.fetchSource(includeFields, excludeFields);
 Map<String, List<? extends ToXContent>> contextMap = new HashMap<>();
    contextMap.put("account_state", Collections.singletonList(CategoryQueryContext.builder().setCategory("active").build()));
        SuggestionBuilder termSuggestionBuilder = SuggestBuilders.completionSuggestion("suggest_member" )
                .prefix( str )
                .contexts(contextMap);
        SuggestBuilder suggestBuilder = new SuggestBuilder();
        suggestBuilder.addSuggestion( "suggest-mem", termSuggestionBuilder );

         SuggestionBuilder termSuggestionBuilder1 = SuggestBuilders.completionSuggestion("suggest_topic" )
                .prefix( str )
                .skipDuplicates(true);
        suggestBuilder.addSuggestion( "suggest-top", termSuggestionBuilder1 );
        searchSourceBuilder.suggest( suggestBuilder);


        searchRequest.source( searchSourceBuilder );
        SearchResponse searchResponse = null;

            searchResponse = client.search( searchRequest );

        System.out.println("AutoCompleteMemberTopicccccccccccccccc: "+searchResponse.toString());

我现在的 pom.xml:

 <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.10</version>

</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.2.4</version>

</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>6.2.4</version>
</dependency>


        <dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

    </dependencies>

和错误:

java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW
    at org.elasticsearch.common.xcontent.json.JsonXContent.<clinit>(JsonXContent.java:57)
    at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:121)
    at org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder.contexts(CompletionSuggestionBuilder.java:203)
    at com.trustiser.business.ElasticSearchDao1.AutoCompleteMemberTopic(ElasticSearchDao1.java:50)
    at com.trustiser.service.SearchEngineService.searchMemberCategory(SearchEngineService.java:33)
    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:498)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:151)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)

最后是web项目的maven树

--- maven-dependency-plugin:2.8:tree (default-cli) @ elasticmavenprojet ---
com.trustiser:elasticmavenprojet:war:1.0-SNAPSHOT
+- javax:javaee-web-api:jar:7.0:provided
+- com.fasterxml.jackson.core:jackson-core:jar:2.8.10:compile
+- org.elasticsearch:elasticsearch:jar:6.2.4:compile
|  +- org.elasticsearch:elasticsearch-core:jar:6.2.4:compile
|  +- org.apache.lucene:lucene-core:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-analyzers-common:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-backward-codecs:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-grouping:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-highlighter:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-join:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-memory:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-misc:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-queries:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-queryparser:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-sandbox:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-spatial:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-spatial-extras:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-spatial3d:jar:7.2.1:compile
|  +- org.apache.lucene:lucene-suggest:jar:7.2.1:compile
|  +- org.elasticsearch:securesm:jar:1.2:compile
|  +- org.elasticsearch:elasticsearch-cli:jar:6.2.4:compile
|  |  \- net.sf.jopt-simple:jopt-simple:jar:5.0.2:compile
|  +- com.carrotsearch:hppc:jar:0.7.1:compile
|  +- joda-time:joda-time:jar:2.9.9:compile
|  +- org.yaml:snakeyaml:jar:1.17:compile
|  +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.8.10:compile
|  +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.8.10:compile
|  +- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.8.10:compile
|  +- com.tdunning:t-digest:jar:3.0:compile
|  +- org.hdrhistogram:HdrHistogram:jar:2.1.9:compile
|  +- org.apache.logging.log4j:log4j-api:jar:2.9.1:compile
|  \- org.elasticsearch:jna:jar:4.5.1:compile
+- org.elasticsearch.client:elasticsearch-rest-client:jar:6.2.4:compile
|  +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile
|  +- org.apache.httpcomponents:httpcore:jar:4.4.5:compile
|  +- org.apache.httpcomponents:httpasyncclient:jar:4.1.2:compile
|  +- org.apache.httpcomponents:httpcore-nio:jar:4.4.5:compile
|  +- commons-codec:commons-codec:jar:1.10:compile
|  \- commons-logging:commons-logging:jar:1.1.3:compile
+- org.elasticsearch.client:elasticsearch-rest-high-level-client:jar:6.2.4:compile
|  +- org.elasticsearch.plugin:parent-join-client:jar:6.2.4:compile
|  |  +- org.locationtech.spatial4j:spatial4j:jar:0.6:compile
|  |  +- com.vividsolutions:jts:jar:1.13:compile
|  |  \- org.apache.logging.log4j:log4j-core:jar:2.9.1:compile
|  +- org.elasticsearch.plugin:aggs-matrix-stats-client:jar:6.2.4:compile
|  \- org.elasticsearch.plugin:rank-eval-client:jar:6.2.4:compile
\- com.googlecode.json-simple:json-simple:jar:1.1.1:compile
   \- junit:junit:jar:4.10:compile
      \- org.hamcrest:hamcrest-core:jar:1.1:compile

谢谢 :-)

你是对的,这是 elasticsearch 用于 JSON 序列化和反序列化的 Jackson 库抛出的错误消息,并且是由于 Jackson 库的版本冲突引起的。

我可以看到您通过在 pom.xml.

中定义以下依赖项来明确包含 Jackson 库
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.10</version>
</dependency>

通过查看您的 mvn tree,我发现 elasticsearch 也带来了一些 Jackson 库。您可以尝试以下 2 个选项吗?

选项 1. 删除我刚才提到的显式 Jackson 依赖项,并进行干净的构建。 (请注意,这不太可能奏效:),但尝试没有坏处。

选项 2。我查看了我的项目,我正在使用 elasticsearch 7.1 并使用以下 Jackson 核心库。 com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile 如果您无法确定哪个是 compatible jackson core version for your es version,那么您可以升级到 elasticsearch 7.1 并使用我提供的 Jackson 版本,它应该可以工作。

经过几天的工作和研究,我终于找到了解决问题的方法。 该问题是由 ES 高级客户端 api 的依赖项与 glassfish 模块(Jackson-core 是其中之一)上存在的依赖项之间的版本冲突引起的。 因为替换 glassfish 模块目录的 jar 是一种冒险的做法,所以我应用的解决方案是遮蔽 ES 依赖项并使用 maven 遮蔽插件重命名那里的包。 以下是实施解决方案的步骤:

  1. 创建ES的阴影依赖:
    • 创建一个javamaven项目;
    • 项目Pom.xml:
 <modelVersion>4.0.0</modelVersion>
    <groupId>com.trustiser</groupId>
    <artifactId>elasticshade</artifactId>
    <version>6.2.4</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.2.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>

                            <relocations>
                                <relocation>
                                    <pattern>org.elasticsearch</pattern>
                                    <shadedPattern>hidden.org.elasticsearch</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>org.apache.lucene</pattern>
                                    <shadedPattern>hidden.org.apache.lucene</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>com.fasterxml</pattern>
                                    <shadedPattern>hidden.com.fasterxml</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>org.apache.httpcomponents</pattern>
                                    <shadedPattern>hidden.org.apache.httpcomponents</shadedPattern>
                                </relocation>

                                <relocation>
                                    <pattern>org.hdrhistogram</pattern>
                                    <shadedPattern>hidden.org.hdrhistogram</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>org.slf4j</pattern>
                                    <shadedPattern>hidden.org.slf4j</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>org.yaml</pattern>
                                    <shadedPattern>hidden.org.yaml</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>net.sf.jopt-simple</pattern>
                                    <shadedPattern>hidden.net.sf.jopt-simple</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>joda-time</pattern>
                                    <shadedPattern>hidden.joda-time</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>commons-logging</pattern>
                                    <shadedPattern>hidden.commons-logging</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>commons-codec</pattern>
                                    <shadedPattern>hidden.commons-codec</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>com.tdunning</pattern>
                                    <shadedPattern>hidden.com.tdunning</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>com.github.spullara</pattern>
                                    <shadedPattern>hidden.com.github.spullara</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>com.carrotsearch</pattern>
                                    <shadedPattern>hidden.com.carrotsearch</shadedPattern>
                                </relocation>
                            </relocations>
                            <shadedArtifactAttached>false</shadedArtifactAttached>
                            <artifactSet>
                                <includes>
                                    <include>*:*</include>
                                </includes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>                        


                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.PluginXmlResourceTransformer"/>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.trustiser.elasticshade.App</mainClass>
                                </transformer>

                            </transformers>




                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>
  • 运行 清理、构建和 maven 包目标;
  • jar 将在 "elastic shade project location"\target

    上创建
    1. 添加对maven本地仓库的阴影依赖:

      • 从cmd访问m2仓库,默认位置是C:\Users\"user".m2\repository,执行以下命令安装创建的依赖:mvn install:install-file -Dfile="location of the shaded elastic dependency".jar -DgroupId="shaded dependency groupe id" -DartifactId="shaded dependency acrifact id" -Dversion="version of the shaded dependency" -Dpackaging=jar
    2. 添加 elasticsearch 对我的 maven pom.xml web 应用程序 pom.xml 的阴影依赖,而不是直接使用 elasticsearch 高级 rest api

    3. 为了避免 log4j 错误,我在 java 网络应用程序的资源目录中添加了一个 log4j2.properties 文件:
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %marker%m%n

rootLogger.level = info
rootLogger.appenderRef.console.ref = console