kafka-workshop

Exercise: Joining KStream with KTable (“Currency Exchange”)

Develop a Kafka Streams application that simulate a Currency Exchange.

  1. Use the high-level Streams DSL
  2. Use StreamsBuilder.stream to create a KStream for amounts to convert
  3. Use StreamsBuilder.table to create a KTable for rates
  4. Use KStream.join to join amounts with rates

Module: Kafka Streams

Duration: 30 mins

Testing Commands

The following are some commands you can use to test your Kafka Streams application.

kafkacat -P -b :9092 -t amounts -K :
kafkacat -P -b :9092 -t rates -K :
./bin/kafka-console-consumer.sh --bootstrap-server :9092 --topic out
./bin/kafka-console-consumer.sh --bootstrap-server :9092 --topic rates --from-beginning

Solution

StreamsBuilder builder = new StreamsBuilder();

KStream<String, String> amounts = builder.stream("amounts");
KTable<String, String> rates = builder.table("rates");

KStream<String, String> out = amounts.join(rates, (amt, rate) -> "amt is " + amt * rate);
out.to("out");