https://github.com/streamthoughts/kafka-clients-kotlin
This projects gives Kotlin bindings and several extensions for Apache Kafka Clients.
https://github.com/streamthoughts/kafka-clients-kotlin
apache-kafka kafka-clients kafka-consumer kafka-producer kotlin kotlin-library
Last synced: 5 months ago
JSON representation
This projects gives Kotlin bindings and several extensions for Apache Kafka Clients.
- Host: GitHub
- URL: https://github.com/streamthoughts/kafka-clients-kotlin
- Owner: streamthoughts
- Created: 2020-07-29T15:30:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-15T16:30:41.000Z (about 4 years ago)
- Last Synced: 2025-07-05T04:15:06.295Z (11 months ago)
- Topics: apache-kafka, kafka-clients, kafka-consumer, kafka-producer, kotlin, kotlin-library
- Language: Kotlin
- Homepage:
- Size: 158 KB
- Stars: 41
- Watchers: 2
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.adoc
Awesome Lists containing this project
README
= Kafka Clients for Kotlin
:toc:
:toc-placement!:
image:https://img.shields.io/badge/License-Apache%202.0-blue.svg[https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/LICENSE]
image:https://img.shields.io/github/v/release/streamthoughts/kafka-clients-kotlin[GitHub release (latest by date)]
image:https://img.shields.io/github/issues-raw/streamthoughts/kafka-clients-kotlin[GitHub issues]
image:https://img.shields.io/github/workflow/status/streamthoughts/kafka-clients-kotlin/Java%20CI%20with%20Maven[GitHub Workflow Status]
image:https://img.shields.io/github/stars/streamthoughts/kafka-clients-kotlin?style=social[GitHub Repo stars]
WARNING: Be aware that this package is still in heavy development. Some breaking change will occur in future weeks and months.
Thank's for your comprehension.
toc::[]
== What is Kafka Clients for Kotlin ?
The **Kafka Clients for Kotlin** projects packs with convenient Kotlin API for the development of Kafka-based event-driven applications.
It provides high-level abstractions both for sending records `ProducerContainer` and consuming records from topics using one or many
concurrent consumers `KafkaConsumerWorker`.
In addition, it provides builder classes to facilitate the configuration of `Producer` and `Consumer` objects: `KafkaProducerConfigs` and `KafkaConsumerConfigs`
**Kafka Clients for Kotlin** is based on the pure java `kafka-clients`.
== How to contribute ?
The project is in its early stages so it can be very easy to contribute by proposing APIs changes, new features and so on.
Any feedback, bug reports and PRs are greatly appreciated!
* Source Code: https://github.com/streamthoughts/kafka-clients-kotlin
* Issue Tracker: https://github.com/streamthoughts/kafka-clients-kotlin/issues
== Show your support
You think this project can help you or your team to develop kafka-based application with Kotlin ?
Please ⭐ this repository to support us!
== How to give it a try ?
Just add **Kafka Clients for Kotlin** to the dependencies of your projects.
=== For Maven
[source,xml]
----
io.streamthoughts
kafka-clients-kotlin
0.2.0
----
== Getting Started
=== Writing messages to Kafka
**Example: How to create `KafkaProducer` config ?**
[source,kotlin]
----
val configs = producerConfigsOf()
.client { bootstrapServers("localhost:9092") }
.acks(Acks.Leader)
.keySerializer(StringSerializer::class.java.name)
.valueSerializer(StringSerializer::class.java.name)
----
==== Example with standard `KafkaProducer` (i.e : using java `kafka-clients`)
[source,kotlin]
----
val producer = KafkaProducer(configs)
val messages = listOf("I ❤️ Logs", "Making Sense of Stream Processing", "Apache Kafka")
producer.use {
messages.forEach {value ->
val record = ProducerRecord(topic, value)
producer.send(record) { m: RecordMetadata, e: Exception? ->
when (e) {
null -> println("Record was successfully sent (topic=${m.topic()}, partition=${m.partition()}, offset= ${m.offset()})")
else -> e.printStackTrace()
}
}
}
}
----
N.B: See the full source code: https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ProducerClientExample.kt[ProducerClientExample.kt]
==== Example with Kotlin DSL
[source,kotlin]
----
val producer: ProducerContainer = kafka("localhost:9092") {
client {
clientId("my-client")
}
producer {
configure {
acks(Acks.InSyncReplicas)
}
keySerializer(StringSerializer())
valueSerializer(StringSerializer())
defaultTopic("demo-topic")
onSendError {_, _, error ->
error.printStackTrace()
}
onSendSuccess{ _, _, metadata ->
println("Record was sent successfully: topic=${metadata.topic()}, partition=${metadata.partition()}, offset=${metadata.offset()} ")
}
}
}
val messages = listOf("I ❤️ Logs", "Making Sense of Stream Processing", "Apache Kafka")
producer.use {
producer.init() // create internal producer and call initTransaction() if `transactional.id` is set
messages.forEach { producer.send(value = it) }
}
----
N.B: See the full source code: https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ProducerKotlinDSLExample.kt[ProducerKotlinDSLExample.kt]
=== Consuming messages from a Kafka topic
==== Example: How to create `KafkaConsumer` config ?
[source,kotlin]
----
val configs = consumerConfigsOf()
.client { bootstrapServers("localhost:9092") }
.groupId("demo-consumer-group")
.keyDeserializer(StringDeserializer::class.java.name)
.valueDeserializer(StringDeserializer::class.java.name)
----
==== Example with standard `KafkaConsumer` (i.e : using java `kafka-clients`)
[source,kotlin]
----
val consumer = KafkaConsumer(configs)
consumer.use {
consumer.subscribe(listOf(topic))
while(true) {
consumer
.poll(Duration.ofMillis(500))
.forEach { record ->
println(
"Received record with key ${record.key()} " +
"and value ${record.value()} from topic ${record.topic()} and partition ${record.partition()}"
)
}
}
}
----
N.B: See the full source code: https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ConsumerClientExample.kt[ConsumerClientExample.kt]
==== Example with Kotlin DSL
[source,kotlin]
----
val consumerWorker: ConsumerWorker = kafka("localhost:9092") {
client {
clientId("my-client")
}
val stringDeserializer: Deserializer = StringDeserializer()
consumer("my-group", stringDeserializer, stringDeserializer) {
configure {
maxPollRecords(1000)
autoOffsetReset(AutoOffsetReset.Earliest)
}
onDeserializationError(replaceWithNullOnInvalidRecord())
onPartitionsAssigned { _: Consumer<*, *>, partitions ->
println("Partitions assigned: $partitions")
}
onPartitionsRevokedAfterCommit { _: Consumer<*, *>, partitions ->
println("Partitions revoked: $partitions")
}
onConsumed { _: Consumer<*, *>, value: String? ->
println("consumed record-value: $value")
}
onConsumedError(closeTaskOnConsumedError())
Runtime.getRuntime().addShutdownHook(Thread { run { stop() } })
}
}
consumerWorker.use {
consumerWorker.start("demo-topic", maxParallelHint = 4)
runBlocking {
println("All consumers started, waiting one minute before stopping")
delay(Duration.ofMinutes(1).toMillis())
}
}
----
N.B: See the full source code: https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ConsumerKotlinDSLExample.kt[ConsumerKotlinDSLExample.kt]
== All Examples:
* https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ProducerClientExample.kt[ProducerClientExample.kt]
* https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ProducerKotlinDSLExample.kt[ProducerKotlinDSLExample.kt]
* https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/TxProducerContainerExample.kt[TxProducerContainerExample.kt]
* https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ConsumerClientExample.kt[ConsumerClientExample.kt]
* https://github.com/streamthoughts/kafka-clients-kotlin/blob/master/examples/src/main/kotlin/io/streamthoughts/kafka/client/examples/ConsumerKotlinDSLExample.kt[ConsumerKotlinDSLExample.kt]
== How to build project ?
Kafka Clients for Kotlin uses https://github.com/takari/maven-wrapper[maven-wrapper].
[source,bash]
----
$ ./mvnw clean package
----
Run Tests
[source,bash]
----
$ ./mvnw clean test
----
== Licence
Copyright 2020 StreamThoughts.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0["http://www.apache.org/licenses/LICENSE-2.0"]
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License