1
- # Kafka sources & drains
1
+ # Kafka flows
2
2
3
3
Dependency:
4
4
@@ -17,7 +17,8 @@ To read from a Kafka topic, use:
17
17
import ox .kafka .{ConsumerSettings , KafkaFlow , ReceivedMessage }
18
18
import ox .kafka .ConsumerSettings .AutoOffsetReset
19
19
20
- val settings = ConsumerSettings .default(" my_group" ).bootstrapServers(" localhost:9092" ).autoOffsetReset(AutoOffsetReset .Earliest )
20
+ val settings = ConsumerSettings .default(" my_group" ).bootstrapServers(" localhost:9092" )
21
+ .autoOffsetReset(AutoOffsetReset .Earliest )
21
22
val topic = " my_topic"
22
23
23
24
val source = KafkaFlow .subscribe(settings, topic)
@@ -49,7 +50,9 @@ In order to do so, a `Flow[SendPacket]` needs to be created. The definition of `
49
50
import org .apache .kafka .clients .producer .ProducerRecord
50
51
import ox .kafka .ReceivedMessage
51
52
52
- case class SendPacket [K , V ](send : List [ProducerRecord [K , V ]], commit : List [ReceivedMessage [_, _]])
53
+ case class SendPacket [K , V ](
54
+ send : List [ProducerRecord [K , V ]],
55
+ commit : List [ReceivedMessage [_, _]])
53
56
```
54
57
55
58
The ` send ` list contains the messages to be sent (each message is a Kafka ` ProducerRecord ` ). The ` commit ` list contains
@@ -63,15 +66,17 @@ import ox.kafka.ConsumerSettings.AutoOffsetReset
63
66
import ox .pipe
64
67
import org .apache .kafka .clients .producer .ProducerRecord
65
68
66
- val consumerSettings = ConsumerSettings .default(" my_group" ).bootstrapServers(" localhost:9092" ).autoOffsetReset(AutoOffsetReset .Earliest )
69
+ val consumerSettings = ConsumerSettings .default(" my_group" )
70
+ .bootstrapServers(" localhost:9092" ).autoOffsetReset(AutoOffsetReset .Earliest )
67
71
val producerSettings = ProducerSettings .default.bootstrapServers(" localhost:9092" )
68
72
val sourceTopic = " source_topic"
69
73
val destTopic = " dest_topic"
70
74
71
75
KafkaFlow
72
76
.subscribe(consumerSettings, sourceTopic)
73
77
.map(in => (in.value.toLong * 2 , in))
74
- .map((value, original) => SendPacket (ProducerRecord [String , String ](destTopic, value.toString), original))
78
+ .map((value, original) =>
79
+ SendPacket (ProducerRecord [String , String ](destTopic, value.toString), original))
75
80
.pipe(KafkaDrain .runPublishAndCommit(producerSettings))
76
81
```
77
82
0 commit comments