首页 > 其他分享 >kafka监听全流程相关代码--从获取到数据到存储(彩民画像功能)

kafka监听全流程相关代码--从获取到数据到存储(彩民画像功能)

时间:2023-01-12 17:33:07浏览次数:35  
标签:String -- portraitEntity list shopId kafka int dayStr 监听

先建一个PortraitTask 
 1 @Component
 2 @Slf4j
 3 public class PortraitTask implements StatAble {
 4 
 5     private static final String LOCK_ID = "sss:portrait_lock:portrait";
 6 
 7     @Resource
 8     private PortraitAddTask portraitAddTask;
 9 
10 
11 
12     /**
13      * 定时同步任务 //TODO
14      */
15     @Scheduled(cron = "${portrait.task.task-sync}")
16     @ClusterLock(lockId = LOCK_ID)
17     public void portraitTask() {
18         try {
19             //同步当天彩民画像
20             log.debug("start portrait ,,,");
21 
22             portraitAddTask.syncPortraitTask();
23 
24         } catch (Exception e) {
25             return;
26         }
27     }
28 }

  此处为上面syncPortraitTask()方法的具体内容

  1 @Component
  2 @Slf4j
  3 public class PortraitAddTask {  7 
  8     @Resource
  9     private FeatureStatisticsMapper featureStatisticsMapper;
 10 
 11     @Resource
 12     private PortraitMapper portraitMapper;
 13 
 14     @Resource
 15     private PortraitDataService portraitDataService;
 16 
 17     public void syncPortraitTask(){
 18 
 19         log.info("enter portrait syncPortraitTask");
 20         //获取到当前时间和前一天的时间  此处代码为借鉴其他博主(将时间格式转化为需要的格式)
 21         Date dNow = new Date();   //当前时间
 22         Date dBefore = new Date();
 23         Calendar calendar = Calendar.getInstance(); //得到日历
 24         calendar.setTime(dNow);//把当前时间赋给日历
 25         calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
 26         dBefore = calendar.getTime();   //得到前一天的时间
 27         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
 28         SimpleDateFormat sdfStr=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
 29         String defaultStartDate = sdf.format(dBefore);    //格式化前一天
 30         String defaultEndDate = sdf.format(dNow); //格式化当前时间
 31         String dayStr = sdfStr.format(dNow); //格式化当前时间
 32  34         String lastDay =String.valueOf(yesterday); 37         String beginTime = defaultStartDate; 39         String endTime = defaultEndDate; 41         Date date =Date.from(yesterday.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); 43 
 44         addPortrait(beginTime,endTime,lastDay);
 45 
 46     }
 47 
 48     public void addPortrait(String beginTime, String endTime,String lastDay) {
 49     PortraitInfo portraitEntity = new PortraitInfo();
 50 
 51     SimpleDateFormat sdfStr=new SimpleDateFormat("yyyy-MM-dd");
 52     String dayStr = sdfStr.format(new Date()); //格式化当前时间 54 
 55     List<String> shopIds = portraitDataService.getShopId(); 57     for (String shopId : shopIds) {
 58         String shopName = portraitDataService.getShopNameByShopId(shopId); 60         SumMsgCustomerFlowInfo message = featureStatisticsMapper.queryOldNewBlackByShopIdAndDayStr(shopId, lastDay); 62         int orgId = portraitDataService.getOrgIdByShopId(shopId) ;
 63         FeatureStatisticsEntity list = featureStatisticsMapper.queryAgeAndGenderByShopIdAndDayStr(shopId,lastDay); 65         int total = 0;
 66         int female = 0;
 67         int male = 0;
 68         int underEighteen = 0;
 69         int eighteenToThirty = 0;
 70         int thirtyToFortyFive = 0;
 71         int fortyFiveToSixty = 0;
 72         int moreThanSixty = 0;
 73         int unknownOfAge = 0;
 74         int unknownOfGender = 0;
 75         int blackCount =0;
 76         int newCount = 0;
 77         int oldCount = 0;
 78         if (message != null) {
 80             total = message.getTotalCount();
 81             blackCount =message.getBlackCount();
 82             newCount = message.getNewCount();
 83             oldCount = message.getOldCount();
 84         }
 98         if(list != null){
 99                 female = list.getNumOfFemale();
100                 male = list.getNumOfMale();
101                 underEighteen = list.getUnderEighteen();
102                 eighteenToThirty = list.getEighteenToThirty();
103                 thirtyToFortyFive = list.getThirtyToFortyFive();
104                 fortyFiveToSixty = list.getFortyFiveToSixty();
105                 moreThanSixty = list.getMoreThanSixty();
106                 unknownOfAge = list.getUnknownOfAge();
107                 unknownOfGender = list.getUnknownOfGender();
108 
109         }
110         portraitEntity.setUnderEighteen(underEighteen);
111         portraitEntity.setEighteenToThirty(eighteenToThirty);
112         portraitEntity.setThirtyToFortyFive(thirtyToFortyFive);
113         portraitEntity.setFortyFiveToSixty(fortyFiveToSixty);
114         portraitEntity.setMoreThanSixty(moreThanSixty);
115         portraitEntity.setNumOfFemale(female);
116         portraitEntity.setNumOfMale(male);
117         portraitEntity.setUnknownOfGender(unknownOfGender);
118         portraitEntity.setUnknownOfAge(unknownOfAge);
119         portraitEntity.setTotal(total);
120         portraitEntity.setBlackCustomerNum(blackCount);
121         portraitEntity.setNewCustomerNum(newCount);
122         portraitEntity.setOldCustomerNum(oldCount);
123         portraitEntity.setShopId(shopId);
124         portraitEntity.setShopName(shopName);
125         List<String> firstPeople =portraitDataService.getFirstPeopleTime(shopId,beginTime,endTime);
126         log.info("firstPeople:"+firstPeople);
127         if(CollectionUtils.isEmpty(firstPeople)){
128             firstPeople.add("");
129         }131         List<String> lastPeople = portraitDataService.getLastPeopleTime(shopId,beginTime,endTime);133         if(CollectionUtils.isEmpty(lastPeople)){
134             lastPeople.add("");
135         }137         portraitEntity.setBeginTime(lastPeople.get(0));
138         portraitEntity.setEndTime(firstPeople.get(0));
139         portraitEntity.setDayStr(dayStr);
140         portraitEntity.setOrgId(orgId);
141         portraitEntity.setUpdateTime(endTime);143         portraitMapper.insetPortraitInfo(portraitEntity);
144         }
145     }
146 }
 1 @Repository
 2 public interface FeatureStatisticsMapper extends BaseMapper<FeatureStatisticsEntity>
 3 {20 
21     FeatureStatisticsEntity queryAgeAndGenderByShopIdAndDayStr(@Param("shopId") String shopId, @Param("dayStr") String dayStr);
22 
23     SumMsgCustomerFlowInfo queryOldNewBlackByShopIdAndDayStr(@Param("shopId") String shopId, @Param("dayStr") String dayStr);28 }
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.allcam.abshop.service.statistics.mapper.FeatureStatisticsMapper">

    <select id="queryAgeAndGenderByShopIdAndDayStr"
            resultType="com.allcam.abshop.service.statistics.entity.FeatureStatisticsEntity">
        select * from tbl_sss_passenger_flow_feature where shopId = #{shopId}
            and  dayStr =#{dayStr}
    </select>

    <select id="queryOldNewBlackByShopIdAndDayStr"
            resultType="com.allcam.abshop.service.message.model.SumMsgCustomerFlowInfo">
        select
        sum(total) totalCount,
        sum(countOfOld) oldCount,
        sum(countOfNew) newCount,
        sum(countOfBlack) blackCount
        from tbl_sss_passenger_flow_statistics where shopId = #{shopId}
            and  dayStr =#{dayStr}
    </select>
