• 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. }

     

  • 相关阅读:
    python 数据挖掘库orange3 介绍
    在IDEA中使用.env文件配置信息
    python采集美女内容,快来学会把你喜欢的内容全部下载吧~
    01 - Linux系统概要(再论计算机系统)
    MTK平台Metadata的加载(4)—Q版本后
    暴雪网易事件大讨论:Web3游戏未来发展趋势
    【clickhouse专栏】单机版的安装与验证
    Mybatis缓存
    网络协议头分析及抓包三次挥手四次握手
    【目标检测】SPP-Net论文理解(超详细版本)
  • 原文地址:https://blog.csdn.net/Damien_J_Scott/article/details/134400339