-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.sql
77 lines (70 loc) · 1.94 KB
/
query.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
CREATE TABLE analytic_events (
browser TEXT,
browserLanguage TEXT,
host TEXT,
path TEXT,
referer TEXT,
screen_height INT,
screen_width INT,
source_type TEXT,
time BIGINT,
timestamp TEXT,
title TEXT,
url TEXT
) WITH (
connector = 'kafka',
format = 'json',
type = 'source',
bootstrap_servers = 'kafka:9092',
topic = 'javascript-events'
);
CREATE TABLE metrics_sink (
time TIMESTAMP,
metric TEXT,
value FLOAT,
tag TEXT
) WITH (
connector = 'kafka',
format = 'debezium_json',
'json.include_schema' = 'true',
type = 'sink',
bootstrap_servers = 'kafka:9092',
topic = 'metrics'
);
INSERT INTO metrics_sink
SELECT window.end, 'views_1_minute', count, path FROM (
SELECT count (*) as count, path, hop(interval '5 seconds', '1 minute') as window
FROM analytic_events
GROUP BY path, window);
INSERT INTO metrics_sink
SELECT window.end, 'views_15_minute', count, path FROM (
SELECT count (*) as count, path, hop(interval '5 seconds', '15 minute') as window
FROM analytic_events
GROUP BY path, window);
INSERT INTO metrics_sink
SELECT window.end, 'views_1_hour', count, path FROM (
SELECT count (*) as count, path, hop(interval '5 seconds', '1 hour') as window
FROM analytic_events
GROUP BY path, window);
CREATE TABLE top_pages_sink (
time TIMESTAMP,
page TEXT,
count FLOAT,
rank FLOAT
) WITH (
connector = 'kafka',
format = 'debezium_json',
'json.include_schema' = 'true',
type = 'sink',
bootstrap_servers = 'kafka:9092',
topic = 'top_pages'
);
INSERT INTO top_pages_sink
SELECT window.end, path, count, row_num FROM (
SELECT *, ROW_NUMBER() OVER (
PARTITION BY window
ORDER BY count DESC) as row_num
FROM (
SELECT count(*) as count, path, hop(interval '5 seconds', interval '1 hour') as window
FROM analytic_events
GROUP BY path, window)) WHERE row_num <= 10;