Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Kafka Clients for Kotlin GitHub release (latest by date) GitHub issues GitHub Workflow Status GitHub Repo stars

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.

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!

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


Getting Started

Writing messages to Kafka

Example: How to create KafkaProducer config ?

val configs = producerConfigsOf()
    .client { bootstrapServers("localhost:9092") }

Example with standard KafkaProducer (i.e : using java kafka-clients)

val producer = KafkaProducer<String, String>(configs)

val messages = listOf("I ❤️ Logs", "Making Sense of Stream Processing", "Apache Kafka")
producer.use {
    messages.forEach {value ->
        val record = ProducerRecord<String, String>(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: ProducerClientExample.kt

Example with Kotlin DSL

val producer: ProducerContainer<String, String> = kafka("localhost:9092") {
    client {

    producer {
        configure {


        onSendError {_, _, error ->

        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 `` is set
    messages.forEach { producer.send(value = it) }

N.B: See the full source code: ProducerKotlinDSLExample.kt

Consuming messages from a Kafka topic

Example: How to create KafkaConsumer config ?

val configs = consumerConfigsOf()
    .client { bootstrapServers("localhost:9092") }

Example with standard KafkaConsumer (i.e : using java kafka-clients)

val consumer = KafkaConsumer<String, String>(configs)

consumer.use {
    while(true) {
            .forEach { record ->
                    "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: ConsumerClientExample.kt

Example with Kotlin DSL

val consumerWorker: ConsumerWorker<String, String> = kafka("localhost:9092") {
    client {

    val stringDeserializer: Deserializer<String> = StringDeserializer()
    consumer("my-group", stringDeserializer, stringDeserializer) {
        configure {


        onPartitionsAssigned { _: Consumer<*, *>, partitions ->
            println("Partitions assigned: $partitions")

        onPartitionsRevokedAfterCommit { _: Consumer<*, *>, partitions ->
            println("Partitions revoked: $partitions")

        onConsumed { _: Consumer<*, *>, value: String? ->
            println("consumed record-value: $value")


        Runtime.getRuntime().addShutdownHook(Thread { run { stop() } })

consumerWorker.use {
    consumerWorker.start("demo-topic", maxParallelHint = 4)
    runBlocking {
        println("All consumers started, waiting one minute before stopping")

N.B: See the full source code: ConsumerKotlinDSLExample.kt

How to build project ?

Kafka Clients for Kotlin uses maven-wrapper.

$ ./mvnw clean package

Run Tests

$ ./mvnw clean test


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

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