.. title:: Kafka Streams Code Examples for Confluent Platform .. meta:: :description: Examples for programming Kafka Streams on Confluent Platform .. _streams_code_examples: |kstreams| Code Examples for |cp| --------------------------------- If you're new to |kstreams|, here is a curated list of resources to get you started. Getting started examples """""""""""""""""""""""" """"""""""" Collections """"""""""" The :cp-examples:`kafka-streams-examples|` GitHub repo is a curated repo with examples that demonstrate the use of :ref:`Kafka Streams DSL `, the :ref:`low-level Processor API `, Java 8 lambda expressions, reading and writing Avro data, and implementing unit tests with `TopologyTestDriver` and end-to-end integration tests using embedded |ak| clusters. There are also numerous |kstreams| examples in `Kafka Tutorials `__ that provide full code examples with step--by-step instructions. """" Java """" With lambda expressions for Java 8+: * :cp-examples:`WordCountLambdaExample|src/main/java/io/confluent/examples/streams/WordCountLambdaExample.java` * :cp-examples:`AnomalyDetectionLambdaExample|src/main/java/io/confluent/examples/streams/AnomalyDetectionLambdaExample.java` * :cp-examples:`GlobalKTablesExample|src/main/java/io/confluent/examples/streams/GlobalKTablesExample.java` * :cp-examples:`MapFunctionLambdaExample|src/main/java/io/confluent/examples/streams/MapFunctionLambdaExample.java` * :cp-examples:`PageViewRegionLambdaExample|src/main/java/io/confluent/examples/streams/PageViewRegionLambdaExample.java` * :cp-examples:`UserRegionLambdaExample|src/main/java/io/confluent/examples/streams/UserRegionLambdaExample.java` * :cp-examples:`WikipediaFeedAvroLambdaExample|src/main/java/io/confluent/examples/streams/WikipediaFeedAvroLambdaExample.java` * :cp-examples:`ApplicationResetExample|src/main/java/io/confluent/examples/streams/ApplicationResetExample.java` * :cp-examples:`SessionWindowsExample|src/main/java/io/confluent/examples/streams/SessionWindowsExample.java` * :cp-examples:`SumLambdaExample|src/main/java/io/confluent/examples/streams/SumLambdaExample.java` * :cp-examples:`TopArticlesLambdaExample|src/main/java/io/confluent/examples/streams/TopArticlesLambdaExample.java` Without lambda expressions for Java 7+: * :cp-examples:`PageViewRegionExample|src/main/java/io/confluent/examples/streams/PageViewRegionExample.java` * :cp-examples:`WikipediaFeedAvroExample|src/main/java/io/confluent/examples/streams/WikipediaFeedAvroExample.java` """"" Scala """"" * :cp-examples:`MapFunctionScalaExample|src/main/scala/io/confluent/examples/streams/MapFunctionScalaExample.scala` Security examples """"""""""""""""" """"""""""""""""""""""""" Java programming language """"""""""""""""""""""""" Without lambda expressions for Java 7: * :cp-examples:`SecureKafkaStreamsExample|src/main/java/io/confluent/examples/streams/SecureKafkaStreamsExample.java` Interactive Queries examples """""""""""""""""""""""""""" Since |cp| 3.1+ and |ak| 0.10.1+, it is possible to query state stores created via the :ref:`Kafka Streams DSL ` and the :ref:`Processor API `. For further information, see :ref:`streams_developer-guide_interactive-queries`. """" Java """" With lambda expressions for Java 8+: * :cp-examples:`WordCountInteractiveQueriesExample|src/main/java/io/confluent/examples/streams/interactivequeries/WordCountInteractiveQueriesExample.java` * :cp-examples:`KafkaMusicExample|src/main/java/io/confluent/examples/streams/interactivequeries/kafkamusic/KafkaMusicExample.java` End-to-end application examples """"""""""""""""""""""""""""""" These demo applications use embedded instances of |ak| and |sr-long|. They are implemented as integration tests. .. seealso:: To see end-to-end examples of |kstreams| applications deployed in an event streaming platform with all the services in |cp| and interconnecting other end systems, refer to :devx-examples:`confluentinc/examples|README.md`. """" Java """" With lambda expressions for Java 8+: * :cp-examples:`WordCountLambdaIntegrationTest|src/test/java/io/confluent/examples/streams/WordCountLambdaIntegrationTest.java` * :cp-examples:`FanoutLambdaIntegrationTest|src/test/java/io/confluent/examples/streams/FanoutLambdaIntegrationTest.java` * :cp-examples:`GenericAvroIntegrationTest|src/test/java/io/confluent/examples/streams/GenericAvroIntegrationTest.java` * :cp-examples:`GlobalKTablesExampleTest|src/test/java/io/confluent/examples/streams/GlobalKTablesExampleTest.java` * :cp-examples:`HandlingCorruptedInputRecordsIntegrationTest|src/test/java/io/confluent/examples/streams/HandlingCorruptedInputRecordsIntegrationTest.java` * :cp-examples:`MapFunctionLambdaIntegrationTest|src/test/java/io/confluent/examples/streams/MapFunctionLambdaIntegrationTest.java` * :cp-examples:`MixAndMatchLambdaIntegrationTest|src/test/java/io/confluent/examples/streams/MixAndMatchLambdaIntegrationTest.java` -- how to mix the DSL and the Processor API * :cp-examples:`SpecificAvroIntegrationTest|src/test/java/io/confluent/examples/streams/SpecificAvroIntegrationTest.java` * :cp-examples:`StateStoresInTheDSLIntegrationTest|src/test/java/io/confluent/examples/streams/StateStoresInTheDSLIntegrationTest.java` -- how to use state stores in the DSL * :cp-examples:`StreamToStreamJoinIntegrationTest|src/test/java/io/confluent/examples/streams/StreamToStreamJoinIntegrationTest.java` * :cp-examples:`StreamToTableJoinIntegrationTest|src/test/java/io/confluent/examples/streams/StreamToTableJoinIntegrationTest.java` * :cp-examples:`TableToTableJoinIntegrationTest|src/test/java/io/confluent/examples/streams/TableToTableJoinIntegrationTest.java` * :cp-examples:`UserCountsPerRegionLambdaIntegrationTest|src/test/java/io/confluent/examples/streams/UserCountsPerRegionLambdaIntegrationTest.java` * :cp-examples:`ApplicationResetIntegrationTest|src/test/java/io/confluent/examples/streams/ApplicationResetIntegrationTest.java` * :cp-examples:`EventDeduplicationLambdaIntegrationTest|src/test/java/io/confluent/examples/streams/EventDeduplicationLambdaIntegrationTest.java` * :cp-examples:`SumLambdaIntegrationTest|src/test/java/io/confluent/examples/streams/SumLambdaIntegrationTest.java` * :cp-examples:`TopArticlesLambdaExampleTest|src/test/java/io/confluent/examples/streams/TopArticlesLambdaExampleTest.java` Without lambda expressions for Java 7: * :cp-examples:`PassThroughIntegrationTest|src/test/java/io/confluent/examples/streams/PassThroughIntegrationTest.java` """"" Scala """"" * :cp-examples:`StreamToTableJoinScalaIntegrationTest|src/test/scala/io/confluent/examples/streams/StreamToTableJoinScalaIntegrationTest.scala` * :cp-examples:`ProbabilisticCountingScalaIntegrationTest|src/test/scala/io/confluent/examples/streams/ProbabilisticCountingScalaIntegrationTest.scala` - demonstrates how to probabilistically count items in an input stream by implementing a custom state store that is backed by a `Count-Min Sketch `__ data structure * :cp-examples:`GenericAvroScalaIntegrationTest|src/test/scala/io/confluent/examples/streams/GenericAvroScalaIntegrationTest.scala` * :cp-examples:`SpecificAvroScalaIntegrationTest|src/test/scala/io/confluent/examples/streams/SpecificAvroScalaIntegrationTest.scala` * :cp-examples:`WordCountScalaIntegrationTest|src/test/scala/io/confluent/examples/streams/WordCountScalaIntegrationTest.scala` Event-Driven Microservice example """"""""""""""""""""""""""""""""" """" Java """" The Event-Driven Microservice example implements an Order Service that provides a REST interface to POST and GET orders. Posting an order creates an event in |ak|, which is picked up by three different validation engines: a Fraud Service, an Inventory Service, and an Order Details Service. These services validate the order in parallel, emitting a PASS or FAIL based on whether each validation succeeds. * :cp-examples:`Microservices example source code|/src/main/java/io/confluent/examples/streams/microservices/` * :cp-examples:`Microservices example tests|/src/test/java/io/confluent/examples/streams/microservices/` * :ref:`Self-paced Kafka Streams tutorial based on the microservices example above`: deployed in an event streaming platform with all the services in |cp| and interconnecting other end systems .. image:: ../tutorials/examples/microservices-orders/docs/images/microservices-demo.png :width: 600px .. include:: ../.hidden/docs-common/home/includes/ak-share.rst