|
@@ -1,5 +1,6 @@
|
|
|
package flink.zanxiangnet.ad.monitoring.process;
|
|
|
|
|
|
+import flink.zanxiangnet.ad.monitoring.AdStatJob;
|
|
|
import flink.zanxiangnet.ad.monitoring.clickhouse.sink.ClickhouseUtil;
|
|
|
import flink.zanxiangnet.ad.monitoring.pojo.entity.AdStatOfHourDWD;
|
|
|
import flink.zanxiangnet.ad.monitoring.pojo.entity.AdStatOfMinuteDWD;
|
|
@@ -9,9 +10,11 @@ import flink.zanxiangnet.ad.monitoring.util.DateUtil;
|
|
|
import flink.zanxiangnet.ad.monitoring.util.JsonUtil;
|
|
|
import org.apache.flink.configuration.Configuration;
|
|
|
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
|
|
|
+import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
|
|
|
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
|
|
|
import org.apache.flink.util.Collector;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
@@ -23,17 +26,18 @@ import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.Properties;
|
|
|
|
|
|
-public class CostHourProcess extends ProcessWindowFunction<AdStatOfHourDWD, CostHourDM, Long, TimeWindow> {
|
|
|
+public class CostHourProcess extends ProcessWindowFunction<AdStatOfHourDWD, CostHourDM, Long, GlobalWindow> {
|
|
|
private static final DateTimeFormatter formatForLastReduceKey = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm");
|
|
|
private Connection connection = null;
|
|
|
- private int minutenow = 1;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public void open(Configuration conf) throws SQLException, ClassNotFoundException {
|
|
|
- ClickhouseUtil clickhouseUtil = new ClickhouseUtil();
|
|
|
- connection = ClickhouseUtil.getConn("cc-bp11803zbt0oq045io.ads.rds.aliyuncs.com",
|
|
|
+ public void open(Configuration conf) throws SQLException, ClassNotFoundException, IOException {
|
|
|
+ Properties props = new Properties();
|
|
|
+ props.load(AdStatJob.class.getResourceAsStream("/application.properties"));
|
|
|
+ connection = ClickhouseUtil.getConn(props, "cc-bp11803zbt0oq045io.ads.rds.aliyuncs.com",
|
|
|
"8123", "data_monitoring");
|
|
|
}
|
|
|
|
|
@@ -47,8 +51,8 @@ public class CostHourProcess extends ProcessWindowFunction<AdStatOfHourDWD, Cost
|
|
|
costHourDM.createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(adStatOfMinuteDWD.getCreateTime());
|
|
|
//时间-小时
|
|
|
//TODO:之后需要进一步修改
|
|
|
- String tmpHour = new SimpleDateFormat("yyyy-MM-dd HH:00:00").format(adStatOfMinuteDWD.getCreateTime());
|
|
|
- costHourDM.hour = tmpHour;
|
|
|
+ String tmpHour = adStatOfMinuteDWD.getHour() > 9 ? adStatOfMinuteDWD.getHour().toString() : "0" + adStatOfMinuteDWD.getHour().toString();
|
|
|
+ costHourDM.hour = adStatOfMinuteDWD.getStatDay() + " " + tmpHour + ":00:00";
|
|
|
//广告id
|
|
|
costHourDM.adId = adStatOfMinuteDWD.getAdId().toString();
|
|
|
//广告组id
|
|
@@ -64,7 +68,7 @@ public class CostHourProcess extends ProcessWindowFunction<AdStatOfHourDWD, Cost
|
|
|
//当天小时消耗
|
|
|
costHourDM.costHour = adStatOfMinuteDWD.getCostHour();
|
|
|
//消耗速度
|
|
|
- costHourDM.costSpeed = this.minutenow == 0 ? 0 : adStatOfMinuteDWD.getCostHour() / this.minutenow;
|
|
|
+ costHourDM.costSpeed = adStatOfMinuteDWD.getCostHour();
|
|
|
//总浏览量
|
|
|
costHourDM.viewCountTotal = adStatOfMinuteDWD.getViewCountTotal();
|
|
|
//天-总浏览量
|
|
@@ -208,25 +212,10 @@ public class CostHourProcess extends ProcessWindowFunction<AdStatOfHourDWD, Cost
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public void process(Long elementCount, ProcessWindowFunction<AdStatOfHourDWD, CostHourDM, Long, TimeWindow>.Context context,
|
|
|
+ public void process(Long elementCount, ProcessWindowFunction<AdStatOfHourDWD, CostHourDM, Long, GlobalWindow>.Context context,
|
|
|
Iterable<AdStatOfHourDWD> iterable, Collector<CostHourDM> collector) throws Exception {
|
|
|
- long beginTime = context.window().getStart();
|
|
|
- LocalDateTime beginDateTime = DateUtil.milliToLocalDateTime(beginTime);
|
|
|
- LocalDate beginDate = beginDateTime.toLocalDate();
|
|
|
- //当前天
|
|
|
- String statDay = DateUtil.formatLocalDate(beginDate);
|
|
|
- System.out.println(statDay);
|
|
|
- //当前小时
|
|
|
- int hourInt = beginDateTime.getHour();
|
|
|
- this.minutenow = beginDateTime.getMinute();
|
|
|
- String hour = beginDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
|
|
|
- String lastHour = beginDateTime.minusHours(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
|
|
|
- String lastTwoHour = beginDateTime.minusHours(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
|
|
|
- String lastThreeHour = beginDateTime.minusHours(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
|
|
|
-
|
|
|
- long now = System.currentTimeMillis();
|
|
|
-
|
|
|
|
|
|
+ System.out.println("进入get in ");
|
|
|
//获取前几分钟
|
|
|
|
|
|
|
|
@@ -235,9 +224,24 @@ public class CostHourProcess extends ProcessWindowFunction<AdStatOfHourDWD, Cost
|
|
|
for (AdStatOfHourDWD adStatOfMinuteDWD : iterable) {
|
|
|
adStatOfMinuteDWDlist.add(adStatOfMinuteDWD);
|
|
|
CostHourDM costHourDM = new CostHourDM();
|
|
|
- if (adStatOfMinuteDWD.getHour() != hourInt) {
|
|
|
- continue;
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+ //当前天
|
|
|
+ String statDay = adStatOfMinuteDWD.getStatDay();
|
|
|
+ //当前小时
|
|
|
+ int hour = adStatOfMinuteDWD.getHour();
|
|
|
+ String tmpHour = "";
|
|
|
+ int tmpHourInt = adStatOfMinuteDWD.getHour() - 1;
|
|
|
+ tmpHour = tmpHourInt > 9 ? String.valueOf(tmpHourInt) : "0" + tmpHourInt;
|
|
|
+ String lastHour = adStatOfMinuteDWD.getStatDay() + " " + tmpHour + ":00:00";
|
|
|
+
|
|
|
+ tmpHourInt = adStatOfMinuteDWD.getHour() - 2;
|
|
|
+ tmpHour = tmpHourInt > 9 ? String.valueOf(tmpHourInt) : "0" + tmpHourInt;
|
|
|
+ String lastTwoHour = adStatOfMinuteDWD.getStatDay() + " " + tmpHour + ":00:00";
|
|
|
+
|
|
|
+ tmpHourInt = adStatOfMinuteDWD.getHour() - 3;
|
|
|
+ tmpHour = tmpHourInt > 9 ? String.valueOf(tmpHourInt) : "0" + tmpHourInt;
|
|
|
+ String lastThreeHour = adStatOfMinuteDWD.getStatDay() + " " + tmpHour + ":00:00";
|
|
|
|
|
|
String adId = adStatOfMinuteDWD.getAdId().toString();
|
|
|
String sql = "select " +
|
|
@@ -261,6 +265,7 @@ public class CostHourProcess extends ProcessWindowFunction<AdStatOfHourDWD, Cost
|
|
|
CostHourDM costHourDM_new = datachange(adStatOfMinuteDWD, costHourDM);
|
|
|
|
|
|
collector.collect(costHourDM_new);
|
|
|
+ System.out.println("costhour_输入" + JsonUtil.toString(adStatOfMinuteDWD));
|
|
|
System.out.println("costhour_输出:" + JsonUtil.toString(costHourDM_new));
|
|
|
}
|
|
|
System.out.println("costhour_windowCount:" + adStatOfMinuteDWDlist.size());
|
|
@@ -269,7 +274,7 @@ public class CostHourProcess extends ProcessWindowFunction<AdStatOfHourDWD, Cost
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void clear(ProcessWindowFunction<AdStatOfHourDWD, CostHourDM, Long, TimeWindow>.Context context) throws Exception {
|
|
|
+ public void clear(ProcessWindowFunction<AdStatOfHourDWD, CostHourDM, Long, GlobalWindow>.Context context) throws Exception {
|
|
|
System.out.println("窗口关闭");
|
|
|
}
|
|
|
|