使用Jest操作ElasticSearch 报错:No time zone indicator问题的解决方案
问题描述: 在ElasticSearch中的时间格式是 yyyy-MM-dd'T'HH:mm:ss 的格式的,但是在使用Jest查询结果时 执行jestClient.execute(search)
,会提示错误:
com.google.gson.JsonSyntaxException: 2018-09-30T16:07:53 ... Caused by: java.text.ParseException: Failed to parse date ["2018-09-30T16:07:53"]: No time zone indicator at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274) ~[gson-2.8.5.jar:na] at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:149) ~[gson-2.8.5.jar:na] ... 48 common frames omitted
可以看到错误时Gson的错误,我们数据的格式和Jest去查询的格式不相符所致
看源码在io.searchbox.client.AbstractJestClient#ELASTIC_SEARCH_DATE_FORMAT
中定义了 public static final String ELASTIC_SEARCH_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
与我们的数据格式不相符,因此需要去定义Gson的格式
解决方案: 将JestClient自己实现并加入spring的管理中,问题得到解决
/** * @author qi.liu * @create 2018-10-18 15:14 * @desc 描述: **/ @Configuration public class JestClientConfig { @Autowired(required = false) private JestClientFactory jestClientFactory; @Autowired private ElasticsearchJestProperties properties; @Bean public JestClient createJestClient() { GsonFactoryBean gsonFactoryBean = new GsonFactoryBean(); gsonFactoryBean.setDateFormatPattern("yyyy-MM-dd'T'HH:mm:ss"); gsonFactoryBean.afterPropertiesSet(); HttpClientConfig.Builder builder = new HttpClientConfig.Builder(properties.getUri()).gson(gsonFactoryBean.getObject()) .maxTotalConnection(properties.getMaxTotalConnection()) .defaultMaxTotalConnectionPerRoute(properties.getDefaultMaxTotalConnectionPerRoute()) .readTimeout(properties.getReadTimeout()) .multiThreaded(properties.getMultiThreaded()); JestClientFactory factory = jestClientFactory != null ? jestClientFactory : new JestClientFactory(); factory.setHttpClientConfig(builder.build()); return factory.getObject(); } }
时间被正确解析出来:
相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29