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/
Agenda
● What is Kafka Streams?
● Why do we need to implement it in Scala?
● Challenge
● Solution
● Examples of Kafka Streams with Scala
● Demo
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
● 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
Where we can use it?
Img src - https://kafka.apache.org/documentation/streams/
Example
object WordCountApplication {
 
    def main(args: Array[String]) {
        val textLines: KStream[String, String]  =  
builder.stream("TextLinesTopic")
        
        val wordCounts: KTable[String, Long] = textLines
            .flatMapValues(textLine => 
textLine.toLowerCase.split("W+").toIterable.asJava)
            .groupBy((_, word) => word)
            .count("Counts")
        wordCounts.to(Serdes.String(), Serdes.Long(), 
"WordsWithCountsTopic")
 
        val streams: KafkaStreams = new KafkaStreams(builder, config)
        streams.start()
    }
 
}
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.
Challenge
No Scala API
Challenge
No Scala API
Is it really a challenge?
Challenge
No Scala API
Is it really a challenge?
Yes
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
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
Solution???
Solution
val r: Runnable =
    new Runnable() { 
      def run() = println("Run!")
    }
Explicitly implement Runnable interface anonymously in
Scala
Solution
val r: Runnable =
    new Runnable() { 
      def run() = println("Run!")
    }
Explicitly implement Runnable interface anonymously in
Scala
What’s the problem with this approach?
Solution
val r: Runnable =
    new Runnable() { 
      def run() = println("Run!")
    }
Explicitly implement Runnable interface anonymously in
Scala
Its too verbose!
What can be other solution?
Alternative Solution
Use Scala 2.12
Alternative Solution
Use Scala 2.12
But why?
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
Example
It works!
It doesn’t work!
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.
Examples of Kafka Streams with
Scala
Example #1 – Simple Pipeline
Example #2 – Mapped Pipeline
(w/o SAM)
Example #2 – Mapped Pipeline
(with SAM)
Example #2 – Aggregation
Pipeline (w/o SAM)
Example #2 – Aggregation
Pipeline (with SAM)
There are other Examples too -
https://github.com/knoldus/kafka-streams-scala-examples
DEMO
References
● https://kafka.apache.org/documentation/streams/
● http://www.scala-lang.org/news/2.12.0/#lambda-syn
Thank You

More Related Content

PPTX
High Performance Serverless Functions in Scala
PDF
Async/Await: TPL & Message Pumps
PDF
Async/Await Best Practices
PDF
Raphael Amorim - Scrating React Fiber
PDF
OASGraph LoopBack 4 Integration
ODP
Gatling
PDF
Matheus Marsiglio - Isomorphic React + Redux App
PPTX
FaaS on AWS for .NET developers
High Performance Serverless Functions in Scala
Async/Await: TPL & Message Pumps
Async/Await Best Practices
Raphael Amorim - Scrating React Fiber
OASGraph LoopBack 4 Integration
Gatling
Matheus Marsiglio - Isomorphic React + Redux App
FaaS on AWS for .NET developers

What's hot (20)

PPTX
How and why test Azure Front Door with AWS Lambda & PowerShell? | Osman Sahin...
PPTX
Reactive
PPTX
Async Programming in C# 5
PPTX
Gatling overview
PDF
Deliver docker containers continuously on aws
PDF
Serverless APIs with JavaScript - Matt Searle - ChocPanda
ODP
Boost your App with Gatling
PDF
Communication tool & Environment for Remote Worker
PDF
Sînică Alboaie - Programming for cloud computing Flows of asynchronous messages
PDF
Serverless data processing with Data Pipeline
PDF
Async Await for Mobile Apps
PPTX
C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
PDF
面向引擎——编写高效率JS
PDF
Compute @ edge
PPTX
Async CTP 3 Presentation for MUGH 2012
PDF
Camunda GraphQL Extension (09/2017 Berlin)
PPTX
Intro to Functional Programming with RxJava
PPTX
AWS Jungle - Lambda
PDF
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
PPTX
London Java Community - An Experiment in Continuous Deployment of JVM applica...
How and why test Azure Front Door with AWS Lambda & PowerShell? | Osman Sahin...
Reactive
Async Programming in C# 5
Gatling overview
Deliver docker containers continuously on aws
Serverless APIs with JavaScript - Matt Searle - ChocPanda
Boost your App with Gatling
Communication tool & Environment for Remote Worker
Sînică Alboaie - Programming for cloud computing Flows of asynchronous messages
Serverless data processing with Data Pipeline
Async Await for Mobile Apps
C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin
面向引擎——编写高效率JS
Compute @ edge
Async CTP 3 Presentation for MUGH 2012
Camunda GraphQL Extension (09/2017 Berlin)
Intro to Functional Programming with RxJava
AWS Jungle - Lambda
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
London Java Community - An Experiment in Continuous Deployment of JVM applica...
Ad

Viewers also liked (6)

ODP
Deploying Microservice on Docker
ODP
Reactors.io
ODP
Introduction to Neo4j APOC
ODP
Introduction to ReactJS
ODP
Introduction to D3.js
ODP
Presto
Deploying Microservice on Docker
Reactors.io
Introduction to Neo4j APOC
Introduction to ReactJS
Introduction to D3.js
Presto
Ad

Similar to Learning Kafka Streams with Scala (20)