</mapper>
代码还有很多需要优化的地方,仅供学习参考

标签:String,--,portraitEntity,list,shopId,kafka,int,dayStr,监听
From: https://www.cnblogs.com/dengliming/p/17047320.html

相关文章

  • 揭开 TLS 握手的神秘面纱:它是什么以及它是如何工作的
    传输层安全性(TLS)旨在为网络通信增加安全性。就是浏览互联网时HTTP和HTTPS的区别。使用TLS为客户端和服务器增加了额外的工作,但它有其好处,包括:机密性:TLS将流量包装......
  • 如何在powershell中处理wmi查询返回的时间
    如何将wmi中返回的时间转化成可识别时间wmi中获取的时间类似于这种20230112005430.373878-000 以下代码用户返回操作系统安装时间用来做实验(gwmi-Query"SELECT*FROM......
  • 性能测试|JMeter压测结果分析
    查看结果树对​​https://ceshiren.com/t/topic/1369.json​​发起请求1、增加线程组、HTTPRequest、添加结果树,配置协议、域名、请求地址,如下图所示:请求结果如下图所示:......
  • 恒创科技:国内免备案服务器怎么选比较好?
    ​一般购买国内服务器是需要经过7-20天左右的备案时间,但有的人为了节省时间会直接租用国内的免备案服务器,也就是说不用备案的国内服务器,目前国内不需要备案的服务器仅有......
  • MyBatis(国税)
    一、MyBatis概要1.1、ORM介绍对象关系映射(ObjectRelationalMapping,简称ORM,或O/RM,或O/Rmapping),用于实现面向对象编程语言里不同类型系统的数据之间的转换。它是创建了一个......
  • JRTPLIB RTP头和扩展头代码解析
    RTP文档规范文档查阅网址​​​https://www.rfc-editor.org/rfc/rfc3550​​​​​​​https://www.rfc-editor.org/rfc/rfc5285​​对比说明   在RFC3550头扩展包含......
  • GIS杂谈:国内地图开发常用坐标系
    地图的坐标系有很多种,全球的,地方的,在postgis扩展表中,有srid标识的就有8000多个,这还不包括一些地方测绘部门的坐标系,当然有一些是保密的,外边也看不到。所以,有时去做专业地图......
  • Java.day.01
    Markdown学习标题三级标题四级标题五级标题六级标题字体Hello,World!Hello,World!Hello,World!Hello,World!引用选择狂神说Java,走向人生巅峰分割线图片......
  • 数论导论
    数论导论快速幂求\(a^b\bmodp\)的结果。我们可以构造如下算法:\(a^b=\begin{cases}(a^{\fracb2})^2&\texttt{biseven}\\a(a^{\frac{b-1}2})^2&\texttt{bisodd}......
  • 2023.1.12 有源汇上下界最大流简笔
    警示2023.1.12[模板]有源汇上下界最大流错误点:1.建边方法简便:靠近于用u,v,w建边,不要冗杂多余(9-17行)2.算法注意点:首先抽出每条边的下界(每条边边权=上界-......