大数据之实时项目 第10天 es查询分时

上篇:大数据之实时项目 第9天 es 查询总数


1、查询分时代码基本实现

在这里插入图片描述

PublisherService.java

package com.study.gmall0315publisher.service;

import java.util.Map;

public interface PublisherService {

    //查看总数接口
    public Integer getDauTotal(String date);

    //查询分时接口
    public Map getDauHourMap(String date);
}

PublishServiceimpl.java

package com.study.gmall0315publisher.service.impl;

import com.study.gmall0315.common.constant.GmallConstant;
import com.study.gmall0315publisher.service.PublisherService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PublishServiceimpl implements PublisherService {
  @Autowired
  JestClient jestClient;

  //实现查询总数接口的实现方法
    @Override
    public Integer getDauTotal(String date) {
        String query="{\n" +
                "  \"query\":{\n" +
                "    \"bool\": {\n" +
                "      \"filter\": {\n" +
                "        \"term\": {\n" +
                "          \"logDate\": \"2020-03-18\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";

         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
         BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
         boolQueryBuilder.filter(new TermQueryBuilder("logDate",date));
        searchSourceBuilder.query(boolQueryBuilder);

        System.out.println(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(GmallConstant.ES_INDEX_DAU).addType("_doc").build();
        Integer total=0;
         try {
             SearchResult searchResult = jestClient.execute(search);
             total= Math.toIntExact(searchResult.getTotal());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return total;
    }


    //实现查询分时接口的实现方法
    @Override
    public Map getDauHourMap(String date) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //过滤
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.filter(new TermQueryBuilder("logDate",date));
        searchSourceBuilder.query(boolQueryBuilder);
        //聚合
        TermsAggregationBuilder aggsBuilder = AggregationBuilders.terms("groupby_logHour").field("logHour").size(24);
        Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(GmallConstant.ES_INDEX_DAU).addType("_doc").build();

        Map dauHourMap = new HashMap();


        try {
             SearchResult searchResult = jestClient.execute(search);
             List<TermsAggregation.Entry> buckets = searchResult.getAggregations().getTermsAggregation("groupby_logHour").getBuckets();
            for (TermsAggregation.Entry bucket : buckets) {
                 String key = bucket.getKey();
                 Long count = bucket.getCount();
                 dauHourMap.put(key,count);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

PublisherController.java

package com.study.gmall0315publisher.controller;

import com.alibaba.fastjson.JSON;
import com.study.gmall0315publisher.service.PublisherService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class PublisherController {

    //查询总数
    @Autowired
    PublisherService publisherService;

    @GetMapping("realtime-total")
    public String getTotal(@RequestParam("date")String date) {
        List<Map> totalList = new ArrayList<>();
        Map dauMap = new HashMap();
        dauMap.put("id", "dau");
        dauMap.put("name", "新增日活");

        Integer dauTotal = publisherService.getDauTotal(date);
        dauMap.put("value", dauTotal);
        totalList.add(dauMap);

        Map newMidMap = new HashMap();
        newMidMap.put("id", "newMid");
        newMidMap.put("name", "新增设备");

        newMidMap.put("value", 233);
        totalList.add(newMidMap);

        return JSON.toJSONString(totalList);

    }

        //查询分时
        @GetMapping("realtime-hour")
        public String  getHourTotal(@RequestParam("id")String id,@RequestParam("date")String today) {

            if ("dau".equals(id)) {
                //今天
                Map dauHourTDMap = publisherService.getDauHourMap(today);
                //求昨天分时明细
                String yesterday = getYesterday(today);
                Map dauHourYDMap = publisherService.getDauHourMap(yesterday);

                HashMap hourMap = new HashMap();
                hourMap.put("today", dauHourTDMap);
                hourMap.put("yesterday", dauHourYDMap);
                return JSON.toJSONString(hourMap);

            }

        return null;
        }
        private String getYesterday(String today){
             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
             String yesterday="";
            try {
                 Date todayDate = simpleDateFormat.parse(today);
                 Date yesterdayDate = DateUtils.addDays(todayDate, -1);
                yesterday= simpleDateFormat.format(yesterdayDate);

            } catch (ParseException e) {
                e.printStackTrace();
            }
            return  yesterday;
        }
}

application.properties

server.port=8070

spring.elasticsearch.rest.uris=http://flink102:9200


启动程序
在这里插入图片描述
url访问
(1)查询总数
http://publisher:8070/realtime-total?date=2020-03-18
在这里插入图片描述
(2)查询分时访问
http://publisher:8070/realtime-hour?id=dau&date=2020-03-18
在这里插入图片描述
以上表明,Springboot接口通了


2、前端对接工程创建

(1)dw-chart工程创建
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
等待它下载完毕
在这里插入图片描述

(2)启动dw-chart工程的主程序类
在这里插入图片描述

url访问
http://127.0.0.1:8089
在这里插入图片描述
之后,先后执行程序:
前端主程序类—>Gmall0315PublisherApplication—>DauApp—>JsonMocker

效果图:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值