-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfraud_detection.sql
64 lines (59 loc) · 1.42 KB
/
fraud_detection.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
-- Create Customer table
CREATE TABLE customers (
id STRING PRIMARY KEY
) WITH (
kafka_topic='customers',
value_format='AVRO'
);
-- Create Transactions stream
CREATE STREAM transactions WITH (
kafka_topic='transactions',
value_format='AVRO'
);
-- Filter out information from Customers
CREATE TABLE customers_filtered WITH (
kafka_topic='customers-filtered',
value_format='AVRO'
) AS
SELECT
*
FROM CUSTOMERS
WHERE USERID != 'User_8'
EMIT CHANGES;
-- Enrich Transactions with Customer data
CREATE STREAM transactions_enriched WITH (
kafka_topic='transactions_enriched',
value_format='AVRO'
) AS
SELECT
*
FROM transactions t
LEFT JOIN customers_filtered c
ON t.user_id = c.id
EMIT CHANGES;
-- Identify Suspicious Activity Ongoing
CREATE TABLE transactions_suspicious WITH (
kafka_topic='transactions_suspicious',
format='AVRO'
) AS
SELECT
t_user_id AS user_id,
COUNT(t_transaction_id) AS transactions_count
FROM transactions_enriched
WINDOW TUMBLING (SIZE 60 SECONDS)
GROUP BY t_user_id
HAVING COUNT(t_transaction_id) > 4
EMIT CHANGES;
-- Identify Suspicious Activity Final
CREATE TABLE transactions_suspicious_final WITH (
kafka_topic='transactions_suspicious_final',
format='AVRO'
) AS
SELECT
t_user_id AS user_id,
COUNT(t_transaction_id) AS transactions_count
FROM transactions_enriched
WINDOW TUMBLING (SIZE 30 SECONDS)
GROUP BY t_user_id
HAVING COUNT(t_transaction_id) > 4
EMIT FINAL;