Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define Scala API for Kafka Streams as dependency in build.sbt?

I am trying to start a new SBT Scala project and have the following in build.sbt file:

name := "ScalaKafkaStreamsDemo"
version := "1.0"
scalaVersion := "2.12.1"

libraryDependencies += "javax.ws.rs" % "javax.ws.rs-api" % "2.1" artifacts(Artifact("javax.ws.rs-api", "jar", "jar"))

libraryDependencies += "org.apache.kafka" %% "kafka" % "2.0.0"
libraryDependencies += "org.apache.kafka" % "kafka-streams" % "2.0.0"

So according to the GitHub repo, in 2.0.0 I should see the Scala classes/functions etc etc that I want to use, however they just don't seem to be available. Within IntelliJ I can open up the kafka-streams-2.0.0.jar, but I don't see any Scala classes.

Is there another JAR I need to include?

Just while we are on the subject of extra JARs, does anyone know what JAR I need to include to be able to use the EmbeddedKafkaCluster?

like image 382
sacha barber Avatar asked Dec 05 '25 23:12

sacha barber


2 Answers

The artifact you need is kafka-streams-scala:

libraryDependencies += "org.apache.kafka" %% "kafka-streams-scala" % "2.0.1"

(please use 2.0.1, or even better 2.1.0, as 2.0.0 has some scala API bugs)

To answer your latter question, it's in the test-jar, which you can address using a classifier:

libraryDependencies += "org.apache.kafka" %% "kafka-streams" % "2.0.1" % "test" classifier "test"

But note that this is an internal class and subject to change (or removal) without notice. If at all possible, it's highly recommended that you use the TopologyTestDriver in the test-utils instead:

libraryDependencies += "org.apache.kafka" %% "kafka-streams-test-utils" % "2.0.1" % "test"
like image 199
John Avatar answered Dec 08 '25 15:12

John


It looks like you face the issue of unresolved javax.ws.rs-api dependency that happens with some Java projects that are direct or transitive dependencies of Scala projects that use sbt. I've faced it with Scala projects that use Apache Spark and recently with Kafka Streams (with and without the Scala API).

A workaround that has worked fine for me is to simply exclude the dependency and define it again explicitly.

excludeDependencies += ExclusionRule("javax.ws.rs", "javax.ws.rs-api")
libraryDependencies += "javax.ws.rs" % "javax.ws.rs-api" % "2.1.1"

Make sure that you use the latest and greatest of sbt (i.e. 1.2.7 as of the time of this writing).

With that said, the dependencies in build.sbt should be as follows:

scalaVersion := "2.12.8"

val kafkaVer = "2.1.0"
libraryDependencies += "org.apache.kafka" % "kafka-streams" % kafkaVer
libraryDependencies += "org.apache.kafka" %% "kafka-streams-scala" % kafkaVer

excludeDependencies += ExclusionRule("javax.ws.rs", "javax.ws.rs-api")
libraryDependencies += "javax.ws.rs" % "javax.ws.rs-api" % "2.1.1"

Within IntelliJ I can open up the kafka-streams-2.0.0.jar, but I don't see any Scala classes. Is there another JAR I need to include?

The following dependency is all you need:

libraryDependencies += "org.apache.kafka" %% "kafka-streams-scala" % kafkaVer
like image 43
Jacek Laskowski Avatar answered Dec 08 '25 16:12

Jacek Laskowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!