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
//! Kafka adapter — streaming reads (consume) and writes (produce) for Apache Kafka.
//!
//! Provides two graph nodes:
//!
//! - [`kafka_sub`] — producer that consumes messages from a Kafka topic and emits them as events
//! - [`kafka_pub`] — consumer that produces messages to a Kafka topic
//!
//! # Setup
//!
//! ## Local (Docker)
//!
//! Using Redpanda (Kafka-compatible, faster startup):
//!
//! ```sh
//! docker run --rm -p 9092:9092 \
//! -e REDPANDA_ADVERTISE_KAFKA_ADDRESS=localhost:9092 \
//! docker.redpanda.com/redpandadata/redpanda:v24.1.1 \
//! redpanda start --overprovisioned --smp 1 --memory 512M \
//! --kafka-addr 0.0.0.0:9092 --advertise-kafka-addr localhost:9092
//! ```
//!
//! # Subscribing to a topic
//!
//! [`kafka_sub`] consumes messages from a Kafka topic, starting from the configured offset.
//! Each message becomes a [`KafkaEvent`] containing the key, value, topic, partition, and offset.
//!
//! ```ignore
//! use wingfoil::adapters::kafka::*;
//! use wingfoil::*;
//!
//! let conn = KafkaConnection::new("localhost:9092");
//!
//! kafka_sub(conn, "my-topic", "my-group")
//! .collapse()
//! .for_each(|event, _| {
//! println!("{}: {:?}", event.topic, event.value_str())
//! })
//! .run(RunMode::RealTime, RunFor::Forever)
//! .unwrap();
//! ```
//!
//! # Publishing messages
//!
//! [`kafka_pub`] (or the fluent `.kafka_pub()` method) consumes a
//! `Burst<KafkaRecord>` stream and produces each record to Kafka.
//!
//! ```ignore
//! use wingfoil::adapters::kafka::*;
//! use wingfoil::*;
//!
//! let conn = KafkaConnection::new("localhost:9092");
//!
//! constant(burst![
//! KafkaRecord { topic: "my-topic".into(), key: Some(b"key1".to_vec()), value: b"hello".to_vec() },
//! ])
//! .kafka_pub(conn)
//! .run(RunMode::RealTime, RunFor::Cycles(1))
//! .unwrap();
//! ```
//!
//! # Round-trip example
//!
//! See [`examples/kafka`](https://github.com/wingfoil-io/wingfoil/tree/main/wingfoil/examples/kafka)
//! for a full working example that produces messages, consumes them, transforms
//! the values, and writes the results to a second topic.
//!
//! ```ignore
//! use wingfoil::adapters::kafka::*;
//! use wingfoil::*;
//!
//! let conn = KafkaConnection::new("localhost:9092");
//!
//! let seed = constant(burst![
//! KafkaRecord { topic: "source".into(), key: Some(b"greeting".to_vec()), value: b"hello".to_vec() },
//! KafkaRecord { topic: "source".into(), key: Some(b"subject".to_vec()), value: b"world".to_vec() },
//! ])
//! .kafka_pub(conn.clone());
//!
//! let round_trip = kafka_sub(conn.clone(), "source", "example-group")
//! .map(|burst| {
//! burst.into_iter().map(|event| {
//! let upper = event.value_str().unwrap_or("").to_uppercase().into_bytes();
//! KafkaRecord { topic: "dest".into(), key: event.key, value: upper }
//! })
//! .collect::<Burst<KafkaRecord>>()
//! })
//! .kafka_pub(conn);
//!
//! Graph::new(vec![seed, round_trip], RunMode::RealTime, RunFor::Cycles(3)).run().unwrap();
//! ```
pub use *;
pub use *;
/// Connection configuration for Kafka.
/// A record to be produced to Kafka.
/// An event consumed from a Kafka topic.