PPTX
Scala final ppt vinay
PDF
Lecture1
PPTX
Spark - The Ultimate Scala Collections by Martin Odersky
PPTX
Bring the Spark To Your Eyes
PDF
Programming in Spark - Lessons Learned in OpenAire project
PPT
PDF
Why scala is not my ideal language and what I can do with this
PPTX
Cleveland Hadoop Users Group - Spark
PDF
Quick introduction to scala
PPTX
Angular JS in 2017
ODP
Refactoring to Scala DSLs and LiftOff 2009 Recap
PDF
Scala and jvm_languages_praveen_technologist
PDF
Develop realtime web with Scala and Xitrum
PPTX
Learn scala and it's componenents learn it
PDF
Infographic on Scala Programming Language
PPTX
Introduction to Scala language
PDF
Martin Odersky - Evolution of Scala
PPT
Scala presentationjune112011
PPTX
Whats New in Visual Studio 2012 for C++ Developers
PDF
Red Hat Nordics 2020 - Apache Camel 3 the next generation of enterprise integ...
Scala final ppt vinay
Lecture1
Spark - The Ultimate Scala Collections by Martin Odersky
Bring the Spark To Your Eyes
Programming in Spark - Lessons Learned in OpenAire project
Why scala is not my ideal language and what I can do with this
Cleveland Hadoop Users Group - Spark
Quick introduction to scala
Angular JS in 2017
Refactoring to Scala DSLs and LiftOff 2009 Recap
Scala and jvm_languages_praveen_technologist
Develop realtime web with Scala and Xitrum
Learn scala and it's componenents learn it
Infographic on Scala Programming Language
Introduction to Scala language
Martin Odersky - Evolution of Scala
Scala presentationjune112011
Whats New in Visual Studio 2012 for C++ Developers
Red Hat Nordics 2020 - Apache Camel 3 the next generation of enterprise integ...

More from Knoldus Inc. (20)

PPTX
Angular Hydration Presentation (FrontEnd)
PPTX
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
PPTX
Self-Healing Test Automation Framework - Healenium
PPTX
Kanban Metrics Presentation (Project Management)
PPTX
Java 17 features and implementation.pptx
PPTX
Chaos Mesh Introducing Chaos in Kubernetes
PPTX
GraalVM - A Step Ahead of JVM Presentation
PPTX
Nomad by HashiCorp Presentation (DevOps)
PPTX
Nomad by HashiCorp Presentation (DevOps)
PPTX
DAPR - Distributed Application Runtime Presentation
PPTX
Introduction to Azure Virtual WAN Presentation
PPTX
Introduction to Argo Rollouts Presentation
PPTX
Intro to Azure Container App Presentation
PPTX
Insights Unveiled Test Reporting and Observability Excellence
PPTX
Introduction to Splunk Presentation (DevOps)
PPTX
Code Camp - Data Profiling and Quality Analysis Framework
PPTX
AWS: Messaging Services in AWS Presentation
PPTX
Amazon Cognito: A Primer on Authentication and Authorization
PPTX
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
PPTX
Managing State & HTTP Requests In Ionic.
Angular Hydration Presentation (FrontEnd)
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Self-Healing Test Automation Framework - Healenium
Kanban Metrics Presentation (Project Management)
Java 17 features and implementation.pptx
Chaos Mesh Introducing Chaos in Kubernetes
GraalVM - A Step Ahead of JVM Presentation
Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)
DAPR - Distributed Application Runtime Presentation
Introduction to Azure Virtual WAN Presentation
Introduction to Argo Rollouts Presentation
Intro to Azure Container App Presentation
Insights Unveiled Test Reporting and Observability Excellence
Introduction to Splunk Presentation (DevOps)
Code Camp - Data Profiling and Quality Analysis Framework
AWS: Messaging Services in AWS Presentation
Amazon Cognito: A Primer on Authentication and Authorization
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Managing State & HTTP Requests In Ionic.

Recently uploaded (20)

PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PPTX
Computer Software - Technology and Livelihood Education
PPTX
Presentation by Samna Perveen And Subhan Afzal.pptx
DOC
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
PDF
What Makes a Great Data Visualization Consulting Service.pdf
PDF
Internet Download Manager IDM Crack powerful download accelerator New Version...
PDF
Visual explanation of Dijkstra's Algorithm using Python
PPTX
R-Studio Crack Free Download 2025 Latest
PDF
Practical Indispensable Project Management Tips for Delivering Successful Exp...
PPTX
CNN LeNet5 Architecture: Neural Networks
PPTX
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
PDF
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
PPTX
Human-Computer Interaction for Lecture 2
PDF
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)
PPTX
HackYourBrain__UtrechtJUG__11092025.pptx
PDF
CapCut PRO for PC Crack New Download (Fully Activated 2025)
PDF
Guide to Food Delivery App Development.pdf
PDF
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
PPTX
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
PPTX
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
Computer Software - Technology and Livelihood Education
Presentation by Samna Perveen And Subhan Afzal.pptx
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
What Makes a Great Data Visualization Consulting Service.pdf
Internet Download Manager IDM Crack powerful download accelerator New Version...
Visual explanation of Dijkstra's Algorithm using Python
R-Studio Crack Free Download 2025 Latest
Practical Indispensable Project Management Tips for Delivering Successful Exp...
CNN LeNet5 Architecture: Neural Networks
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
Human-Computer Interaction for Lecture 2
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)
HackYourBrain__UtrechtJUG__11092025.pptx
CapCut PRO for PC Crack New Download (Fully Activated 2025)
Guide to Food Delivery App Development.pdf
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx

Learning Kafka Streams with Scala