Just another Network site

How to Use the Kafka Streams API

How to Use the Kafka Streams API. #BigData #DataScience #ApacheKafka #Analytics #APIs

  • We have to build two separate clusters for our app: one for our Kafka cluster that stores our data and another to do stream processing on our data.
  • The low-level Processor API provides a client to access stream data and to perform our business logic on the incoming data stream and send the result as the downstream data.
  • To start working on Kafka Streams, the following dependency must be included in the SBT project:

    Following imports are required for the application:

    Next, we have to set up some configuration properties for Kafka Streams

    Now we have to create an instance of  that provides us with a KStream object:

    The builder object has a Stream method that takes a topic name and returns an instance of the   object subscribed to that specific topic:

    Here on this  object, we can use many methods provided by the high-level DSL of Kafka Streams like ‘map’, ‘process’, ‘transform’, ‘join’ which in turn gives us another KStream object with that method applied.

  • One example demonstrates the use of Kafka Streams to combine data from two streams (different topics) and send them to a single stream (topic) using the High-Level DSL.
  • With Kafka Streams, we can process the stream data within Kafka.

Learn what the Kafka Streams API is, get a brief of its features, learn about stream processors and high-level DSL, and look at the code in action.

@gp_pulipaka: How to Use the Kafka Streams API. #BigData #DataScience #ApacheKafka #Analytics #APIs

Whenever we hear the word “Kafka,” all we think about it as a messaging system with a publisher-subscriber model that we use for our streaming applications as a source and a sink.

We could say that Kafka is just a dumb storage system that stores the data that’s been provided by a producer for a long time (configurable) and can provide it customers (from a topic, of course).

Between consuming the data from producer and then sending it to the consumer, we can’t do anything with this data in Kafka. So we make use of other tools, like Spark or Storm, to process the data between producers and consumers. We have to build two separate clusters for our app: one for our Kafka cluster that stores our data and another to do stream processing on our data.

To save us from this hassle, the Kafka Streams API comes to our rescue. With this, we have a unified Kafka where we can set our stream processing inside the Kafka cluster. And with this tight integration, we get all the support from Kafka (for example, topic partition becomes stream partition for parallel processing).

What Is the Kafka Streams API?

The Kafka Streams API allows you to create real-time applications that power your core business. It is the easiest to use yet the most powerful technology to process data stored in Kafka. It gives us the implementation of standard classes of Kafka.

A unique feature of the Kafka Streams API is that the applications you build with it are normal applications. These applications can be packaged, deployed, and monitored like any other application, with no need to install separate processing clusters or similar special-purpose and expensive infrastructure!

Source

The features provided by Kafka Streams:

A stream is the most important abstraction provided by Kafka Streams. It represents an unbounded, continuously updating data set. A stream is an ordered, replayable, and fault-tolerant sequence of immutable data records, where a data record is defined as a key-value pair. It can be considered as either a record stream (defined as KStream) or a changelog stream (defined as KTable or GlobalKTable).

A stream processor is a node in the processor topology. It represents a processing step in a topology (to transform the data). A node is basically our processing logic that we want to apply on streaming data.

As shown in the figure, a source processor is a processor without any upstream processors and a sink processor that does not have downstream processors.

The aim of this processing is to provide ways to enable processing of data that is consumed from Kafka and will be written back into Kafka. Two options available for processing stream data:

1. High-Level DSL

High-Level DSL contains already implemented methods ready to use. It is composed of two main abstractions: KStream and KTable or GlobalKTable.

A KStream is an abstraction of record stream where each data is a simple key value pair in the unbounded dataset. It provides us many functional ways to manipulate stream data like

It also provides joining methods for joining multiple streams and aggregation methods on stream data.

A KTable is an abstraction of a changelog stream. In this change log, every data record is considered an Insert or Update (Upsert) depending upon the existence of the key as any existing row with the same key will be overwritten.

 and overriding the process method which contains our logic. This process method is called once for every key-value pair.

Where the high-level DSL provides ready to use methods with functional style, the low-level processor API provides you the flexibility to implement processing logic according to your need. The trade-off is just the lines of code you need to write for specific scenarios.

Code in Action: Quickstart

To start working on Kafka Streams, the following dependency must be included in the SBT project:

Following imports are required for the application:

Next, we have to set up some configuration properties for Kafka Streams

 that provides us with a KStream object:

 object subscribed to that specific topic:

 object, we can use many methods provided by the high-level DSL of Kafka Streams like ‘map’, ‘process’, ‘transform’, ‘join’ which in turn gives us another KStream object with that method applied. And now the last step is to send this processed data to another topic

The last step is to start the streaming. For this step, we use the builder and the streaming configuration that we created:

This is a simple example of high-level DSL. For clarity, here are some examples. One example demonstrates the use of Kafka Streams to combine data from two streams (different topics) and send them to a single stream (topic) using the High-Level DSL. The other shows filtering data with stateful operations using the Low-Level Processor API. Here is the link to the code repository.

With Kafka Streams, we can process the stream data within Kafka. No separate cluster is required just for processing. With the functionality of the High-Level DSL, it’s much easier to use — but it restricts how the user to processes data. For those situations, we use Lower-Level Processor APIs.

I hope this article was of some help!

How to Use the Kafka Streams API

Comments are closed, but trackbacks and pingbacks are open.