Git Product home page Git Product logo

gregor's Introduction

Clojars Project

API | CHANGELOG

Gregor

Lightweight Clojure bindings for Apache Kafka 0.9.X and up.

Currently targetting Kafka 0.10.0.1.

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.

Example

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

(ns gregor-sample-app.core
  (:gen-class)
  (: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"
                          "testgroup"
                          ["test-topic"]
                          {"auto.offset.reset" "earliest"
                           "enable.auto.commit" "false"})
  :stop (gregor/close consumer))

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

(defn -main
  [& args]
  (mount/start)
  (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)))
  (mount/stop))

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}

Producing

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"})

gregor's People

Contributors

ccann avatar conormcd avatar lambdahands avatar ljank avatar saulius avatar tolitius avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.