其他分享
首页 > 其他分享> > 实时电商数仓(三十三)之实时计算(十二)dws层(四)订单明细实付金额分摊(三)发布数据接口

实时电商数仓(三十三)之实时计算(十二)dws层(四)订单明细实付金额分摊(三)发布数据接口

作者:互联网

发布数据接口

1  代码清单

控制层

PublisherController

实现接口的web发布

服务层

ClickhouseService

数据业务查询interface

ClickhouseServiceImpl

业务查询的实现类

数据层

OrderMapper

数据层查询的interface

OrderMapper.xml

数据层查询的实现配置

接口  

2.1 访问路径

总数

http://publisher:8070/realtime-total?date=2019-02-01

分时统计

http://publisher:8070/realtime-hour?id=order_amount&date=2019-02-01

2.2 要求数据格式

总数

[{"id":"dau","name":"新增日活","value":1200},

{"id":"new_mid","name":"新增设备","value":233 },

{"id":"order_amount","name":"新增交易额","value":1000.2 }]

分时统计

{"yesterday":{"11":383,"12":123,"17":88,"19":200 },

"today":{"12":38,"13":1233,"17":123,"19":688 }}

3 代码开发

 

 

3.1 pom.xml 

 View Code

3.2  OrderMapper

复制代码
package com.atguigu.gmall0105.publisher.mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public interface OrderWideMapper {

    //查询当日总额
    public BigDecimal selectOrderAmount(String date);

    //查询当日分时交易额
    public List<Map> selectOrderAmountHour(String date);
}
复制代码

3.3  OrderMapper.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.atguigu.gmall0105.publisher.mapper.OrderWideMapper">
    <select id="selectOrderAmount" resultType="java.math.BigDecimal">
            select  sum(final_detail_amount) order_amount
            from order_wide_0105
            where dt=#{date}
    </select>
    <select id="selectOrderAmountHour" resultMap="orderAmountHrMap">
            select toHour(create_time) hr, sum(final_detail_amount) order_amount
            from order_wide_0105
            where dt=#{date}
            group by hr
    </select>

    <resultMap id="orderAmountHrMap" type="java.util.Map" autoMapping="true">
    </resultMap>
</mapper>
复制代码

3.4 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
复制代码

3.5 增加扫描包路径

复制代码
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);
    }

}
复制代码

3.6  ClickHouseService

复制代码
package com.atguigu.gmall0105.publisher.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public interface ClickHouseService {

    public BigDecimal getOrderAmount(String date);

    public Map getOrderAmountHour(String date);


}
复制代码

3.7  ClickHouseServiceImpl

复制代码
package com.atguigu.gmall0105.publisher.service.impl;

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

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClickHouseServiceImpl implements ClickHouseService {

    @Autowired
    OrderWideMapper orderWideMapper;

    @Override
    public BigDecimal getOrderAmount(String date) {
        return orderWideMapper.selectOrderAmount(date);
    }

    @Override
    public Map getOrderAmountHour(String date) {
        //进行转换
//        List<Map>     [{"hr":10,"order_amount":1000.00} ,{"hr":11,"order_amount":2000.00}...... ]
//        Map    {"10":1000.00,"11":2000.00,...... }
        List<Map> mapList = orderWideMapper.selectOrderAmountHour(date);
        Map<String,BigDecimal> hourMap=new HashMap<>();
        for (Map map : mapList) {
            hourMap.put(  String.valueOf(map.get("hr"))  , (BigDecimal) map.get("order_amount"));
        }
        return hourMap;
    }
}
复制代码

 

3.5 PublisherController

复制代码
package com.atguigu.gmall0105.publisher.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall0105.publisher.service.ClickHouseService;
import com.atguigu.gmall0105.publisher.service.EsService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class PublisherController {

    @Autowired
    EsService esService;

    @Autowired
    ClickHouseService clickHouseService;

    @RequestMapping(value = "realtime-total",method = RequestMethod.GET)
    public String realtimeTotal(@RequestParam("date") String dt){
        List<Map<String,Object>>  rsList=new ArrayList<>();

        Map<String,Object> dauMap = new HashMap();
        dauMap.put("id","dau");
        dauMap.put("name","新增日活");
        Long dauTotal=0L;
        try {
            dauTotal = esService.getDauTotal(dt);
        }catch ( Exception e){
            e.printStackTrace();
        }
        if(dauTotal!=null){
            dauMap.put("value",dauTotal);
        }else {
            dauMap.put("value",0L);
        }

        rsList.add(dauMap);

        Map<String,Object> newMidMap = new HashMap();
        newMidMap.put("id","new_mid");
        newMidMap.put("name","新增设备");
        newMidMap.put("value",233);
        rsList.add(newMidMap);

        Map<String,Object> orderAmountMap = new HashMap();
        orderAmountMap.put("id","order_amount");
        orderAmountMap.put("name","新增交易额");
        BigDecimal orderAmount = clickHouseService.getOrderAmount(dt);
        orderAmountMap.put("value",orderAmount);
        rsList.add(orderAmountMap);

        return  JSON.toJSONString(rsList);
    }

    @GetMapping("realtime-hour")
    public String realtimeHour(@RequestParam("id") String id ,@RequestParam("date") String dt){
        if(id.equals("dau")){
            Map dauHourMapTD = esService.getDauHour(dt);
            String yd = getYd(dt);
            Map dauHourMapYD = esService.getDauHour(yd);

            Map<String,Map<String,Long>> rsMap=new HashMap<>();
            rsMap.put("yesterday",dauHourMapYD);
            rsMap.put("today",dauHourMapTD);
            return  JSON.toJSONString(rsMap);
        }else if(id.equals("order_amount")){
            Map orderAmountHourMapTD = clickHouseService.getOrderAmountHour(dt);
            String yd = getYd(dt);
            Map orderAmountHourMapYD = clickHouseService.getOrderAmountHour(yd);

            Map<String,Map<String,BigDecimal>> rsMap=new HashMap<>();
            rsMap.put("yesterday",orderAmountHourMapYD);
            rsMap.put("today",orderAmountHourMapTD);
            return  JSON.toJSONString(rsMap);
        }else{
            return  null;
        }

    }

    private  String getYd(String today){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date todayDate = dateFormat.parse(today);
            Date ydDate = DateUtils.addDays(todayDate, -1);
            return   dateFormat.format(ydDate);

        } catch (ParseException e) {
            e.printStackTrace();
            throw  new RuntimeException("日期格式不正确");
        }

    }



}
复制代码

标签:商数,dws,java,String,Map,实时,put,import,public
来源: https://www.cnblogs.com/qiu-hua/p/13668138.html