This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
/
Demo.java
195 lines (181 loc) · 6.63 KB
/
Demo.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package net.scattered_thoughts.streaming_consistency;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.*;
import org.apache.flink.table.expressions.*;
import org.apache.flink.table.functions.ScalarFunction;
import static org.apache.flink.table.api.Expressions.*;
import org.apache.flink.api.java.typeutils.*;
import org.apache.flink.api.java.tuple.*;
import org.apache.flink.types.*;
import java.sql.Timestamp;
import java.util.Properties;
import org.apache.flink.streaming.connectors.kafka.*;
import org.apache.flink.api.common.serialization.*;
public class Demo {
public static void main(String[] args) throws Exception {
EnvironmentSettings settings = EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build();
StreamExecutionEnvironment sEnv = StreamExecutionEnvironment.getExecutionEnvironment();
StreamTableEnvironment tEnv = StreamTableEnvironment.create(sEnv, settings);
tEnv.executeSql(String.join("\n",
"CREATE TABLE transactions (",
" id BIGINT,",
" from_account INT,",
" to_account INT,",
" amount DOUBLE,",
" ts TIMESTAMP(3),",
" WATERMARK FOR ts AS ts - INTERVAL '5' SECOND",
") WITH (",
" 'connector' = 'kafka',",
" 'topic' = 'transactions',",
" 'properties.bootstrap.servers' = 'localhost:9092',",
" 'properties.group.id' = 'demo',",
" 'scan.startup.mode' = 'earliest-offset',",
" 'format' = 'json',",
" 'json.fail-on-missing-field' = 'true',",
" 'json.ignore-parse-errors' = 'false'",
")"
));
tEnv.executeSql(String.join("\n",
"CREATE VIEW accepted_transactions(id) AS",
"SELECT",
" id",
"FROM",
" transactions"
));
sinkToKafka(tEnv, "accepted_transactions");
tEnv.executeSql(String.join("\n",
"CREATE VIEW outer_join_with_time(id, other_id) AS",
"SELECT",
" t1.id, t2.id as other_id",
"FROM",
" transactions as t1",
"LEFT JOIN",
" transactions as t2",
"ON",
" t1.id = t2.id AND t1.ts = t2.ts"
));
sinkToKafka(tEnv, "outer_join_with_time");
tEnv.executeSql(String.join("\n",
"CREATE VIEW outer_join_without_time(id, other_id) AS",
"SELECT",
" t1.id, t2.id as other_id",
"FROM",
" (SELECT id FROM transactions) as t1",
"LEFT JOIN",
" (SELECT id FROM transactions) as t2",
"ON",
" t1.id = t2.id"
));
sinkToKafka(tEnv, "outer_join_without_time");
tEnv.executeSql(String.join("\n",
"CREATE VIEW sums(total) AS",
"SELECT",
" sum(amount) as total",
"FROM",
" transactions"
));
sinkToKafka(tEnv, "sums");
tEnv.executeSql(String.join("\n",
"CREATE VIEW credits(account, credits) AS",
"SELECT",
" to_account as account, sum(amount) as credits",
"FROM",
" transactions",
"GROUP BY",
" to_account"
));
tEnv.executeSql(String.join("\n",
"CREATE VIEW debits(account, debits) AS",
"SELECT",
" from_account as account, sum(amount) as debits",
"FROM",
" transactions",
"GROUP BY",
" from_account"
));
tEnv.executeSql(String.join("\n",
"CREATE VIEW balance(account, balance) AS",
"SELECT",
" credits.account, credits - debits as balance",
"FROM",
" credits, debits",
"WHERE",
" credits.account = debits.account"
));
sinkToKafka(tEnv, "balance");
tEnv.executeSql(String.join("\n",
"CREATE VIEW total(total) AS",
"SELECT",
" sum(balance)",
"FROM",
" balance"
));
sinkToKafka(tEnv, "total");
tEnv.executeSql(String.join("\n",
"CREATE VIEW credits2(account, credits, ts) AS",
"SELECT",
" to_account as account, sum(amount) as credits, max(ts) as ts",
"FROM",
" transactions",
"GROUP BY",
" to_account"
));
tEnv.executeSql(String.join("\n",
"CREATE VIEW debits2(account, debits, ts) AS",
"SELECT",
" from_account as account, sum(amount) as debits, max(ts) as ts",
"FROM",
" transactions",
"GROUP BY",
" from_account"
));
tEnv.executeSql(String.join("\n",
"CREATE VIEW balance2(account, balance, ts) AS",
"SELECT",
" credits2.account, credits - debits as balance, credits2.ts",
"FROM",
" credits2, debits2",
"WHERE",
" credits2.account = debits2.account AND credits2.ts = debits2.ts"
));
sinkToKafka(tEnv, "balance");
tEnv.executeSql(String.join("\n",
"CREATE VIEW total2(total) AS",
"SELECT",
" sum(balance)",
"FROM",
" balance2"
));
sinkToKafka(tEnv, "total2");
sEnv.execute("Demo");
}
public static void sinkToKafka(StreamTableEnvironment tEnv, String name) {
Properties properties = new Properties();
properties.setProperty("bootstrap.servers", "localhost:9092");
FlinkKafkaProducer<String> sink = new FlinkKafkaProducer<String>(
name,
new SimpleStringSchema(),
properties
);
tEnv
.toRetractStream(tEnv.from(name), Row.class)
.map(kv ->
(kv.getField(0) ? "insert" : "delete")
+ " "
+ kv.getField(1).toString()
)
.addSink(sink);
}
public static class SlowMap extends ScalarFunction {
public Long eval(Long a) {
// do something for ~10ms
try {
Thread.sleep(100);
} catch (Exception e) {
}
return a;
}
}
}