Consuming from Kafka
I have been exploring on the best ways to consume from Kafka topic in Java. There are several ways:
1 The simplest way is using KafkaListener
2 The second way is to use Apache Camel . Using Apache camel is useful if you have lot of filtering logic to be applied on incoming messages and also output the processed messages onto another topic or stream.
3 The final and my preferred way is to use Kafka Streams
There are various advantages of using Kafka’s Streams API.
Kafka’s Streams API (https://kafka.apache.org/documentation/streams/) is built on top of Kafka’s producer and consumer clients. It’s significantly more powerful and also more expressive than the Kafka consumer client. Here are some of the features of the Kafka Streams API:
- supports exactly-once processing semantics (Kafka versions 0.11+)
- supports fault-tolerant stateful processing including streaming joins, aggregations, and windowing
- supports event-time processing as well as processing based on processing-time and ingestion-time has first-class support for both streams and tables, which is where stream processing meets databases; in practice, most stream processing applications need both streams AND tables for implementing their respective use cases, so if a stream processing technology lacks either of the two abstractions (say, no support for tables) you are either stuck or must manually implement this functionality yourself (good luck with that…)
- supports interactive queries to expose the latest processing results to other applications and services)
- more expressive: it ships with (1) a functional programming style DSL with operations such as map, filter, reduce as well as (2) an imperative style Processor API for e.g. doing complex event processing (CEP), and (3) you can even combine the DSL and the Processor API.