forked from confluentinc/demo-scene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
61 lines (52 loc) · 1.95 KB
/
queries.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**********************************************/
/* */
/* Before running this script, it is a good */
/* idea to set the position of the offset to */
/* the earliest position to load all data. */
/* */
/**********************************************/
/* */
/* SET 'auto.offset.reset' = 'earliest'; */
/* */
/**********************************************/
/**********************************************/
/*************** Stream Sources ***************/
/**********************************************/
CREATE STREAM USER_GAME (USER VARCHAR, GAME STRUCT<SCORE INT, LIVES INT, LEVEL INT>)
WITH (KAFKA_TOPIC='USER_GAME', VALUE_FORMAT='JSON', PARTITIONS=6, REPLICAS=3);
CREATE STREAM USER_LOSSES (USER VARCHAR)
WITH (KAFKA_TOPIC='USER_LOSSES', VALUE_FORMAT='JSON', PARTITIONS=6, REPLICAS=3);
/**********************************************/
/***************** Scoreboard *****************/
/**********************************************/
CREATE TABLE STATS_PER_USER AS
SELECT
USER AS USER,
MAX(GAME->SCORE) AS HIGHEST_SCORE,
MAX(GAME->LEVEL) AS HIGHEST_LEVEL
FROM USER_GAME
GROUP BY USER;
CREATE TABLE LOSSES_PER_USER AS
SELECT
USER AS USER,
COUNT(USER) AS TOTAL_LOSSES
FROM USER_LOSSES
GROUP BY USER;
CREATE TABLE SCOREBOARD AS
SELECT
SPU.USER AS USER,
SPU.HIGHEST_SCORE AS HIGHEST_SCORE,
SPU.HIGHEST_LEVEL AS HIGHEST_LEVEL,
CASE WHEN LPU.TOTAL_LOSSES IS NULL
THEN CAST(0 AS BIGINT)
ELSE LPU.TOTAL_LOSSES
END AS TOTAL_LOSSES
FROM STATS_PER_USER SPU LEFT JOIN
LOSSES_PER_USER LPU ON SPU.USER = LPU.USER;
/**********************************************/
/**************** Highest Score ***************/
/**********************************************/
CREATE TABLE HIGHEST_SCORE AS
SELECT
MAX(GAME->SCORE) AS HIGHEST_SCORE
FROM USER_GAME GROUP BY 'HIGHEST_SCORE';