Sandbox + ES 改造踩坑记

本人Java水平有限,此文仅记录一个Java菜鸟在使用ES时碰到的坑。

ES使用代码本身就不介绍了,只说调试完成,可以使用后,运用到SandBox时碰到的问题。

ClassNotFoundException: org.elasticsearch.common.xcontent.XContentType

这个问题比较奇怪,因为在使用ES之前,我建了一个测试项目,在里面做测试时,同样的代码可以正常的取到存储的数据,放到SandBox之后,才出现这个问题。

找到开发大佬一起查看问题时发现,ES的transport模块,它依赖的ES版本是2.4.5,和我目前使用的7.9.0相距甚远,两者之间产生了冲突,在没有手动配置依赖解决冲突之前,会默认走到2.4.5版本,而2.4.5版本确实是没有这个类的,于是报出了这个错误。

最后的解决方案也比较简单,只需要在pom.xml中,将所有的ES依赖都排除掉,然后重新依赖一遍7.9.0版本的ES即可。


    com.alibaba.jvm.sandbox
    repeater-console-service
    1.0.0-SNAPSHOT
    
        
            org.elasticsearch
            elasticsearch
        
    


    org.elasticsearch
    elasticsearch
    7.9.0

java.lang.BootstrapMethodError: call site initialization exception
java.lang.invoke.LambdaConversionException: Invalid receiver type interface org.apache.http.Header; not a subtype of implementation type interface org.apache.http.NameValuePair

这两个错误指向同一个问题,并且经过一番谷歌,最后发现和第一个问题其实是非常类似的,也是包冲突了,所以解决方案也是完全一样。


    com.alibaba.jvm.sandbox
    repeater-console-service
    1.0.0-SNAPSHOT
    
        
            org.elasticsearch
            elasticsearch
        
        
            org.apache.httpcomponents
            httpclient
        
        
            org.apache.httpcomponents
            httpcore
        
    


    org.elasticsearch
    elasticsearch
    7.9.0


    org.apache.httpcomponents
    httpclient
    4.5.13


    org.apache.httpcomponents
    httpcore
    4.4.15

最终把所有httpclient和httpcore的包都排除,并引入最新的依赖。

Elasticsearch exception [type=mapper_parsing_exception, reason=failed to parse field [gmt_create] of type [date] in document with id 'TjjVP4ABr2KZ7MY07d1W'. Preview of field's value: 'Tue Apr 19 11:19:29 CST 2022']

阅读错误信息可以知道,这是因为处理字段时,date类型的字段处理失败了。我最初以为改为String即可,处理成String后发现依旧提示我是date类型,这就很奇怪了。

谷歌了一下这段错误,在一个帖子里找到了一段解释:

The [date parser 31](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html) defaults to strict_date_optional_time or epoch_millis.

strict_date_optional_time is "A generic ISO datetime parser, where the date must include the year at a minimum, and the time (separated by T), is optional. Examples: yyyy-MM-dd'T'HH:mm:ss.SSSZ or yyyy-MM-dd."

So you can use mutate+gsub to change the " " to "T" in "2021-03-17 10:27:00.115", or else use a date filter to parse the field and overwrite it, in which case the elasticsearch output will send it to elasticsearch in an appropriate format.

这里说了一下,date默认的是strict_date_optional_time,这个类型的数据需要将日期和时间之间的空格,用T代替。

按照这个说法尝试了一下,最终解决了这个问题。

至此,Sandbox记录的数据已经可以正常的存储到ES之中。

你可能感兴趣的:(Sandbox + ES 改造踩坑记)