elasticsearch做统计分析接口请求数据,UTC时间导致的8小时无数据解决方案

这些天想收集下ES按每天每小时,服务器接口请求量的统计。方法凌晨0点至8点,无数据。

查了下,发现是UTC问题。折腾了大半天,发现不管怎么转换时间,0~8点的数据始终无数据。如下图:

elasticsearch做统计分析接口请求数据,UTC时间导致的8小时无数据解决方案_第1张图片

后来发现,index索引也是按天来的。所以要取今日凌晨0点至8点,每小时数据。

则必须根据今日凌晨的时间转换成UTC时间或者时间戳,到昨日的index索引里取。

因为国内的时区是+8的,所以比UTC时间快8小时,而索引是按UTC时间计算存储的。

由于我用的ES和logstash版本比较新,都是6.6。没有找到有时间改源码按国内时区存数据的方法。

只能通过做日期切割来计算获取数据。也还好,就是麻烦了一点点。

        
            org.elasticsearch.client
            transport
            6.6.0
        

简单参考代码:

 QueryBuilder hourBuilder = QueryBuilders.rangeQuery("@timestamp").gte(startL).lt(endL);
        TransportClient client = EsClientUtil.getInstance();
        SearchResponse rsp = client.prepareSearch(index1, index2).setSearchType(SearchType.DEFAULT).setQuery(hourBuilder).setSize(1).get();
        long count = rsp.getHits().getTotalHits();

index1表示昨日的索引名,index2表示今天的索引名

日志输出:

ElasticsearchApiImpl.0pm count>>>>>>>>>322625
ElasticsearchApiImpl.yesterday count>>>>>>>>>21497297

 

你可能感兴趣的:(elasticsearch,java,spring)