monix-kafka-9


License

License

GroupId

GroupId

io.monix
ArtifactId

ArtifactId

monix-kafka-9_2.11
Last Version

Last Version

1.0.0-RC7
Release Date

Release Date

Type

Type

jar
Description

Description

monix-kafka-9
monix-kafka-9
Project URL

Project URL

https://github.com/monix/monix-kafka
Project Organization

Project Organization

io.monix
Source Code Management

Source Code Management

https://github.com/monix/monix-kafka

Download monix-kafka-9_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/io.monix/monix-kafka-9_2.11/ -->
<dependency>
    <groupId>io.monix</groupId>
    <artifactId>monix-kafka-9_2.11</artifactId>
    <version>1.0.0-RC7</version>
</dependency>
// https://jarcasting.com/artifacts/io.monix/monix-kafka-9_2.11/
implementation 'io.monix:monix-kafka-9_2.11:1.0.0-RC7'
// https://jarcasting.com/artifacts/io.monix/monix-kafka-9_2.11/
implementation ("io.monix:monix-kafka-9_2.11:1.0.0-RC7")
'io.monix:monix-kafka-9_2.11:jar:1.0.0-RC7'
<dependency org="io.monix" name="monix-kafka-9_2.11" rev="1.0.0-RC7">
  <artifact name="monix-kafka-9_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.monix', module='monix-kafka-9_2.11', version='1.0.0-RC7')
)
libraryDependencies += "io.monix" % "monix-kafka-9_2.11" % "1.0.0-RC7"
[io.monix/monix-kafka-9_2.11 "1.0.0-RC7"]

Dependencies

compile (7)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
io.monix : monix-reactive_2.11 jar 3.3.0
com.typesafe.scala-logging : scala-logging_2.11 jar 3.9.2
com.typesafe : config jar 1.4.1
org.slf4j : log4j-over-slf4j jar 1.7.30
org.scala-lang.modules : scala-collection-compat_2.11 jar 2.3.2
org.apache.kafka : kafka-clients jar 0.9.0.1

test (3)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.2.3
org.scalatest : scalatest_2.11 jar 3.0.9
org.scalacheck : scalacheck_2.11 jar 1.15.2

Project Modules

There are no modules declared in this project.

Monix-Kafka

Build Status Maven Central Scala Steward badge

Gitter

Monix integration with Kafka

Work in progress!

Table of Contents

  1. Getting Started with Kafka 1.0.x or above
  2. Getting Started with Kafka 0.11.x
  3. Getting Started with Kafka 0.10.x
  4. Getting Started with Kafka 0.9.x
  5. Getting Started with Kafka 0.8.x (no longer supported)
  6. Usage
  7. How can I contribute to Monix-Kafka?
  8. Maintainers
  9. License

Getting Started with Kafka 1.0.x or above

In SBT:

libraryDependencies += "io.monix" %% "monix-kafka-1x" % "1.0.0-RC6"

For kafka versions higher than 1.0.x also add a dependency override:

dependencyOverrides += "org.apache.kafka" % "kafka-clients" % "2.1.0"

Or in case you're interested in running the tests of this project, it now supports embedded kafka for integration testing. You can simply run:

sbt kafka1x/test

Getting Started with Kafka 0.11.x

In SBT:

libraryDependencies += "io.monix" %% "monix-kafka-11" % "1.0.0-RC6"

Or in case you're interested in running the tests of this project, it now supports embedded kafka for integration testing. You can simply run:

sbt kafka11/test

Getting Started with Kafka 0.10.x

In SBT:

libraryDependencies += "io.monix" %% "monix-kafka-10" % "1.0.0-RC6"

Or in case you're interested in running the tests of this project, it now supports embedded kafka for integration testing. You can simply run:

sbt kafka10/test

Getting Started with Kafka 0.9.x

Please note that EmbeddedKafka is not supported for Kafka 0.9.x

In SBT:

libraryDependencies += "io.monix" %% "monix-kafka-9" % "1.0.0-RC6"

Or in case you're interested in running the tests of this project, first download the Kafka server, version 0.9.x from their download page (note that 0.10.x or higher do not work with 0.9), then as the quick start section says, open a terminal window and first start Zookeeper:

bin/zookeeper-server-start.sh config/zookeeper.properties

Then start Kafka:

bin/kafka-server-start.sh config/server.properties

Create the topic we need for our tests:

bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  --replication-factor 1 --partitions 1 \
  --topic monix-kafka-tests

And run the tests:

sbt kafka9/test

Getting Started with Kafka 0.8.x

Please note that support for Kafka 0.8.x is dropped and the last available version with this dependency is 0.14.

In SBT:

libraryDependencies += "io.monix" %% "monix-kafka-8" % "0.14"

Or in case you're interested in running the tests of this project, first download the Kafka server, version 0.8.x from their download page (note that 0.9.x or higher do not work with 0.8), then as the quick start section says, open a terminal window and first start Zookeeper:

