1. 程式人生 > 實用技巧 >實時電商數倉(三十五)之實時計算(十四)ads層(二)釋出介面

實時電商數倉(三十五)之實時計算(十四)ads層(二)釋出介面

釋出介面

釋出介面的目的是為視覺化工具提供資料服務。

釋出介面的地址和引數都要根據視覺化工具的要求進行設定。

後面的視覺化工具選用了阿里雲服務的DataV,由於DataV對地址沒有要求(可以自行配置),只對返回資料格式有一定要求。最好可以提前瞭解一下資料格式的要求。

或者可以不考慮介面格式,先完成service的查詢,然後再controller針對不同的格式要求在進行調整。

1 配置檔案

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.15.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.atguigu.gmall0105</groupId> <artifactId>gmall0105-publisher</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gmall0105-publisher</name> <description>Demo project for
Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.4</version> </dependency> <dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.1.55</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
View Code

application.properties

server.port=8070

spring.elasticsearch.jest.uris=http://hdp1:9200,http://hdp2:9200

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://hdp1/gmall0105_rs?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123123

#spring.datasource.driver-class-name=ru.yandex.clickhouse.ClickHouseDriver
#spring.datasource.url=jdbc:clickhouse://hdp1:8123/test0105

mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

其中mybatis.mapperLocations作用能夠讓spring容器找到mapper.xml用於和mapper介面進行配對。

2 程式碼部分

控制層

PublisherController

實現介面的web釋出

服務層

MySQLService

資料業務查詢interface

MySQLServiceImpl

業務查詢的實現類

資料層

TrademarkStatMapper

資料層查詢的interface

TrademarkStatMapper.xml

資料層查詢的實現配置,實質上是Mapper介面的“實現類”。

主程式

GmallPublisherApplication

增加掃描包

2.1 GmallPublisherApplication增加掃描包

作用:能夠讓spring容器找到mapper介面用於和mapper.xml進行配對

package com.atguigu.gmall0105.publisher;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.atguigu.gmall0105.publisher.mapper")
public class Gmall0105PublisherApplication {

    public static void main(String[] args) {
        SpringApplication.run(Gmall0105PublisherApplication.class, args);
    }

}

2.2 controller

package com.atguigu.gmall0105.publisher.controller;


import com.alibaba.fastjson.JSON;
import com.atguigu.gmall0105.publisher.CustomerInfo;
import com.atguigu.gmall0105.publisher.service.MySQLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
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 DataVController {

    @Autowired
    MySQLService mySQLService;

    @RequestMapping("trademarkStat")
    public String trademarkStat(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime, @RequestParam("topn") int topn){
        List<Map> mapList = mySQLService.getTrademarkStat(startTime, endTime, topn);
        List<Map> datavFormatList=new ArrayList<>();
        for (Map map : mapList) {
            Map dataVmap=new HashMap();
            dataVmap.put("x",map.get("trademark_name"));
            dataVmap.put("y",map.get("order_amount"));
            dataVmap.put("s","1");
            datavFormatList.add(dataVmap);
        }


        return JSON.toJSONString(datavFormatList);

    }
}

2.3 service

package com.atguigu.gmall0105.publisher.service;

import java.util.List;
import java.util.Map;

public interface MySQLService {
    //1 省市地區
    //2  品牌
    public List<Map> getTrademarkStat(String startTime , String endTime , int topn);

    //3 年齡段
    // 4 性別
    // 5 spu
    // 6 品類

}

2.4 service層實現類

package com.atguigu.gmall0105.publisher.service.impl;

import com.atguigu.gmall0105.publisher.mapper.TrademarkStatMapper;
import com.atguigu.gmall0105.publisher.service.MySQLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MySQLServiceImpl implements MySQLService {


    @Autowired
    TrademarkStatMapper trademarkStatMapper;

    @Override
    public List<Map> getTrademarkStat(String startTime, String endTime, int topn) {
        return trademarkStatMapper.selectTrademarkSum(  startTime,   endTime,   topn);
    }
}

2.5資料層mapper

package com.atguigu.gmall0105.publisher.mapper;

import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface TrademarkStatMapper {


    public List<Map> selectTrademarkSum(@Param("startTime") String startTime , @Param("endTime")String endTime ,@Param("topn") int topn);
}

2.6資料層實現配置

<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.atguigu.gmall0105.publisher.mapper.TrademarkStatMapper">
    <!-- 1傳參  2 返回值 -->
    <select id="selectTrademarkSum" resultMap="trademarkRsMap">
        SELECT trademark_id , trademark_name ,SUM(amount) order_amount
        FROM  trademark_amount_stat
        WHERE stat_time &gt; #{startTime} AND stat_time  &lt;#{endTime}
        GROUP BY trademark_id , trademark_name
        ORDER BY order_amount DESC
        LIMIT #{topn}
    </select>

    <resultMap id="trademarkRsMap" type="java.util.Map" autoMapping="true"></resultMap>
</mapper>