OCaml bindings for Kafka
  • OCaml 55.6%
  • C 42.8%
  • Dune 1.1%
  • Makefile 0.5%
Find a file
Frej Soya ccabbb6946 run dune fmt
Format according to default rules
2023-09-11 13:40:14 +02:00
.github Bump actions/checkout from 3 to 4 2023-09-09 21:19:32 +02:00
bin run dune fmt 2023-09-11 13:40:14 +02:00
lib run dune fmt 2023-09-11 13:40:14 +02:00
lib_async run dune fmt 2023-09-11 13:40:14 +02:00
lib_helpers run dune fmt 2023-09-11 13:40:14 +02:00
lib_lwt run dune fmt 2023-09-11 13:40:14 +02:00
test/integration run dune fmt 2023-09-11 13:40:14 +02:00
test_async run dune fmt 2023-09-11 13:40:14 +02:00
test_lwt run dune fmt 2023-09-11 13:40:14 +02:00
.astylerc Async bindings for producers and consumers 2020-03-04 12:58:58 +01:00
.gitignore Update gitignore to ignore opam 2 local switches 2019-03-07 12:56:35 +01:00
.ocamlformat Add github workflow based on ocaml setup workflow 2023-09-09 20:41:07 +02:00
CHANGES.md Log changes 2020-08-25 17:24:26 +01:00
dune Add github workflow based on ocaml setup workflow 2023-09-09 20:41:07 +02:00
dune-project Add github workflow based on ocaml setup workflow 2023-09-09 20:41:07 +02:00
kafka.opam switch to dune 2.0 2022-10-31 16:14:00 +01:00
kafka.opam.template Add category prefix to freebsd depexts 2020-08-13 15:12:43 +02:00
kafka_async.opam Add github workflow based on ocaml setup workflow 2023-09-09 20:41:07 +02:00
kafka_lwt.opam switch to dune 2.0 2022-10-31 16:14:00 +01:00
librdkafka-version.c Makefile simplification. 2017-03-22 18:48:04 +01:00
LICENSE Tests and doc. 2015-01-28 15:41:03 +01:00
Makefile Add doc comments 2020-08-25 15:46:50 +01:00
README.md Update build status badge 2023-09-09 21:36:21 +02:00
TODO.md Add Kafka.flush handler 2020-08-25 16:39:00 +01:00

OCaml bindings for Kafka

Build Status

Pre-requisites

License

MIT License

Install

$ opam install kafka

From source:

$ make            # use dune
$ make test       # assuming kafka is running at localhost:9092 with a 'test' topic.
$ make install    # use opam

Usage

#use "topfind";;
#require "kafka";;

(* Prepare a producer handler. *)
let producer = Kafka.new_producer ["metadata.broker.list","localhost:9092"];;
let producer_topic = Kafka.new_topic producer "test" ["message.timeout.ms","10000"];;

(* Prepare a consumer handler *)
let consumer = Kafka.new_consumer ["metadata.broker.list","localhost:9092"];;
let consumer_topic = Kafka.new_topic consumer "test" ["auto.commit.enable","false"];;
let partition = 0;;
let timeout_ms = 1000;;

(* Start collecting messages *)
(* Here we start from offset_end, i.e. we will consume only messages produced from now. *)
Kafka.consume_start consumer_topic partition Kafka.offset_end;;

(* Produce some messages *)
Kafka.produce producer_topic ~partition "message 0";;
Kafka.produce producer_topic ~partition "message 1";;
Kafka.produce producer_topic ~partition "message 2";;

(* Consume messages *)
let rec consume t p = match Kafka.consume ~timeout_ms t p with
  | Kafka.Message(_,_,_,msg,_) -> msg
  | Kafka.PartitionEnd(_,_,_) -> consume t p
  | exception Kafka.Error(Kafka.TIMED_OUT,_) ->
    (Printf.fprintf stderr "Timeout after: %d ms\n%!" timeout_ms; consume t p)
in
let msg = consume consumer_topic partition in assert (msg = "message 0");
let msg = consume consumer_topic partition in assert (msg = "message 1");
let msg = consume consumer_topic partition in assert (msg = "message 2");

(* Stop collecting messages. *)
Kafka.consume_stop consumer_topic partition;;

(* Topics, consumers and producers must be released. *)
Kafka.destroy_topic producer_topic;;
Kafka.destroy_handler producer;;
Kafka.destroy_topic consumer_topic;;
Kafka.destroy_handler consumer;;

Documentation

The API is documented in lib/kafka.mli, and the Lwt extension is documented in lib_lwt/kafka_lwt.mli.

See bin/tail_kafka_topic.ml for a consumer using queues, batches and lwt. See bin/sendto_kafka_topic.ml for a producer.

For Async producer and consumer, see test_async/producer/producer.ml and test_async/consumer/consumer.ml.

The configuration options for producers, consumers and topics are inherited from librdkafka/CONFIGURATION.