Kafka Streams is a client library for building applications and microservices, where the input and output data are stored in Kafka clusters. But, learning it with Scala is a major challenge because it does not provide a Scala API. In this KnolX we will see how to overcome the challenges of using Kafka Streams with Scala.
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Learn Kafka Streams with Scala
1. Learning Kafka Streams
with Scala
Himanshu Gupta
Lead Consultant
Knoldus Software LLP
Img src - http://danlebrero.com/2017/01/05/proof-of-concept-using-kafkastreams-and-ktables/
2. Agenda
● What is Kafka Streams?
● Why do we need to implement it in Scala?
● Challenge
● Solution
● Examples of Kafka Streams with Scala
● Demo
3. What is Kafka Streams?
The easiest way to write mission-critical real-
time applications and microservices with all
the benefits of Kafka's server-side cluster
technology
4. ● Write standard Java
applications.
● No separate processing
cluster required
● Elastic, highly scalable,
fault-tolerant
● Equally viable for small,
medium, & large use cases
● Exactly-once processing
semantics
● Develop on Mac, Linux,
Windows
● Deploy to containers,
VM(s), bare metal, cloud
● Fully integrated with Kafka
security
Key Features
5. Where we can use it?
Img src - https://kafka.apache.org/documentation/streams/
7. Why do we need to implement it in Scala?
● To provide a functional solution to a problem.
● To get better concurrency model.
● To write succinct and concise code.
● To have type safety or static typing.
11. Challenge
No Scala API
Is it really a challenge?
Yes
Because Scala does not accept a function literal as a valid
expression for any Single Abstract Method (SAM) type
12. Example
Scala functions don’t implement the Runnable and Callable
interfaces even though there’s a simply mapping.
val r: Runnable = () => println("Run!")
error: type mismatch;
found : () => Unit
required: Runnable
val r: Runnable = () =>
println("Run!")
Compile
20. Alternative Solution
Use Scala 2.12
But why?
Because Scala 2.12 accepts a function literal as a valid expression
for any Single Abstract Method (SAM) type
22. Key Features of Scala 2.12
● A trait compiles directly to an interface with default methods.
This improves binary compatibility and Java interoperability.
● Scala and Java 8 interop is also improved for functional code,
as methods that take functions can easily be called in both
directions using lambda syntax. The FunctionN classes in
Scala’s standard library are now Single Abstract Method
(SAM) types, and all SAM types are treated uniformly – from
type checking through code generation. No class file is
generated for a lambda, invokedynamic is used instead.