1
+ -- adding connectors
2
+ ADD JAR ' /opt/sql-client/lib/flink-sql-connector-kafka-3.1.0-1.18.jar' ;
3
+ ADD JAR ' /opt/sql-client/lib/flink-sql-avro-confluent-registry-1.18.1.jar' ;
4
+ ADD JAR ' /opt/sql-client/lib/flink-json-1.18.1.jar' ;
5
+
6
+ DROP TABLE IF EXISTS heartbeat_60sec;
7
+ DROP TABLE IF EXISTS heartbeat;
8
+
9
+ CREATE TABLE heartbeat (
10
+ person_id STRING,
11
+ heartbeat_value INT ,
12
+ beat_time TIMESTAMP (3 ),
13
+ WATERMARK FOR beat_time AS beat_time
14
+ ) WITH (
15
+ ' connector' = ' kafka' ,
16
+ ' topic' = ' heartbeat' ,
17
+ ' properties.bootstrap.servers' = ' broker:9092' ,
18
+ ' properties.group.id' = ' flink-heartbeats' ,
19
+ ' scan.startup.mode' = ' earliest-offset' ,
20
+ ' key.format' = ' raw' ,
21
+ ' key.fields' = ' person_id' ,
22
+ ' value.format' = ' avro-confluent' ,
23
+ ' value.avro-confluent.url' = ' http://schema-registry:8081' ,
24
+ ' value.fields-include' = ' EXCEPT_KEY'
25
+ );
26
+
27
+ CREATE TABLE heartbeat_60sec (
28
+ person_id STRING,
29
+ window_start STRING,
30
+ window_end STRING,
31
+ heartbeats_over_120 BIGINT
32
+ ) WITH (
33
+ ' connector' = ' kafka' ,
34
+ ' topic' = ' heartbeat_60sec' ,
35
+ ' properties.bootstrap.servers' = ' broker:9092' ,
36
+ ' properties.group.id' = ' flink-heartbeats60sec' ,
37
+ ' scan.startup.mode' = ' earliest-offset' ,
38
+ ' key.format' = ' raw' ,
39
+ ' key.fields' = ' person_id' ,
40
+ ' value.format' = ' avro-confluent' ,
41
+ ' value.avro-confluent.url' = ' http://schema-registry:8081' ,
42
+ ' value.fields-include' = ' EXCEPT_KEY'
43
+ );
44
+
45
+ INSERT INTO heartbeat_60sec
46
+ SELECT
47
+ person_id,
48
+ DATE_FORMAT(window_start, ' yyyy-MM-dd HH:mm:ss' ) AS window_start,
49
+ DATE_FORMAT(window_end, ' yyyy-MM-dd HH:mm:ss' ) AS window_end,
50
+ COUNT (* ) AS heartbeats_over_120
51
+ FROM TABLE(TUMBLE(TABLE heartbeat, DESCRIPTOR(beat_time), INTERVAL ' 1' MINUTES))
52
+ WHERE
53
+ heartbeat_value > 120
54
+ GROUP BY
55
+ person_id, window_start, window_end;
56
+
57
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 98 , TO_TIMESTAMP(' 2023-02-18 15:10:00' ));
58
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 97 , TO_TIMESTAMP(' 2023-02-18 15:10:05' ));
59
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 103 , TO_TIMESTAMP(' 2023-02-18 15:10:10' ));
60
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 102 , TO_TIMESTAMP(' 2023-02-18 15:10:15' ));
61
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 110 , TO_TIMESTAMP(' 2023-02-18 15:10:20' ));
62
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 122 , TO_TIMESTAMP(' 2023-02-18 15:10:25' ));
63
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 125 , TO_TIMESTAMP(' 2023-02-18 15:10:30' ));
64
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 121 , TO_TIMESTAMP(' 2023-02-18 15:10:35' ));
65
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 118 , TO_TIMESTAMP(' 2023-02-18 15:10:40' ));
66
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 98 , TO_TIMESTAMP(' 2023-02-18 15:10:45' ));
67
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 98 , TO_TIMESTAMP(' 2023-02-18 15:10:50' ));
68
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 93 , TO_TIMESTAMP(' 2023-02-18 15:10:55' ));
69
+
70
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 111 , TO_TIMESTAMP(' 2023-02-18 15:15:00' ));
71
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 117 , TO_TIMESTAMP(' 2023-02-18 15:15:05' ));
72
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 125 , TO_TIMESTAMP(' 2023-02-18 15:15:10' ));
73
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 127 , TO_TIMESTAMP(' 2023-02-18 15:15:15' ));
74
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 128 , TO_TIMESTAMP(' 2023-02-18 15:15:20' ));
75
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 130 , TO_TIMESTAMP(' 2023-02-18 15:15:25' ));
76
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 131 , TO_TIMESTAMP(' 2023-02-18 15:15:30' ));
77
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 125 , TO_TIMESTAMP(' 2023-02-18 15:15:35' ));
78
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 127 , TO_TIMESTAMP(' 2023-02-18 15:15:40' ));
79
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 128 , TO_TIMESTAMP(' 2023-02-18 15:15:45' ));
80
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 129 , TO_TIMESTAMP(' 2023-02-18 15:15:50' ));
81
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 128 , TO_TIMESTAMP(' 2023-02-18 15:15:55' ));
82
+
83
+ INSERT INTO heartbeat (person_id, heartbeat_value, beat_time) VALUES (' MGG1' , 100 , TO_TIMESTAMP(' 2023-02-18 15:30:00' ));
0 commit comments