You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+89-12Lines changed: 89 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,9 +46,40 @@ Just add **Kafka Clients for Kotlin** to the dependencies of your projects.
46
46
47
47
## Getting Started
48
48
49
-
### Kafka Producer
49
+
### Writing messages to Kafka
50
50
51
-
See the full code-snippet : [ProducerExample.kt](https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ProducerKotlinDSLExample.kt)
51
+
**Example: How to create `KafkaProducer` config ?**
null->println("Record was successfully sent (topic=${m.topic()}, partition=${m.partition()}, offset= ${m.offset()})")
73
+
else-> e.printStackTrace()
74
+
}
75
+
}
76
+
}
77
+
}
78
+
```
79
+
80
+
N.B: See the full source code: [ProducerClientExample.kt](https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ProducerClientExample.kt)
81
+
82
+
**Example with Kotlin DSL**
52
83
53
84
```kotlin
54
85
val producer:ProducerContainer<String, String> = kafka("localhost:9092") {
println("Record was sent successfully: topic=${metadata.topic()}, partition=${metadata.partition()}, offset=${metadata.offset()}")
70
105
}
71
106
}
72
107
}
73
108
74
-
with(producer) {
75
-
init()
76
-
listOf("I ❤️ Logs", "Making Sense of Stream Processing", "Apache Kafka").forEach {
77
-
send(value = it)
78
-
}
79
-
close()
109
+
val messages =listOf("I ❤️ Logs", "Making Sense of Stream Processing", "Apache Kafka")
110
+
producer.use {
111
+
producer.init() // create internal producer and call initTransaction() if `transactional.id` is set
112
+
messages.forEach { producer.send(value = it) }
80
113
}
81
114
```
82
115
83
-
### Kafka Consumer
116
+
N.B: See the full source code: [ProducerKotlinDSLExample.kt](https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ProducerKotlinDSLExample.kt)
117
+
118
+
### Consuming messages from a Kafka topic
119
+
120
+
121
+
**Example: How to create `KafkaConsumer` config ?**
**Example with standard `KafkaConsumer` (i.e : using java `kafka-clients`)**
132
+
133
+
```kotlin
134
+
val consumer =KafkaConsumer<String, String>(configs)
135
+
136
+
consumer.use {
137
+
consumer.subscribe(listOf(topic))
138
+
while(true) {
139
+
consumer
140
+
.poll(Duration.ofMillis(500))
141
+
.forEach { record ->
142
+
println(
143
+
"Received record with key ${record.key()}"+
144
+
"and value ${record.value()} from topic ${record.topic()} and partition ${record.partition()}"
145
+
)
146
+
}
147
+
}
148
+
}
149
+
```
84
150
85
-
See the full code-snippet : [ConsumerExample.kt](https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ConsumerKotlinDSLExample.kt)
151
+
N.B: See the full source code: [ConsumerClientExample.kt](https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ConsumerClientExample.kt)
86
152
87
153
```kotlin
88
154
val consumerWorker:ConsumerWorker<String, String> = kafka("localhost:9092") {
println("All consumers started, waiting one minute before stopping")
190
+
delay(Duration.ofMinutes(1).toMillis())
191
+
}
192
+
}
118
193
```
119
194
195
+
N.B: See the full source code: [ConsumerKotlinDSLExample.kt](https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ConsumerKotlinDSLExample.kt)
196
+
120
197
## How to build project ?
121
198
122
199
Kafka Clients for Kotlin uses [maven-wrapper](https://github.com/takari/maven-wrapper).
0 commit comments