Lightweight Clojure bindings for Apache Kafka
Clone or download
Pull request Compare This branch is 29 commits ahead of weftio:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.


Lightweight Clojure wrapper functions for Apache Kafka Consumer + Producer APIs.

 :dependencies [[org.clojure/clojure "1.9.0"]
                [org.apache.kafka/kafka_2.12 "2.0.0"]]

Clojars Project


Gregor wraps most of the Java API for the Kafka Producer and New Consumer and is almost feature complete as of 0.2.0. The intent of this project is to stay very close to the Kafka API instead of adding more advanced features.


Here's an example of at-least-once processing (using mount):

(ns gregor-sample-app.core
  (:require [clojure.repl :as repl]
            [gregor.core :as gregor]
            [mount.core :as mount :refer [defstate]]))

(def run (atom true))

(defstate consumer
  :start (gregor/consumer "localhost:9092"
                          {"auto.offset.reset" "earliest"
                           "" "false"})
  :stop (gregor/close consumer))

(defstate producer
  :start (gregor/producer "localhost:9092")
  :stop (gregor/close producer))

(defn -main
  [& args]
  (repl/set-break-handler! (fn [sig] (reset! run false)))
  (while @run
    (let [consumer-records (gregor/poll consumer)
          values (process-records consumer-records)]
      (doseq [v values]
        (gregor/send producer "other-topic" v))
      (gregor/commit-offsets! consumer)))

Transformations over consumer records are applied in process-records. Each record in the seq returned by poll is a map. Here's an example with a JSON object as the :value:

{:value "{\"foo\":42}"
 :key nil
 :partition 0
 :topic "test-topic"
 :offset 939}


Gregor provides the send function for asynchronously sending a record to a topic. There are multiple arities which correspond to those of the ProducerRecord Java constructor. If you'd like to provide a callback to be invoked when the send has been acknowledged use send-then instead.

Topic Management

Create a topic:

(create-topic {:connection-string "localhost:2181"} "some-topic" {})

That empty map can be used to specify configuration for number of topic partitions, replication factor,

Delete a topic:

(delete-topic {:connection-string "localhost:2181"} "some-topic")

Query about a topic's existence:

(topic-exists? {:connection-string "localhost:2181"} "some-topic")

List existing topics:

(topics {:connection-string "localhost:2181"})