-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathtutorial3.rs
More file actions
69 lines (63 loc) · 1.76 KB
/
tutorial3.rs
File metadata and controls
69 lines (63 loc) · 1.76 KB
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
use anyhow::Result;
use csv::Reader;
use dbsp::utils::Tup2;
use dbsp::{OrdZSet, OutputHandle, RootCircuit, ZSet, ZSetHandle, ZWeight};
use feldera_macros::IsNone;
use rkyv::{Archive, Serialize};
use size_of::SizeOf;
#[derive(
Clone,
Default,
Debug,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
SizeOf,
Archive,
Serialize,
rkyv::Deserialize,
serde::Deserialize,
IsNone,
)]
#[archive_attr(derive(Ord, Eq, PartialEq, PartialOrd))]
struct Record {
location: String,
date: i32,
daily_vaccinations: Option<u64>,
}
fn build_circuit(
circuit: &mut RootCircuit,
) -> Result<(ZSetHandle<Record>, OutputHandle<OrdZSet<Record>>)> {
let (input_stream, input_handle) = circuit.add_input_zset::<Record>();
input_stream.inspect(|records| {
println!("{}", records.weighted_count());
});
let subset = input_stream.filter(|r| {
r.location == "England"
|| r.location == "Northern Ireland"
|| r.location == "Scotland"
|| r.location == "Wales"
});
Ok((input_handle, subset.output()))
}
fn main() -> Result<()> {
// Build circuit.
let (circuit, (input_handle, output_handle)) = RootCircuit::build(build_circuit)?;
// Feed data into circuit.
let path = format!(
"{}/examples/tutorial/vaccinations.csv",
env!("CARGO_MANIFEST_DIR")
);
let mut input_records = Reader::from_path(path)?
.deserialize()
.map(|result| result.map(|record| Tup2(record, 1)))
.collect::<Result<Vec<Tup2<Record, ZWeight>>, _>>()?;
input_handle.append(&mut input_records);
// Execute circuit.
circuit.transaction()?;
// Read output from circuit.
println!("{}", output_handle.consolidate().weighted_count());
Ok(())
}