liveBook · Manning

Process method

As explained in Kafka Streams in Action, Second Edition

The process method is the Processor API method where a processor handles each incoming record and performs its processing logic.

The process method is the Processor API entry point for handling each incoming record. In a stock-performance application, it retrieves a ticker’s state, creates missing state, updates price and volume statistics, and stores the result. section 10.2.3

A typical processing cycle:

  • Read the StockPerformance object from the key-value state store using the stock ticker. section 10.2.3
  • Create the object when no stored state exists, then add the transaction’s share price and volume. section 10.2.3
  • Maintain rolling statistics after 20 trades by removing the oldest value and adding the newest rather than recalculating everything. section 10.2.3
Figure 10.7 Stock analysis process() method walk-through
figure

Follow the stock-analysis walk-through from incoming trades through state updates and rolling calculations. section 10.2.3

KStream.process() places a custom Processor API component inside a Kafka Streams DSL topology. This keeps DSL operators for most of the application while reserving lower-level control for selected logic. section 10.4

Listing 10.14 Example of mixing the DSL and the Processor API
StreamsBuilder builder = new StreamsBuilder();
StoreBuilder<KeyValueStore<String, StockPerformance>> storeBuilder =
                 Stores.keyValueStoreBuilder(storeSupplier,
                                             Serdes.String(),
                                             stockPerformanceSerde);

builder.stream(INPUT_TOPIC,   #1
              Consumed.with(stringSerde, stockTransactionSerde))
        .process(
 new StockPerformanceProcessorSupplier(
                             storeBuilder))    #2
        .peek(printKV("StockPerformance"))
        .to(OUTPUT_TOPIC,
            Produced.with(stringSerde, stockPerformanceSerde));

Inspect the mixed topology: read the input topic, inject StockPerformanceProcessor, inspect results, and write the output topic. section 10.4

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Walks through state lookup, initialization, rolling calculations, and persistence, while deferring broader financial analysis.
Examples
Uses stock transactions, StockPerformance state, 20-trade moving averages, and a mixed input-to-output topology.
What it assumes
Assumes familiarity with Kafka Streams records and state stores while demonstrating processor state updates from the ground up.
What you can do after
Build custom stateful processing and insert it into a Kafka Streams topology with KStream.process().
How it connects
Connects the Processor API with Kafka Streams DSL, state stores, key-value stores, and simple moving averages.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.