bin/zookeeper-server-start.sh config/zookeeper.properties

Then start Kafka:

bin/kafka-server-start.sh config/server.properties

Create the topics we need for our tests:

bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  --replication-factor 1 --partitions 1 \
  --topic monix-kafka-tests
bin/kafka-topics.sh --create --zookeeper localhost:2181 \
  --replication-factor 1 --partitions 1 \
  --topic monix-kafka-manual-commit-tests

And run the tests:

sbt kafka8/test

Usage

Producer

import monix.kafka._
import monix.execution.Scheduler

implicit val scheduler: Scheduler = monix.execution.Scheduler.global

// Init
val producerCfg = KafkaProducerConfig.default.copy(
  bootstrapServers = List("127.0.0.1:9092")
)

val producer = KafkaProducer[String,String](producerCfg, scheduler)

// For sending one message
val recordMetadataF = producer.send("my-topic", "my-message").runToFuture

// For closing the producer connection
val closeF = producer.close().runToFuture

Calling producer.send returns a Task of Option[RecordMetadata] which can then be run and transformed into a Future.

If the Task completes with None it means that producer.send method was called after the producer was closed and that the message wasn't successfully acknowledged by the Kafka broker. In case of the failure of the underlying Kafka client the producer will bubble up the exception and fail the Task. All successfully delivered messages will complete with Some[RecordMetadata].

For pushing an entire Observable to Apache Kafka:

import monix.kafka._
import monix.execution.Scheduler
import monix.reactive.Observable
import org.apache.kafka.clients.producer.ProducerRecord

implicit val scheduler: Scheduler = monix.execution.Scheduler.global

// Initializing the producer
val producerCfg = KafkaProducerConfig.default.copy(
  bootstrapServers = List("127.0.0.1:9092")
)

val producer = KafkaProducerSink[String,String](producerCfg, scheduler)

// Lets pretend we have this observable of records
val observable: Observable[ProducerRecord[String,String]] = ???

observable
  // on overflow, start dropping incoming events
  .whileBusyDrop
  // buffers into batches if the consumer is busy, up to a max size
  .bufferIntrospective(1024)
  // consume everything by pushing into Apache Kafka
  .consumeWith(producer)
  // ready, set, go!
  .runToFuture

Consumer

There are several ways for consuming from Apache Kafka (Version 0.11.x and above):

Consumer which commits offsets itself:

import monix.kafka._

val consumerCfg = KafkaConsumerConfig.default.copy(
  bootstrapServers = List("127.0.0.1:9092"),
  groupId = "kafka-tests"
  // you can use this settings for At Most Once semantics:
  // observableCommitOrder = ObservableCommitOrder.BeforeAck
)

val observable =
  KafkaConsumerObservable[String,String](consumerCfg, List("my-topic"))
    .take(10000)
    .map(_.value())

Consumer which allows you to commit offsets manually:

import monix.kafka._

val consumerCfg = KafkaConsumerConfig.default.copy(
  bootstrapServers = List("127.0.0.1:9092"),
  groupId = "kafka-tests"
)

val observable =
  KafkaConsumerObservable.manualCommit[String,String](consumerCfg, List("my-topic"))
    .map(message => message.record.value() -> message.committableOffset)
    .mapEval { case (value, offset) => performBusinessLogic(value).map(_ => offset) }
    .bufferTimedAndCounted(1.second, 1000)
    .mapEval(offsets => CommittableOffsetBatch(offsets).commitSync())

Enjoy!

Caveats

Issue#101 Starting from Kafka 0.10.1.0, there is max.poll.interval.ms setting:

The maximum delay between invocations of poll() when using consumer group management. 
This places an upper bound on the amount of time that the consumer can be idle before 
fetching more records. If poll() is not called before expiration of this timeout, 
then the consumer is considered failed and  the group will rebalance in order 
to reassign the partitions to another member.

Since, monix-kafka backpressures until all records has been processed. This could be a problem if processing takes time. You can reduce max.poll.records if you are experiencing this issue.

How can I contribute to Monix-Kafka?

We welcome contributions to all projects in the Monix organization and would love for you to help build Monix-Kafka. See our contributor guide for more information about how you can get involed.

Maintainers

The current maintainers (people who can merge pull requests) are:

License

All code in this repository is licensed under the Apache License, Version 2.0. See LICENSE.txt.

io.monix

Monix

Asynchronous, Reactive programming for Scala and Scala.js

Versions

Version
1.0.0-RC7
1.0.0-RC6
1.0.0-RC5
1.0.0-RC4
1.0.0-RC3
1.0.0-RC2
1.0.0-RC1
1.0.0-M3
0.17
0.16
0.15
0.14
0.13
0.12
0.11
0.10
0.9
0.8
0.7
0.6
0.5
0.4
0.3