Spark-ClickHouse-ES实时项目第十一天-发布数据接口-总数

仓库位置

日志生成:https://github.com/SmallScorpion/gmall-mock.git
日志服务器:https://github.com/SmallScorpion/gmall-spark-ch-es-realtime.git

访问路径

在这里插入图片描述

数据格式

在这里插入图片描述

新建spring模块pom

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>

        <dependency>


            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>commons-compiler</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.4.6</version>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

controller

package com.warehouse.gmall.publisher.controller;

import com.alibaba.fastjson.JSON;
import com.warehouse.gmall.publisher.service.EsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PublisherController {

    @Autowired
    EsService esService;

    @RequestMapping(value = "realtime-total", method = RequestMethod.GET)
    public String realtimeTotal(@RequestParam("date") String dt) {

        List<Map<String, Object>> rsList = new ArrayList();

        // {"id":"dau","name":"新增日活","value":1200}
        Map<String, Object> dauMap = new HashMap();
        dauMap.put("id", "dau");
        dauMap.put("name", "新增日活");
        Long dauTotal = esService.getDauTotal(dt);
        dauMap.put("value", dauTotal);
        // 添加
        rsList.add(dauMap);
        // {"id":"new_mid","name":"新增设备","value":233}
        Map<String, Object> newMap = new HashMap();
        newMap.put("id", "new_mid");
        newMap.put("name", "新增设备");
        newMap.put("value", 222);
        // 添加
        rsList.add(newMap);

        return JSON.toJSONString( rsList );
    }


}

serivce

public interface EsService {

    /**
     * 某一天的总数
     * @param date
     * @return
     */
    public Long getDauTotal( String date );

    /**
     * 某一天的每个小时数量
     * @param date
     * @return
     */
    public Map getDauHour(String date );


}

serviceimpl

package com.warehouse.gmall.publisher.service.impl;

import com.warehouse.gmall.publisher.service.EsService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.lang.model.element.VariableElement;
import java.io.IOException;
import java.util.Map;

@Service
public class EsServiceImpl implements EsService {

    // 添加ES
    @Autowired
    JestClient jestClient;

    @Override
    public Long getDauTotal(String date) {
        // es index
        String indexName = "gmall_ch_dau_info" + date + "-query";
        // 查询语句
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query( new MatchAllQueryBuilder());

        // 构建查询
        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(indexName)
                .addType("_doc")
                .build();

        try {
            // 执行查询
            SearchResult searchResult = jestClient.execute(search);
            // 获取总数
            return searchResult.getTotal();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("es 查询异常");
        }

    }

    @Override
    public Map getDauHour(String date) {
        return null;
    }
}

application.properties

spring.elasticsearch.jest.uris=http://hadoop102:9200,http://hadoop102:9200
server.port=8070

代码结构

在这里插入图片描述

代码测试

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值