GithubHelp home page GithubHelp logo

clj-kafka-client's Introduction

clj-kafka-client

Clojars Project

A Clojure library for Kafka new client API.

Development is against the 0.9.0 or newer release of Kafka.

Usage

Producer

(use 'clj-kafka-client.producer)

(def producer (kafka-producer {"bootstrap.servers" "localhost:9092"}))

(let [r (record "My-Topic" "Hello world")  ; Build a record to send
      p (send-record producer r)]          ; Send the record to Kafka server and get a promise for this record
  @p)                                      ; Deref the promise to get the metadata when the record was acknowledged by server

The returned value of send-record implements the java.util.concurrent.Future interface. So if you like you can use it as a Future object instead of a promise in clojure.

Consumer

You can use kafka consumer API directly with a shallow clojure wrapper like:

(use 'clj-kafka-client.consumer)

(def consumer (kafka-consumer {"bootstrap.servers" "localhost:9092"
                               "group.id"          "My-Group"}))

(subscribe consumer)

(loop []
  (let [records (poll consumer)]
    (doseq [record records]
      (println record))))

But we recommend you to use high level API to consume Kafka topic. It helps you to do three dirty works. First, create a kafka consumer subscribing to a topic; Second, create a thread pool to process consumed messages asynchronously with the handler provided by you; Third, pause the assigned partition to prevent polling more messages when consumer can not finish processing all the polled messages during a session timeout. And calling poll periodically to ping Kafka server to keep alive during pausing time. Then finally resume polling messages when all the previous polled messages is processed.

The third part is important and fallible due to the method poll in KafkaConsumer does two things: ping the Kafka server to keep alive and poll new messages from Kafka server. If having not called poll over a session timeout the Kafka server will think the consumer is dead and start a unnecessary partition rebalance which may lead to consume some messages more than once. But sometimes your handler maybe slow and can not process all the polled messages within a session timeout. In this scenario you must call pause in KafkaConsumer to let you call poll only to ping Kafka server without polling any new messages.

The high level API is as follows:

(use 'clj-kafka-client.consumer)

(defn msg-handler [msg]
  (println msg))

(def consumer (create-high-level-consumer {"bootstrap.servers" "localhost:9092"
                                           "group.id"          "My-Group"}
                                           "My-Topic"
                                           msg-handler
                                           :worker-pool-size 10))

;; Close consumer
(close-high-level-consumer consumer)

License

Copyright © 2017 ylgrgyq

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

clj-kafka-client's People

Contributors

ylgrgyq avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.