liveBook · Manning

KStream

As explained in Kafka for Architects

KStream is the Kafka Streams abstraction representing a continuous stream of records and their transformations.

KStream represents a continuous stream of Kafka records. It lets an application express transformations such as filtering, mapping, and aggregation through the Kafka Streams DSL instead of manually coordinating consumers, producers, offsets, and partitions. section 8.1.3

Architecturally, a KStream aligns with a Kafka topic used as an event source or sink. It represents the incoming and outgoing event flow, while KTable represents a stateful, table-like view backed by a changelog topic. section 8.3.2

KStream is useful when an application needs to:

  • Filter events according to business conditions. section 8.1.3
  • Map event values into a different representation. section 8.1.3
  • Feed continuous events into stateful processing, including counts and time-windowed aggregations. section 8.1.3

Filtering and mapping orders

An application reads raw-orders into a KStream, keeps orders whose total is at least 50, maps each value to an order-and-customer JSON shape, and writes the result to processed-orders. section 8.1.3

Listing 8.1 OrderApp performing filtering and mapping of orders
public class OrderApp {

  public static void main(String[] args) {
    Properties props = new Properties(); //  #1
    ...
    StreamsBuilder builder = new StreamsBuilder(); //  #2

    KStream<String, String> ordersStream = builder.stream("raw-
➥orders"); //  #3

    KStream<String, String> processedOrders = ordersStream
      .filter((key, value) -> {  #4
        Order order = Order.fromJson(value);
        return order.getTotalAmount() >= 50;
      })
      .mapValues(value -> { //  #5
        Order order = Order.fromJson(value);
        return String.format("{\"orderId\": \"%s\", 
          \"customerId\": \"%s\"}", 
          order.getOrderId(), order.getCustomerId());
      });

    processedOrders.to("processed-orders"); //  #6
    KafkaStreams streams = new KafkaStreams(builder.build(), props);
    streams.start();
    Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
  }
}

Inspect the compact Java topology showing stream creation, filtering, value mapping, output, and application startup. section 8.1.3

How Kafka for Architects covers it

How deep it goes
Defines the stream abstraction and DSL role, but stops short of low-level consumer, producer, offset, and partition management.
Examples
Demonstrates an order stream that filters totals, maps values, and writes processed records to another topic.
What it assumes
Assumes Kafka topics and event serialization, while leaving serializers and deserializers as necessary configuration for stateful data.
What you can do after
Apply Kafka Streams to declare a small event-processing topology without manually creating producers and consumers.
How it connects
Connects Kafka topics, Kafka Streams DSL, KTable, state stores, changelog topics, and time-windowed aggregation.

Also explained in

Compare how 3 books explain KStream →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka for Architects. The section links point to the parts of the book it draws on.