concept Kafka Streams in category kafka

appears as: Kafka Streams, Kafka Streams
Kafka Streams in Action: Real-time apps and microservices with the Kafka Streams API

This is an excerpt from Manning's book Kafka Streams in Action: Real-time apps and microservices with the Kafka Streams API.

In this book, you’ll learn how to use Kafka Streams to solve your streaming application needs. From basic extract, transform, and load (ETL) to complex stateful transformations to joining records, we’ll cover the components of Kafka Streams so you can solve these kinds of challenges in your streaming applications.

Before we dive into Kafka Streams, we’ll briefly explore the history of big data processing. As we identify problems and solutions, you’ll clearly see how the need for Kafka, and then Kafka Streams, evolved. Let’s look at how the big data era got started and what led to the Kafka Streams solution.

We’ll also define what the state needs to look like and what the requirements are for using state when we discuss state stores in Kafka Streams. Finally, we’ll weigh the importance of timestamps and look at how they can help you work with stateful operations, such as ensuring you only work with events occurring within a given time frame or helping you work with data arriving out of order.

Repartitioning in Kafka Streams

Repartitioning in Kafka Streams is easily accomplished by using the KStream.through() method, as illustrated in figure 4.7. The KStream.through() method creates an intermediate topic, and the current KStream instance will start writing records to that topic. A new KStream instance is returned from the through() method call, using the same intermediate topic for its source. This way, the data is seamlessly repartitioned.

Figure 4.7. Writing out to an intermediate topic and then reading from it in a new KStream instance

Under the covers, Kafka Streams creates a sink and source node. The sink node is a child processor of the calling KStream instance, and the new KStream instance uses the new source node for its source of records. You could write the same type of sub-topology yourself using the DSL, but using the KStream.through() method is more convenient.

We’ve come to the end of our discussion of timestamp usage. In the coming chapters, you’ll run into situations where the difference between timestamps drives some action, such as flushing the cache of a KTable. I don’t expect you remember all three types of timestamp extractors, but it’s vital to understand that timestamps are an important part of how Kafka and Kafka Streams function.

Listing 9.5. Processing transactions from a table in Kafka Streams via Connect
Serde<StockTransaction> stockTransactionSerde =
 StreamsSerdes.StockTransactionSerde();
 StreamsBuilder builder = new StreamsBuilder();
 builder.stream("dbTxnTRANSACTIONS",
 Consumed.with(stringSerde, stockTransactionSerde))
        .peek((k, v)->
 LOG.info("transactions from database key {}value {}", k, v))
        .groupByKey(
 Serialized.with(stringSerde,stockTransactionSerde))                   #1
        .aggregate(()-> 0L,(symb, stockTxn, numShares) ->
 numShares + stockTxn.getShares(),                                     #2
                         Materialized.with(stringSerde, longSerde)).toStream()
        .peek((k,v) -> LOG.info("Aggregated stock sales for {} {}",k, v))
        .to( "stock-counts", Produced.with(stringSerde, longSerde));