Files
spring-cloud-stream-samples/kstream/kstream-word-count
Soby Chacko a2fea3a188 Adding KStream sample applications
Add sample for the canonical WordCount
Showing how to track group of products
Showing how to implement interactive queries

Fix #41

Update scst versions
Enhance kstream samples
2017-09-14 09:49:59 +05:30
..
2017-09-14 09:49:59 +05:30
2017-09-14 09:49:59 +05:30
2017-09-14 09:49:59 +05:30
2017-09-14 09:49:59 +05:30
2017-09-14 09:49:59 +05:30
2017-09-14 09:49:59 +05:30
2017-09-14 09:49:59 +05:30

== What is this app?

This is an example of a Spring Cloud Stream processor using Kafka Streams support.

The example is based on the word count application from the https://github.com/confluentinc/examples/blob/3.2.x/kafka-streams/src/main/java/io/confluent/examples/streams/WordCountLambdaExample.java[reference documentation].
It uses a single input and a single output.
In essence, the application receives text messages from an input topic and computes word occurrence counts in a configurable time window and report that in an output topic.
This sample uses lambda expressions and thus requires Java 8+.

==== Starting Kafka in a docker container

* Skip steps 1-3 if you already have a non-Docker Kafka environment.

1. Go to the docker directory in this repo and invoke the command `docker-compose up -d`.
2. Ensure that in the docker directory and then invoke the script `start-kafka-shell.sh`
3. cd $KAFKA_HOME
4. Start the console producer: +
Assuming that you are running kafka on a docker container on mac osx. Change the zookeeper IP address accordingly otherwise. +
`bin/kafka-console-producer.sh --broker-list 192.168.99.100:9092 --topic words`
5. Start the console consumer: +
Assuming that you are running kafka on a docker container on mac osx. Change the zookeeper IP address accordingly otherwise. +
`bin/kafka-console-consumer.sh --bootstrap-server 192.168.99.100:9092 --topic counts`

=== Running the app:

Go to the root of the repository and do: `./mvnw clean package`

`java -jar target/kstream-word-count-0.0.1-SNAPSHOT.jar --spring.cloud.stream.kstream.timeWindow.length=5000`

* By default we use the docker container IP (mac osx specific) in the `application.yml` for Kafka broker and zookeeper.
Change it in `application.yml` (which requires a rebuild) or pass them as runtime arguments as below.

`spring.cloud.stream.kstream.binder.brokers=<Broker IP Address>` +
`spring.cloud.stream.kstream.binder.zkNodes=<Zookeeper IP Address>`

Enter some text in the console producer and watch the output in the console consumer.

In order to switch to a hopping window, you can use the `spring.cloud.stream.kstream.timeWindow.advanceBy` (value in milliseconds).
This will create an overlapped hopping windows depending on the value you provide.

Here is an example with 2 overlapping windows (window length of 10 seconds and a hop (advance) by 5 seconds:

`java -jar target/kstream-word-count-0.0.1-SNAPSHOT.jar --spring.cloud.stream.kstream.timeWindow.length=10000 --spring.cloud.stream.kstream.timeWindow.advanceBy=5000`