• Java Elasticsearch 按一定时间间隔(timeInterval)循环查询数据


    最近有个需求,前端传入时间间隔,去elasticsearch按照时间间隔统计每个时间间隔内数据量。

    1. public List> getCount(@RequestParam Integer time, @RequestParam String selectedDatedTime) {
    2. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    3. format.setTimeZone(TimeZone.getTimeZone(ZoneId.of(DateUtil.TIMEZONE_ZONE_ID)));
    4. Date date = CommonUtil.getDateByString(selectedDatedTime);
    5. Calendar fromDate = Calendar.getInstance();
    6. fromDate.setTime(date);
    7. fromDate.add(Calendar.HOUR_OF_DAY, -time);
    8. Calendar toDate = Calendar.getInstance();
    9. toDate.setTime(date);
    10. RangeQueryBuilder timeRangeQuery = QueryBuilders.rangeQuery(ApplicationConstant.TIMESTAMP)
    11. // .timeZone("Asia/Singapore")
    12. .gte(fromDate.getTimeInMillis())
    13. .lt(toDate.getTimeInMillis());
    14. String application = "";
    15. if (applications != null && applications.size() > 0){
    16. application = applications.get(0);
    17. }
    18. IndexCoordinates index = IndexCoordinates.of("xxxxxxx");
    19. DateHistogramInterval timeInterval = null;
    20. if(time==1){
    21. timeInterval= DateHistogramInterval.minutes(5);
    22. }else if(time==24 || time==6 ||time==12){
    23. timeInterval = DateHistogramInterval.hours(1);
    24. }else{
    25. timeInterval= DateHistogramInterval.hours(12);
    26. }
    27. Query sq = new NativeSearchQueryBuilder()
    28. .withQuery(timeRangeQuery)
    29. .addAggregation(AggregationBuilders.dateHistogram("date_histogram")
    30. .field(TIMESTAMP_FIELD_NAME)
    31. .fixedInterval(timeInterval)
    32. .minDocCount(0)
    33. .timeZone(ZoneId.of(DateUtil.TIMEZONE_ZONE_ID))
    34. .extendedBounds(new ExtendedBounds(fromDate.getTimeInMillis(), toDate.getTimeInMillis()))
    35. )
    36. .withPageable(Pageable.unpaged())
    37. .build();
    38. return esservice.getCountApi(sq, index);
    39. }

    这里面的 timeInterval 就是设定间隔时间。

    加入 extendedBounds 目的就是防止出现0数据不会返回,例如我只有8am到12am内有数据,现在是12am,timeInterval是一小时,总共时间跨度是12小时。如果不设置extendedBounds的话,date_histogram查询出来的聚合只会有8am-9am,9am-10am,10am-11am,11am-12am这几个的聚合,不会有8am之前的聚合出现,照理来说,会出现12个聚合,不管有没有数据都有聚合返回,只不过某些聚合出来docCount是0而已。所以需要加上这个条件
    因为前端ui需要进行展示,就算没有数据也需要展示。例如这个chart的前半段,虽然聚合出来没有数据,但是也需要展示0数据。

     

     

    这边是处理数据的service

    1. public List> getCountApi(Query sq, IndexCoordinates esindex) {
    2. HashMap data = new HashMap<>();
    3. List> list = new ArrayList<>();
    4. SearchHits result = template.search(sq, HashMap.class, esindex);
    5. Aggregations agg = result.getAggregations();
    6. if (agg != null) {
    7. ParsedDateHistogram histogram = agg.get("date_histogram");
    8. List> innerlist = new ArrayList<>();
    9. for (Histogram.Bucket timebucket : histogram.getBuckets()) {
    10. HashMap tempMap = new HashMap<>();
    11. ZonedDateTime zdt = (ZonedDateTime) timebucket.getKey();
    12. DateTime dt = new DateTime(zdt.toEpochSecond() * 1000L, DateTimeZone.forID(DateUtil.TIMEZONE_ZONE_ID));
    13. // String dateStr = dt.toString("yyyy-MM-dd HH:mm:ss");
    14. tempMap.put("x", dt.getMillis());
    15. tempMap.put("y", timebucket.getDocCount());
    16. innerlist.add(tempMap);
    17. }
    18. data.put("data", innerlist);
    19. data.put("name", NAME_OF_TYPE);
    20. list.add(data);
    21. }
    22. return list;
    23. }

     

  • 相关阅读:
    (转载)基于量子遗传算法的函数寻优算法
    es6---es6新增的Bigint、Symbol数据类型,set、map数据结构
    为什么同样是测试,他能年薪50W?
    数字孪生技术:智慧运维的未来之路
    springboot实验室自主预约系统毕业设计源码111953
    【问题解决】我遇到并解决PlatformIO无法使用的各种问题汇总及解决方法,简单粗暴使用的网络问题解决方法...
    aix360-gec
    创业公司的十种死法
    Node.js躬行记(23)——Worker threads
    SpringBoot中使用Redisson分布式锁的应用场景-多线程、服务、节点秒杀/抢票处理
  • 原文地址:https://blog.csdn.net/Damien_J_Scott/article/details/134400339