liveBook · Manning

Filtering

As explained in Kafka Streams in Action, Second Edition

filter is a Kafka Streams operation that retains records satisfying a predicate and emits deletion markers for records removed from a table result.

Filtering selectively processes records in a Kafka Streams topology. KStream#filter drops records whose predicate returns false, allowing downstream operators to handle only qualifying events. section 6.6 section 6.7

Use filtering according to the record type and desired topology behavior:

  • Place KStream.filter before downstream operations such as mapValues; its predicate receives the record key and value. section 6.6.1
  • Use filterNot when records failing the predicate should continue instead. section 6.6.1
  • On a KTable, nonmatching records produce tombstone markers rather than simply disappearing, while null input values retain delete semantics. section 8.3

Reward purchases over $10

A retailer can filter purchases before mapping them into reward objects. The predicate sums each purchased item's quantity multiplied by price and retains the record only when the total exceeds 10.00. section 6.6 section 6.6.1

Listing 6.16 Adding a filter to drop purchases not meeting rewards criteria
KStream<String, RewardAccumulator> rewardsKStream =  #1
  retailPurchaseKStream.filter((key, value) ->    #2
                              value.getPurchasedItemsList().stream()
                             .mapToDouble((item -> item.getQuantity()
                                                  * item.getPrice()))
                             .sum() > 10.00)
        .mapValues(rewardObjectMapper);   #3

Inspect the Java topology fragment that filters qualifying purchases before applying mapValues. section 6.6.1

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains predicate placement, filterNot, and KTable tombstones, but stops short of detailed topology implementation.
Examples
Demonstrates retaining retailer purchases whose item totals exceed $10 before mapping reward values.
What it assumes
Assumes a Kafka Streams topology and key-value records while defining predicates as true-or-false conditions.
What you can do after
Apply it via Kafka Streams to drop nonmatching stream records and emit tombstones for filtered table results.
How it connects
Connects filtering with predicates, KStream, KTable, mapValues, filterNot, and tombstone markers.

Also explained in

Compare how 33 books explain filtering →

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.