-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataflow.yaml
349 lines (318 loc) · 7.38 KB
/
dataflow.yaml
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
apiVersion: 0.5.0
meta:
name: car-processing
version: 0.1.0
namespace: examples
config:
converter: json
consumer:
default_starting_offset:
value: 0
position: End
types:
car:
type: object
properties:
maker:
type: string
model:
type: string
color:
type: string
license:
type: string
location:
type: string
mph:
type: u32
timestamp:
type: string
maker:
type: object
properties:
maker:
type: string
country:
type: string
continent:
type: string
car-location:
type: object
properties:
car:
type: string
color:
type: string
location:
type: string
color:
type: object
properties:
color:
type: string
count:
type: u32
car-colors:
type: list
items:
type: color
license:
type: object
properties:
maker:
type: string
model:
type: string
license:
type: string
licenses:
type: list
items:
type: license
stolen-plate:
type: object
properties:
license:
type: string
owner:
type: string
violator:
type: string
topics:
cars:
schema:
value:
type: car
makers:
schema:
value:
type: maker
speeding:
schema:
value:
type: car
saratoga:
schema:
value:
type: car-location
sunnyvale:
schema:
value:
type: car-location
car-colors:
schema:
value:
type: car-colors
licenses:
consumer:
default_starting_offset:
value: 0
position: Beginning
schema:
value:
type: licenses
violations:
schema:
value:
type: stolen-plate
services:
# Call to an HTTP server to identify a car maker
lookup-maker:
sources:
- type: topic
id: cars
transforms:
- operator: map
dependencies:
- name: sdf-http
version: "0.4.2"
- name: serde_json
version: "1"
run: |
fn get_car_maker(car: Car) -> Result<Maker> {
let maker = car.maker.replace(" ", "%20");
let url = format!("https://demo-data.infinyon.com/api/maker/{maker}");
let response = sdf_http::get(url)?;
let m: Maker = serde_json::from_slice(response.as_slice())?;
Ok(m)
}
sinks:
- type: topic
id: makers
# Find speeding cars
speeding-cars:
sources:
- type: topic
id: cars
transforms:
- operator: filter
run: |
fn is_speeding(car: Car) -> Result<bool> {
Ok(car.mph > 45)
}
sinks:
- type: topic
id: speeding
# Split cars into 2 streams based on location
divide-by-location:
sources:
- type: topic
id: cars
transforms:
- operator: map
run: |
fn get_car_location(car: Car) -> Result<CarLocation> {
Ok(CarLocation {
car: format!("{} {}", car.maker, car.model),
color: car.color,
location: car.location,
})
}
sinks:
- type: topic
id: saratoga
transforms:
- operator: filter
run: |
fn is_saratoga(cl: CarLocation) -> Result<bool> {
Ok(cl.location == "Saratoga")
}
- type: topic
id: sunnyvale
transforms:
- operator: filter
run: |
fn is_sunnyvale(cl: CarLocation) -> Result<bool> {
Ok(cl.location == "Sunnyvale")
}
# Count cars by color
count-by-color:
sources:
- type: topic
id: sunnyvale
states:
count-by-color:
type: keyed-state
properties:
key:
type: string
value:
type: arrow-row
properties:
count:
type: u32
window:
tumbling:
duration: 30s
assign-timestamp:
run: |
fn assign_event_timestamp(_cl: CarLocation, event_time: i64) -> Result<i64> {
Ok(event_time)
}
partition:
assign-key:
run: |
fn key_by_color(cl: CarLocation) -> Result<String> {
Ok(cl.color)
}
update-state:
run: |
fn increment_color_count(_cl: CarLocation) -> Result<()> {
let mut cbc = count_by_color();
cbc.count += 1;
cbc.update()?;
Ok(())
}
flush:
run: |
fn get_car_color_count() -> Result<CarColors> {
let cc = sql(&format!("SELECT * FROM count_by_color"))?;
let rows = cc.rows()?;
let color_col = cc.key()?;
let count_col = cc.col("count")?;
let mut colors = vec![];
while rows.next() {
let color = rows.str(&color_col)?;
let count = rows.u32(&count_col)?;
colors.push(Color {color, count});
}
Ok(colors)
}
sinks:
- type: topic
id: car-colors
# Save license plates
save-license-plates:
sources:
- type: topic
id: licenses
states:
license-plates:
type: keyed-state
properties:
key:
type: string
value:
type: arrow-row
properties:
maker:
type: string
model:
type: string
transforms:
- operator: flat-map
run: |
fn split_licenses(licenses: Licenses) -> Result<Vec<License>> {
Ok(licenses)
}
partition:
assign-key:
run: |
fn get_license_plate(license: License) -> Result<String> {
Ok(license.license)
}
update-state:
run: |
fn add_license_plate(license: License) -> Result<()> {
let mut lp = license_plates();
lp.maker = license.maker;
lp.model = license.model;
lp.update()?;
Ok(())
}
# Check license plates
check-license-plates:
sources:
- type: topic
id: cars
states:
license-plates:
from: save-license-plates.license-plates
transforms:
- operator: filter-map
run: |
fn check_license_plate(car: Car) -> Result<Option<StolenPlate>> {
let lp = sql(&format!("select * from license_plates where _key = '{}'", car.license))?;
let rows = lp.rows()?;
let maker_col = lp.col("maker")?;
let model_col = lp.col("model")?;
if !rows.next() {
return Ok(None)
}
let maker = rows.str(&maker_col)?;
let model = rows.str(&model_col)?;
if maker != car.maker.as_str() || model != car.model.as_str() {
Ok(Some(
StolenPlate {
license: car.license.clone(),
owner: format!("{} {}", &car.maker, &car.model),
violator: format!("{} {}", maker, model)
}
))
} else {
Ok(None)
}
}
sinks:
- type: topic
id: violations