liveBook · Manning

Fluent interface

As explained in Kafka Streams in Action, Second Edition

A fluent interface is an API design pattern in which methods return objects that allow successive operations to be chained in a readable expression.

A fluent interface is an API design pattern that chains method calls into a concise, readable expression. In Kafka Streams, transformations return a new KStream or KTable, so each result can become the receiver for the next operation. section 6.3.1 section 6.2

A Kafka Streams chain typically follows this sequence:

  • Read records from a topic with builder.stream and Consumed serializers/deserializers. section 6.3.1
  • Transform values with an operation such as mapValues, receiving another stream. section 6.3.1
  • Write the final stream to a topic with to and Produced serializers/deserializers. section 6.3.1
  • Use named intermediate variables when chaining would reduce clarity. section 6.3.1

Uppercase a Kafka stream

KStream<String, String> result = builder.stream("src-topic", Consumed.with(Serdes.String(), Serdes.String())) .mapValues(value -> value.toUpperCase()); result.to("out-topic", Produced.with(Serdes.String(), Serdes.String())); The equivalent fluent form chains the complete topology: builder.stream("src-topic", Consumed.with(Serdes.String(), Serdes.String())).mapValues(value -> value.toUpperCase()).to("out-topic", Produced.with(Serdes.String(), Serdes.String())). section 6.3.1

builder.stream("src-topic", Consumed.with(Serdes.String(), Serdes.String()))
 .mapValues(value -> value.toUpperCase())
 .to("out-topic", Produced.with(Serdes.String(), Serdes.String()));

Inspect the complete one-expression topology to compare fluent chaining with the three-statement version. section 6.3.1

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains returned stream objects and demonstrates a complete chain, but does not descend into API internals.
Examples
Uses the Yelling app to uppercase records flowing from src-topic into out-topic.
What it assumes
Assumes basic Kafka Streams and Java familiarity while flagging the need for a running Kafka broker and serialization configuration.
What you can do after
Builds Kafka Streams topologies from source to sink using chained Java API calls.
How it connects
Connects fluent interfaces with Kafka Streams DSL, KStream, KTable, lambda expressions, Serdes, and the Processor API.

Also explained in

Compare how 4 books explain fluent interface →

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.