[!NOTE] This repo is replaced with Confluent Tutorials for Apache Kafka. We still "keep the lights on", but we don't improve existing examples any longer, nor do we add new example.
This project contains code examples that demonstrate how to implement real-time applications and event-driven microservices using the Streams API of Apache Kafka aka Kafka Streams.
For more information take a look at the latest Confluent documentation on the Kafka Streams API, notably the Developer Guide
Table of Contents
This repository has several branches to help you find the correct code examples for the version of Apache Kafka and/or Confluent Platform that you are using. See Version Compatibility Matrix below for details.
There are three kinds of examples:
Additional examples may be found under src/main/.
| Application Name | Concepts used | Java 8+ | Java 7+ | Scala |
|---|---|---|---|---|
| WordCount | DSL, aggregation, stateful | Java 8+ example | Scala Example | |
| MapFunction | DSL, stateless transformations, map() |
Java 8+ example | Scala Example | |
| SessionWindows | Sessionization of user events, user behavior analysis | Java 7+ example | ||
| GlobalKTable | join() between KStream and GlobalKTable |
Java 8+ example | ||
| GlobalStore | "join" between KStream and GlobalStore |
Java 8+ example | ||
| PageViewRegion | join() between KStream and KTable |
Java 8+ example | Java 7+ example | |
| PageViewRegionGenericAvro | Working with data in Generic Avro format | Java 8+ example | Java 7+ example | |
| WikipediaFeedSpecificAvro | Working with data in Specific Avro format | Java 8+ example | Java 7+ example | |
| SecureKafkaStreams | Secure, encryption, client authentication | Java 7+ example | ||
| Sum | DSL, stateful transformations, reduce() |
Java 8+ example | ||
| WordCountInteractiveQueries | Interactive Queries, REST, RPC | Java 8+ example | ||
| KafkaMusic | Interactive Queries, State Stores, REST API | Java 8+ example | ||
| ApplicationReset | Application Reset Tool kafka-streams-application-reset |
Java 8+ example | ||
| Microservice | Microservice ecosystem, state stores, dynamic routing, joins, filtering, branching, stateful operations | Java 8+ example |
The stream processing of Kafka Streams can be unit tested with the TopologyTestDriver from the
org.apache.kafka:kafka-streams-test-utils artifact. The test driver allows you to write sample input into your
processing topology and validate its output.
See the documentation at Testing Streams Code.
We also provide several integration tests, which demonstrate end-to-end data pipelines. Here, we spawn embedded Kafka clusters and the Confluent Schema Registry, feed input data to them (using the standard Kafka producer client), process the data using Kafka Streams, and finally read and verify the output results (using the standard Kafka consumer client).
Additional examples may be found under src/test/.
Tip: Run
mvn testto launch the tests.
| Integration Test Name | Concepts used | Java 8+ | Java 7+ | Scala |
|---|---|---|---|---|
| WordCount | DSL, aggregation, stateful | Java 8+ Example | Scala Example | |
| WordCountInteractiveQueries | Interactive Queries, REST, RPC | Java 7+ Example | ||
| Aggregate | DSL, groupBy(), aggregate() |
Java 8+ Example | Scala Example | |
| CustomStreamTableJoin | DSL, Processor API, Transformers | Java 8+ Example | ||
| EventDeduplication | DSL, Processor API, Transformers | Java 8+ Example | ||
| GlobalKTable | DSL, global state | Java 7+ Example | ||
| GlobalStore | DSL, global state, Transformers | Java 7+ Example | ||
| HandlingCorruptedInputRecords | DSL, flatMap() |
Java 8+ Example | ||
| KafkaMusic (Interactive Queries) | Interactive Queries, State Stores, REST API | Java 7+ Example | ||
| MapFunction | DSL, stateless transformations, map() |
Java 8+ Example | ||
| MixAndMatch DSL + Processor API | Integrating DSL and Processor API | Java 8+ Example | ||
| PassThrough | DSL, stream(), to() |
Java 7+ Example | ||
| PoisonPill | DSL, flatMap() |
Java 8+ Example | ||
| ProbabilisticCounting*** | DSL, Processor API, custom state stores | Scala Example | ||
| Reduce (Concatenate) | DSL, groupByKey(), reduce() |
Java 8+ Example | Scala Example | |
| SessionWindows | DSL, windowed aggregation, sessionization | Java 7+ Example | ||
| StatesStoresDSL | DSL, Processor API, Transformers | Java 8+ Example | ||
| StreamToStreamJoin | DSL, join() between KStream and KStream |
Java 7+ Example | ||
| StreamToTableJoin | DSL, join() between KStream and KTable |
Java 7+ Example | Scala Example | |
| Sum | DSL, aggregation, stateful, reduce() |
Java 8+ Example | ||
| TableToTableJoin | DSL, join() between KTable and KTable |
Java 7+ Example | ||
| UserCountsPerRegion | DSL, aggregation, stateful, count() |
Java 8+ Example | ||
| ValidateStateWithInteractiveQueries | Interactive Queries for validating state | Java 8+ Example | ||
| GenericAvro | Working with data in Generic Avro format | Java 7+ Example | Scala Example | |
| SpecificAvro | Working with data in Specific Avro format | Java 7+ Example | Scala Example |
***demonstrates how to probabilistically count items in an input stream by implementing a custom state store (CMSStore) that is backed by a Count-Min Sketch data structure (with the CMS implementation of Twitter Algebird)
This containerized example launches:
$ claude mcp add kafka-streams-examples \
-- python -m otcore.mcp_server <graph>