SlideShare a Scribd company logo
Building Interactive Queries
In Kafka Streams
Bill Bejeck
@bbejeck
@bbejeck
Nice to meet you!
• Member of the DevX team
• Prior to DevX ~3 years as engineer on Kafka Streams team
• Apache Kafka® Committer and PMC member
• Author of “Kafka Streams in Action” - 2nd edition underway!
2
@bbejeck
Agenda
• Kafka Streams Background
• Interactive Queries
• Interactive Query Requirements
• Building the Query Service
• Executing Queries
3
@bbejeck
The Motivation
4
Kafka Streams Background
@bbejeck
Kafka Streams – DSL API
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String > stockStr = builder.stream(”stocks”);
stockStr.groupByKey()
.windowedBy(TimeWindows.of(Duration.ofMinutes(5))
.aggregate(()-> new TradeStats(),
(k, v, tradeStats) -> tradeStats.add(v),
Materialized.as(“trade-stats-store”))
.toStream().mapValues(..)
.to(“output”);
6
@bbejeck
Processor Topology
builder.stream(..)
stockStr.groupByKey()
.windowedBy()
.aggregate()
mapValues()
to(..)
7
@bbejeck
Tasks
8
@bbejeck
Stream Threads
9
@bbejeck
Stream Threads
10
State Management
@bbejeck
State Durability
12
@bbejeck
State Management & Task Assignment
13
@bbejeck
State Management & Task Assignment
14
@bbejeck
Fault Tolerance with State
15
@bbejeck
Solution: Standby Task
16
@bbejeck
Failover with Standby Tasks
17
@bbejeck
Standby Frictional Lag
18
Interactive Queries
@bbejeck
Traditional Querying
20
@bbejeck
Interactive Queries
21
@bbejeck
Interactive Queries
22
@bbejeck
IQ Availability
23
@bbejeck
IQ Availability
24
@bbejeck
IQ with Standbys
25
Interactive Query Requirements
@bbejeck
Interactive Query Requirements
Naming the store makes it available
for Interactive Queries
Materialized.as(“trade-stats-store”))
27
@bbejeck
Interactive Query Requirements
Set the application.server configuration
28
@bbejeck
Interactive Query Requirements
server.port=7076
application.server=http://somehost:${server.port}
29
Implementing The Query Service
@bbejeck
Implementing The Query Service
1. Inversion of Control container
2. Uses Dependency Injection to implement IoC
3. Inject dependencies without implementation specifics
4. Autowiring achieved with annotations
31
@bbejeck
Implementing The Query Service
Spring Boot
1. Provides the Spring container
2. Embeds a web-server
3. Quickly set up a stand-alone application
32
@bbejeck
Implementing The Query Service
1. Build the Topology
2. Handle Kafka Streams Lifecycle
3. Expose REST endpoint / Handle Queries
4. A view for rendering query results
33
@bbejeck
Build the Topology
@Component
public class AggregationStream {
private final KafkaStreamsAppConfiguration streamsConfiguration;
@Autowired
public AggregationStream(KafkaStreamsAppConfiguration streamsConfiguration) {
this.streamsConfiguration = streamsConfiguration;
}
34
@bbejeck
Build the Topology
public Topology topology() {
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> input =
builder.stream(streamsConfiguration.inputTopic(),
Consumed.with(stringSerde, stringSerde)
//Details omitted
return builder.build()
}
35
@bbejeck
KafkaStreams Instance
@Component
public class KafkaStreamsContainer {
private final KafkaStreamsAppConfiguration appConfiguration;
private final AggregationStream aggregationStream;
private KafkaStreams kafkaStreams
@Autowired
public KafkaStreamsContainer(final AggregationStream aggregationStream,
final KafkaStreamsAppConfiguration appConfiguration) {
}
//Details omitted
36
@bbejeck
KafkaStreams Instance
@Component
public class KafkaStreamsContainer {
@Bean
public KafkaStreams kafkaStreams() {
return kafkaStreams;
}
}
37
@bbejeck
Start Kafka Streams
@PostConstruct
public void init() {
Properties properties = appConfiguration.streamsConfigs();
Topology topology = aggregationStream.topology();
kafkaStreams = new KafkaStreams(topology, properties);
kafkaStreams.cleanUp(); //Optional – not a production setting
kafkaStreams.start();
}
38
@bbejeck
Stop Kafka Streams
@PreDestroy
public void tearDown(){
kafkaStreams.close(Duration.ofSeconds(10));
}
39
@bbejeck
Expose REST endpoint
@RestController
@RequestMapping("/streams-iq”)
public class StockController {
private final KafkaStreams kafkaStreams;
private final RestTemplate restTemplate
}
40
@bbejeck
Inject KafkaStreams Object
@Autowired
public StockController(final KafkaStreams kafkaStreams,
final RestTemplateBuilder restTemplateBuilder) {
this.kafkaStreams = kafkaStreams;
this.restTemplate = restTemplateBuilder.build()
}
41
@bbejeck
Set “this” HostInfo
@Value("${application.server}")
private String applicationServer;
. . . .
@PostConstruct
public void init() {
String[] parts = applicationServer.split(":");
thisHostInfo = new HostInfo(parts[0], Integer.parseInt(parts[1]));
}
42
@bbejeck
Set “this” HostInfo
43
@bbejeck
Handle Incoming Requests
private static final String IQ_URL = "http://{host}:{port}/streams-iq{path}";
@GetMapping(value = "/keyquery/{symbol}")
public QueryResponse<V> getAggregation(@PathVariable String symbol) {
//Execute the query
}
44
@bbejeck
Handle Incoming Requests
private static final String IQ_URL = "http://{host}:{port}/streams-iq{path}";
@GetMapping(value = "/range")
public QueryResponse<List<V>> getAggregationRange(
@RequestParam(required = false) String lower,
@RequestParam(required = false) String upper) {
//Execute the query
45
Executing Queries
@bbejeck
Executing Queries
Two step process
1. Find the correct host *
2. Query the current instance or forward to the correct one
47
@bbejeck
Executing Queries
KeyQueryMetadata =
KafkaStreams.queryMetadataForKey(StoreName,
<Key>,
<Key Serializer>)
48
@bbejeck
Executing Queries
HostInfo = KeyQueryMetadata.activeHost()
Set<HostInfo> = KeyQueryMetadata.standByHosts()
int partition = KeyQueryMetadata.partition();
49
@bbejeck
Executing Queries
if (keyMetadata.activeHost().equals(thisHostInfo)) {
//query current Kafka Streams instance
}
50
@bbejeck
Executing Queries
} else {
String host = keyMetadata.activeHost().host();
int port = keyMetadata.activeHost().port();
String path = "/keyquery/” + symbol;
51
@bbejeck
Executing Queries
} else {
String host = keyMetadata.activeHost().host();
int port = keyMetadata.activeHost().port();
String path = String.format("/keyquery/" + symbol;
try {
resp = restTemplate.getForObject(IQ_URL, QueryResponse.class, host, port, path);
} catch (RestClientException exception) {
//Handle error
}
52
@bbejeck
Executing Queries – Query All Hosts*
53
@bbejeck
Executing Queries – Query All Hosts*
Collection<StreamsMetadata> storeMetadata =
KafkaStreams.allMetadataForStore(“store-name”);
Collection<StreamsMetadata> allStreamsMetadata =
KafkaStreams.metadataForAllStreamsClients();*
54
@bbejeck
Executing Queries – Query All Hosts*
@GetMapping(value = "/range")
public QueryResponse<R> getAggregationRange(
@RequestParam(required = false) String lower,
@RequestParam(required = false) String upper) {
Collection<StreamsMetadata> streamsMetadata =
kafkaStreams.streamsMetadataForStore(storeName);
streamsMetadata.forEach(streamsClient -> {
// query each client
}
55
@bbejeck
Executing Queries – Query All Hosts*
@GetMapping(value = "/internal/range")
public QueryResponse<R> getAggregationRangeInternal(
@RequestParam(required = false) String lower,
@RequestParam(required = false) String upper,
@RequestParam(required = false) List<> partitions
return doRangeQuery(thisHostInfo, partitions, lower, upper);
}
56
Executing Queries
@bbejeck
Executing Queries
StateQueryResult<R> result =
KafkaStreams.query(
StateQueryRequest.inStore(StoreName)
.withQuery(Query<R> instance)
);
58
@bbejeck
Executing Queries – Query Types
KeyQuery
RangeQuery *
WindowKeyQuery
WindowRangeQuery *
59
@bbejeck
Executing Queries – Options
// Default is active or standby
StateQueryRequest.requireActive();
//Default is all partitions
StateQueryRequest.withPartitions(Set<Integer>partitions);
60
@bbejeck
Executing Queries – Options
StateQueryRequest.withPositionBound(PositionBound pb);
StateQueryRequest.enableExecutionInfo();
61
@bbejeck
Executing Queries – Results
Map<Integer QueryResult<R>> result =
StateQueryResult.getPartitionResults();
//other methods
StateQueryResult.getGlobalResult();
StateQueryResult.getOnlyPartitionResult();
62
Displaying Results
@bbejeck
Index Page Location
src
main
resources
public
index.html
js and css files
64
@bbejeck
Single Page Application
<script type="text/javascript">
function runIqTables() {
getRangeTableData("http://localhost:7076/streams-iq/range")
updateKeyTableWithList("http://localhost:7076/streams-iq/keyquery/");
}
function getRangeTableData(path) {
$.getJSON(path, function (response) {
. . .
});
}
....
setInterval(runIqTables, 5000);
</script>
65
@bbejeck
Summary
• Interactive Queries allow to you view the state of a running Kafka Streams
application
• You can leverage standbys for higher availability with IQ
• Use Interactive Queries v2
• By leveraging Spring Boot and creating an @RestController quickly get a
Kafka Streams application with IQ enabled
66
@bbejeck
Thank You!
67
@bbejeck
bill@confluent.io
@bbejeck
Resources
• Source Code - https://github.com/bbejeck/KafkaStreamsInteractiveQueries
• Kafka Tutorials - https://kafka-tutorials.confluent.io/
• Confluent Developer - developer.confluent.io
• Spring Boot - https://spring.io/projects/spring-boot/
• Spring Kafka - https://spring.io/projects/spring-kafka
• Documentation - https://docs.confluent.io/current/streams
68
Your Apache Kafka®
journey begins here
developer.confluent.io
69

More Related Content

PDF
Kafka Streams: What it is, and how to use it?
PDF
Getting Started with Confluent Schema Registry
PDF
Apache Kafka - Martin Podval
PDF
Introduction to Kafka Streams
PPTX
Elastic - ELK, Logstash & Kibana
PDF
Introducing KRaft: Kafka Without Zookeeper With Colin McCabe | Current 2022
PDF
ksqlDB - Stream Processing simplified!
PPTX
Autoscaling Flink with Reactive Mode
Kafka Streams: What it is, and how to use it?
Getting Started with Confluent Schema Registry
Apache Kafka - Martin Podval
Introduction to Kafka Streams
Elastic - ELK, Logstash & Kibana
Introducing KRaft: Kafka Without Zookeeper With Colin McCabe | Current 2022
ksqlDB - Stream Processing simplified!
Autoscaling Flink with Reactive Mode

What's hot (20)

PPTX
Processing Large Data with Apache Spark -- HasGeek
PDF
Improving fault tolerance and scaling out in Kafka Streams with Bill Bejeck |...
PPTX
Squirreling Away $640 Billion: How Stripe Leverages Flink for Change Data Cap...
PDF
Designing ETL Pipelines with Structured Streaming and Delta Lake—How to Archi...
PPSX
Apache Flink, AWS Kinesis, Analytics
PPTX
大量のデータ処理や分析に使えるOSS Apache Sparkのご紹介(Open Source Conference 2020 Online/Kyoto ...
PDF
Apache Spark Data Source V2 with Wenchen Fan and Gengliang Wang
PPTX
YugaByte DB Internals - Storage Engine and Transactions
PPTX
Introduction to Apache Kafka
PPTX
スケールアウトするPostgreSQLを目指して!その第一歩!(NTTデータ テクノロジーカンファレンス 2020 発表資料)
PPTX
Envoy and Kafka
PDF
All about Zookeeper and ClickHouse Keeper.pdf
PDF
When NOT to use Apache Kafka?
PDF
Oracle GoldenGate アーキテクチャと基本機能
PDF
Consumer offset management in Kafka
PDF
Native Support of Prometheus Monitoring in Apache Spark 3.0
PDF
Fundamentals of Apache Kafka
PDF
Solving Enterprise Data Challenges with Apache Arrow
ODP
Introduction to Kafka connect
PDF
Introduce to Terraform
Processing Large Data with Apache Spark -- HasGeek
Improving fault tolerance and scaling out in Kafka Streams with Bill Bejeck |...
Squirreling Away $640 Billion: How Stripe Leverages Flink for Change Data Cap...
Designing ETL Pipelines with Structured Streaming and Delta Lake—How to Archi...
Apache Flink, AWS Kinesis, Analytics
大量のデータ処理や分析に使えるOSS Apache Sparkのご紹介(Open Source Conference 2020 Online/Kyoto ...
Apache Spark Data Source V2 with Wenchen Fan and Gengliang Wang
YugaByte DB Internals - Storage Engine and Transactions
Introduction to Apache Kafka
スケールアウトするPostgreSQLを目指して!その第一歩!(NTTデータ テクノロジーカンファレンス 2020 発表資料)
Envoy and Kafka
All about Zookeeper and ClickHouse Keeper.pdf
When NOT to use Apache Kafka?
Oracle GoldenGate アーキテクチャと基本機能
Consumer offset management in Kafka
Native Support of Prometheus Monitoring in Apache Spark 3.0
Fundamentals of Apache Kafka
Solving Enterprise Data Challenges with Apache Arrow
Introduction to Kafka connect
Introduce to Terraform
Ad

Similar to Building an Interactive Query Service in Kafka Streams With Bill Bejeck | Current 2022 (20)

PPTX
Kick your database_to_the_curb_reston_08_27_19
PPTX
Kick Your Database to the Curb
PDF
What's in store? Part Deux; Creating Custom Queries with Kafka Streams IQv2
PDF
Richmond kafka streams intro
PDF
Interactive Query in Kafka Streams: The Next Generation with Vasiliki Papavas...
PDF
Kafka Streams: the easiest way to start with stream processing
PDF
Kafka Summit SF 2017 - Query the Application, Not a Database: “Interactive Qu...
PPTX
Exactly-once Stream Processing with Kafka Streams
PDF
Kafka Summit SF 2017 - Exactly-once Stream Processing with Kafka Streams
PDF
Performance Analysis and Optimizations for Kafka Streams Applications
PDF
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
PDF
Stream Processing made simple with Kafka
PDF
Apples and Oranges - Comparing Kafka Streams and Flink with Bill Bejeck
PDF
Queryable State for Kafka Streamsを使ってみた
PDF
Apache Kafka, and the Rise of Stream Processing
PDF
Deep Dive Into Kafka Streams (and the Distributed Stream Processing Engine) (...
PDF
Building a Scalable Real-Time Fleet Management IoT Data Tracker with Kafka St...
PDF
The Possibilities and Pitfalls of Writing Your Own State Stores with Daan Gertis
ODP
Stream processing using Kafka
PDF
Akka Streams And Kafka Streams: Where Microservices Meet Fast Data
Kick your database_to_the_curb_reston_08_27_19
Kick Your Database to the Curb
What's in store? Part Deux; Creating Custom Queries with Kafka Streams IQv2
Richmond kafka streams intro
Interactive Query in Kafka Streams: The Next Generation with Vasiliki Papavas...
Kafka Streams: the easiest way to start with stream processing
Kafka Summit SF 2017 - Query the Application, Not a Database: “Interactive Qu...
Exactly-once Stream Processing with Kafka Streams
Kafka Summit SF 2017 - Exactly-once Stream Processing with Kafka Streams
Performance Analysis and Optimizations for Kafka Streams Applications
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
Stream Processing made simple with Kafka
Apples and Oranges - Comparing Kafka Streams and Flink with Bill Bejeck
Queryable State for Kafka Streamsを使ってみた
Apache Kafka, and the Rise of Stream Processing
Deep Dive Into Kafka Streams (and the Distributed Stream Processing Engine) (...
Building a Scalable Real-Time Fleet Management IoT Data Tracker with Kafka St...
The Possibilities and Pitfalls of Writing Your Own State Stores with Daan Gertis
Stream processing using Kafka
Akka Streams And Kafka Streams: Where Microservices Meet Fast Data
Ad

More from HostedbyConfluent (20)

PDF
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
PDF
Renaming a Kafka Topic | Kafka Summit London
PDF
Evolution of NRT Data Ingestion Pipeline at Trendyol
PDF
Ensuring Kafka Service Resilience: A Dive into Health-Checking Techniques
PDF
Exactly-once Stream Processing with Arroyo and Kafka
PDF
Fish Plays Pokemon | Kafka Summit London
PDF
Tiered Storage 101 | Kafla Summit London
PDF
Building a Self-Service Stream Processing Portal: How And Why
PDF
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
PDF
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
PDF
Navigating Private Network Connectivity Options for Kafka Clusters
PDF
Apache Flink: Building a Company-wide Self-service Streaming Data Platform
PDF
Explaining How Real-Time GenAI Works in a Noisy Pub
PDF
TL;DR Kafka Metrics | Kafka Summit London
PDF
A Window Into Your Kafka Streams Tasks | KSL
PDF
Mastering Kafka Producer Configs: A Guide to Optimizing Performance
PDF
Data Contracts Management: Schema Registry and Beyond
PDF
Code-First Approach: Crafting Efficient Flink Apps
PDF
Debezium vs. the World: An Overview of the CDC Ecosystem
PDF
Beyond Tiered Storage: Serverless Kafka with No Local Disks
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Renaming a Kafka Topic | Kafka Summit London
Evolution of NRT Data Ingestion Pipeline at Trendyol
Ensuring Kafka Service Resilience: A Dive into Health-Checking Techniques
Exactly-once Stream Processing with Arroyo and Kafka
Fish Plays Pokemon | Kafka Summit London
Tiered Storage 101 | Kafla Summit London
Building a Self-Service Stream Processing Portal: How And Why
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
Navigating Private Network Connectivity Options for Kafka Clusters
Apache Flink: Building a Company-wide Self-service Streaming Data Platform
Explaining How Real-Time GenAI Works in a Noisy Pub
TL;DR Kafka Metrics | Kafka Summit London
A Window Into Your Kafka Streams Tasks | KSL
Mastering Kafka Producer Configs: A Guide to Optimizing Performance
Data Contracts Management: Schema Registry and Beyond
Code-First Approach: Crafting Efficient Flink Apps
Debezium vs. the World: An Overview of the CDC Ecosystem
Beyond Tiered Storage: Serverless Kafka with No Local Disks

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Electronic commerce courselecture one. Pdf
PDF
KodekX | Application Modernization Development
PDF
cuic standard and advanced reporting.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Big Data Technologies - Introduction.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
sap open course for s4hana steps from ECC to s4
Electronic commerce courselecture one. Pdf
KodekX | Application Modernization Development
cuic standard and advanced reporting.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Advanced methodologies resolving dimensionality complications for autism neur...
20250228 LYD VKU AI Blended-Learning.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Per capita expenditure prediction using model stacking based on satellite ima...

Building an Interactive Query Service in Kafka Streams With Bill Bejeck | Current 2022