|
@@ -0,0 +1,243 @@
|
|
|
+package flink.zanxiangnet.ad.monitoring.clickhouse.sink;
|
|
|
+
|
|
|
+import flink.zanxiangnet.ad.monitoring.pojo.entity.CostHourDM;
|
|
|
+import org.apache.flink.configuration.Configuration;
|
|
|
+import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.Statement;
|
|
|
+
|
|
|
+public class BatchSinkHour extends RichSinkFunction<CostHourDM> {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(BatchSinkHour.class);
|
|
|
+
|
|
|
+ // 对象锁,防止MaxCompute的 Tunnel对象多次初始化
|
|
|
+ private static final Object DUMMY_LOCK = new Object();
|
|
|
+
|
|
|
+ private String sql;
|
|
|
+ private Connection connection = null;
|
|
|
+
|
|
|
+ public BatchSinkHour() {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void open(Configuration config) throws Exception {
|
|
|
+ super.open(config);
|
|
|
+ connection = ClickhouseUtil.getConn("cc-bp11803zbt0oq045io.ads.rds.aliyuncs.com",
|
|
|
+ "8123", "data_monitoring");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void invoke(CostHourDM costhour, Context context) throws SQLException {
|
|
|
+ String sql = "insert into data_monitoring.cost_hour " +
|
|
|
+ "(dt,\n" +
|
|
|
+ "create_time,\n" +
|
|
|
+ "hour,\n" +
|
|
|
+ "ad_id,\n" +
|
|
|
+ "adgroup_id,\n" +
|
|
|
+ "adcreative_id,\n" +
|
|
|
+ "account_id,\n" +
|
|
|
+ "campaign_id,\n" +
|
|
|
+ "cost_total,\n" +
|
|
|
+ "cost_day,\n" +
|
|
|
+ "cost_hour,\n" +
|
|
|
+ "cost_diff,\n" +
|
|
|
+ "cost_last_hour,\n" +
|
|
|
+ "cost_last_hour_diff,\n" +
|
|
|
+ "cost_last_two_hour,\n" +
|
|
|
+ "cost_last_two_hour_diff,\n" +
|
|
|
+ "cost_last_three_trend,\n" +
|
|
|
+ "cost_speed,\n" +
|
|
|
+ "view_count_total,\n" +
|
|
|
+ "view_count_day,\n" +
|
|
|
+ "view_count_hour,\n" +
|
|
|
+ "thousand_display_price_all,\n" +
|
|
|
+ "thousand_display_price_day,\n" +
|
|
|
+ "thousand_display_price_hour,\n" +
|
|
|
+ "valid_click_count_total,\n" +
|
|
|
+ "valid_click_count_day,\n" +
|
|
|
+ "valid_click_count_hour,\n" +
|
|
|
+ "ctr_all,\n" +
|
|
|
+ "ctr_day,\n" +
|
|
|
+ "ctr_hour,\n" +
|
|
|
+ "cpc_all,\n" +
|
|
|
+ "cpc_day,\n" +
|
|
|
+ "cpc_hour,\n" +
|
|
|
+ "conversions_count_total,\n" +
|
|
|
+ "conversions_count_day,\n" +
|
|
|
+ "conversions_count_hour,\n" +
|
|
|
+ "conversions_cost_total,\n" +
|
|
|
+ "conversions_cost_day,\n" +
|
|
|
+ "conversions_cost_hour,\n" +
|
|
|
+ "conversions_rate_all,\n" +
|
|
|
+ "conversions_rate_day,\n" +
|
|
|
+ "conversions_rate_hour,\n" +
|
|
|
+ "first_day_order_roi_total,\n" +
|
|
|
+ "first_day_order_roi_day,\n" +
|
|
|
+ "first_day_order_roi_hour,\n" +
|
|
|
+ "first_day_order_amount_total,\n" +
|
|
|
+ "first_day_order_amount_day,\n" +
|
|
|
+ "first_day_order_amount_hour,\n" +
|
|
|
+ "first_day_order_count_total,\n" +
|
|
|
+ "first_day_order_count_day,\n" +
|
|
|
+ "first_day_order_count_hour,\n" +
|
|
|
+ "web_order_amount_total,\n" +
|
|
|
+ "web_order_amount_day,\n" +
|
|
|
+ "web_order_amount_hour,\n" +
|
|
|
+ "web_order_cost_total,\n" +
|
|
|
+ "web_order_cost_day,\n" +
|
|
|
+ "web_order_cost_hour,\n" +
|
|
|
+ "web_order_rate_total,\n" +
|
|
|
+ "web_order_rate_day,\n" +
|
|
|
+ "web_order_rate_hour,\n" +
|
|
|
+ "web_order_count_total,\n" +
|
|
|
+ "web_order_count_day,\n" +
|
|
|
+ "web_order_count_hour,\n" +
|
|
|
+ "order_roi_total,\n" +
|
|
|
+ "order_roi_day,\n" +
|
|
|
+ "order_roi_hour,\n" +
|
|
|
+ "order_unit_price_total,\n" +
|
|
|
+ "order_unit_price_day,\n" +
|
|
|
+ "order_unit_price_hour,\n" +
|
|
|
+ "from_follow_uv_total,\n" +
|
|
|
+ "from_follow_uv_day,\n" +
|
|
|
+ "from_follow_uv_hour,\n" +
|
|
|
+ "from_follow_cost_total,\n" +
|
|
|
+ "from_follow_cost_day,\n" +
|
|
|
+ "from_follow_cost_hour,\n" +
|
|
|
+ "from_follow_rate_total,\n" +
|
|
|
+ "from_follow_rate_day,\n" +
|
|
|
+ "from_follow_rate_hour,\n" +
|
|
|
+ "web_register_count_total,\n" +
|
|
|
+ "web_register_count_day,\n" +
|
|
|
+ "web_register_count_hour,\n" +
|
|
|
+ "web_register_uv_total,\n" +
|
|
|
+ "web_register_uv_day,\n" +
|
|
|
+ "web_register_uv_hour,\n" +
|
|
|
+ "web_register_cost_total,\n" +
|
|
|
+ "web_register_cost_day,\n" +
|
|
|
+ "web_register_cost_hour) values " +
|
|
|
+ "(?,?,?,?,?,?,?,?,?,?,\n" +
|
|
|
+ "?,?,?,?,?,?,?,?,?,?,\n" +
|
|
|
+ "?,?,?,?,?,?,?,?,?,?,\n" +
|
|
|
+ "?,?,?,?,?,?,?,?,?,?,\n" +
|
|
|
+ "?,?,?,?,?,?,?,?,?,?,\n" +
|
|
|
+ "?,?,?,?,?,?,?,?,?,?,\n" +
|
|
|
+ "?,?,?,?,?,?,?,?,?,?,\n" +
|
|
|
+ "?,?,?,?,?,?,?,?,?,?,\n" +
|
|
|
+ "?,?,?,?,?,?,?)";
|
|
|
+ System.out.println(costhour);
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+ preparedStatement.setString(1, costhour.dt);
|
|
|
+ preparedStatement.setString(2, costhour.createTime);
|
|
|
+ preparedStatement.setString(3, costhour.hour);
|
|
|
+ preparedStatement.setString(4, costhour.adId);
|
|
|
+ preparedStatement.setString(5, costhour.adgroupId);
|
|
|
+ preparedStatement.setString(6, costhour.adcreativeId);
|
|
|
+ preparedStatement.setString(7, costhour.accountId);
|
|
|
+ preparedStatement.setString(8, costhour.campaignId);
|
|
|
+ preparedStatement.setLong(9, costhour.costTotal);
|
|
|
+ preparedStatement.setLong(10, costhour.costDay);
|
|
|
+ preparedStatement.setLong(11, costhour.costHour);
|
|
|
+ preparedStatement.setLong(12, costhour.costDiff);
|
|
|
+ preparedStatement.setLong(13, costhour.costLastHour);
|
|
|
+ preparedStatement.setLong(14, costhour.costLastHourDiff);
|
|
|
+ preparedStatement.setLong(15, costhour.costLastTwoHour);
|
|
|
+ preparedStatement.setLong(16, costhour.costLastTwoHourDiff);
|
|
|
+ preparedStatement.setLong(17, costhour.costLastThreeTrend);
|
|
|
+ preparedStatement.setDouble(18, costhour.costSpeed);
|
|
|
+ preparedStatement.setLong(19, costhour.viewCountTotal);
|
|
|
+ preparedStatement.setLong(20, costhour.viewCountDay);
|
|
|
+ preparedStatement.setLong(21, costhour.viewCountHour);
|
|
|
+ preparedStatement.setLong(22, costhour.thousandDisplayPriceAll);
|
|
|
+ preparedStatement.setLong(23, costhour.thousandDisplayPriceDay);
|
|
|
+ preparedStatement.setLong(24, costhour.thousandDisplayPriceHour);
|
|
|
+ preparedStatement.setLong(25, costhour.validClickCountTotal);
|
|
|
+ preparedStatement.setLong(26, costhour.validClickCountDay);
|
|
|
+ preparedStatement.setLong(27, costhour.validClickCountHour);
|
|
|
+ preparedStatement.setDouble(28, costhour.ctrAll);
|
|
|
+ preparedStatement.setDouble(29, costhour.ctrDay);
|
|
|
+ preparedStatement.setDouble(30, costhour.ctrHour);
|
|
|
+ preparedStatement.setLong(31, costhour.cpcAll);
|
|
|
+ preparedStatement.setLong(32, costhour.cpcDay);
|
|
|
+ preparedStatement.setLong(33, costhour.cpcHour);
|
|
|
+ preparedStatement.setLong(34, costhour.conversionsCountTotal);
|
|
|
+ preparedStatement.setLong(35, costhour.conversionsCountDay);
|
|
|
+ preparedStatement.setLong(36, costhour.conversionsCountHour);
|
|
|
+ preparedStatement.setLong(37, costhour.conversionsCostTotal);
|
|
|
+ preparedStatement.setLong(38, costhour.conversionsCostDay);
|
|
|
+ preparedStatement.setLong(39, costhour.conversionsCostHour);
|
|
|
+ preparedStatement.setDouble(40, costhour.conversionsRateAll);
|
|
|
+ preparedStatement.setDouble(41, costhour.conversionsRateDay);
|
|
|
+ preparedStatement.setDouble(42, costhour.conversionsRateHour);
|
|
|
+ preparedStatement.setDouble(43, costhour.firstDayOrderRoiTotal);
|
|
|
+ preparedStatement.setDouble(44, costhour.firstDayOrderRoiDay);
|
|
|
+ preparedStatement.setDouble(45, costhour.firstDayOrderRoiHour);
|
|
|
+ preparedStatement.setLong(46, costhour.firstDayOrderAmountTotal);
|
|
|
+ preparedStatement.setLong(47, costhour.firstDayOrderAmountDay);
|
|
|
+ preparedStatement.setLong(48, costhour.firstDayOrderAmountHour);
|
|
|
+ preparedStatement.setLong(49, costhour.firstDayOrderCountTotal);
|
|
|
+ preparedStatement.setLong(50, costhour.firstDayOrderCountDay);
|
|
|
+ preparedStatement.setLong(51, costhour.firstDayOrderCountHour);
|
|
|
+ preparedStatement.setLong(52, costhour.webOrderAmountTotal);
|
|
|
+ preparedStatement.setLong(53, costhour.webOrderAmountDay);
|
|
|
+ preparedStatement.setLong(54, costhour.webOrderAmountHour);
|
|
|
+ preparedStatement.setLong(55, costhour.webOrderCostTotal);
|
|
|
+ preparedStatement.setLong(56, costhour.webOrderCostDay);
|
|
|
+ preparedStatement.setLong(57, costhour.webOrderCostHour);
|
|
|
+ preparedStatement.setDouble(58, costhour.webOrderRateTotal);
|
|
|
+ preparedStatement.setDouble(59, costhour.webOrderRateDay);
|
|
|
+ preparedStatement.setDouble(60, costhour.webOrderRateHour);
|
|
|
+ preparedStatement.setLong(61, costhour.webOrderCountTotal);
|
|
|
+ preparedStatement.setLong(62, costhour.webOrderCountDay);
|
|
|
+ preparedStatement.setLong(63, costhour.webOrderCountHour);
|
|
|
+ preparedStatement.setDouble(64, costhour.orderRoiTotal);
|
|
|
+ preparedStatement.setDouble(65, costhour.orderRoiDay);
|
|
|
+ preparedStatement.setDouble(66, costhour.orderRoiHour);
|
|
|
+ preparedStatement.setLong(67, costhour.orderUnitPriceTotal);
|
|
|
+ preparedStatement.setLong(68, costhour.orderUnitPriceDay);
|
|
|
+ preparedStatement.setLong(69, costhour.orderUnitPriceHour);
|
|
|
+ preparedStatement.setLong(70, costhour.fromFollowUvTotal);
|
|
|
+ preparedStatement.setLong(71, costhour.fromFollowUvDay);
|
|
|
+ preparedStatement.setLong(72, costhour.fromFollowUvHour);
|
|
|
+ preparedStatement.setLong(73, costhour.fromFollowCostTotal);
|
|
|
+ preparedStatement.setLong(74, costhour.fromFollowCostDay);
|
|
|
+ preparedStatement.setLong(75, costhour.fromFollowCostHour);
|
|
|
+ preparedStatement.setDouble(76, costhour.fromFollowRateTotal);
|
|
|
+ preparedStatement.setDouble(77, costhour.fromFollowRateDay);
|
|
|
+ preparedStatement.setDouble(78, costhour.fromFollowRateHour);
|
|
|
+ preparedStatement.setLong(79, costhour.webRegisterCountTotal);
|
|
|
+ preparedStatement.setLong(80, costhour.webRegisterCountDay);
|
|
|
+ preparedStatement.setLong(81, costhour.webRegisterCountHour);
|
|
|
+ preparedStatement.setLong(82, costhour.webRegisterUvTotal);
|
|
|
+ preparedStatement.setLong(83, costhour.webRegisterUvDay);
|
|
|
+ preparedStatement.setLong(84, costhour.webRegisterUvHour);
|
|
|
+ preparedStatement.setDouble(85, costhour.webRegisterCostTotal);
|
|
|
+ preparedStatement.setDouble(86, costhour.webRegisterCostDay);
|
|
|
+ preparedStatement.setDouble(87, costhour.webRegisterCostHour);
|
|
|
+ preparedStatement.addBatch();
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ int ints[] = preparedStatement.executeBatch();
|
|
|
+ connection.commit();
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
+ System.out.println("批量插入耗时: " + (endTime - startTime) + "插入数据数量=" + ints.length);
|
|
|
+ //clickhouse 处理重复数据
|
|
|
+ //TODO:数据去重有问题,去除掉非最新的数据
|
|
|
+ Statement statement_duplicate = connection.createStatement();
|
|
|
+ String sql_duplicate = "optimize table data_monitoring.cost_hour final;";
|
|
|
+ statement_duplicate.executeQuery(sql_duplicate);
|
|
|
+ connection.commit();
|
|
|
+ long endTime_dp = System.currentTimeMillis();
|
|
|
+ System.out.println("数据清理耗时: " + (endTime_dp - endTime));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void close() throws Exception {
|
|
|
+
|
|
|
+ super.close();
|
|
|
+ }
|
|
|
+}
|