TimeWindowedKStream — Time-Windowed Streaming Aggregations

TimeWindowedKStream is the abstraction of a windowed record stream that allows Kafka Streams developers for aggregate, count, reduce aggregations.

TimeWindowedKStream is the result of KGroupedStream.windowedBy stream operator with a TimeWindows window specification.

Tip
Use Scala API for Kafka Streams to make your Kafka Streams development more pleasant if Scala is your programming language.
import org.apache.kafka.streams.kstream.TimeWindows
import scala.concurrent.duration._
val everyMinute = TimeWindows.of(1.minute.toMillis)

import org.apache.kafka.streams.scala.StreamsBuilder
import org.apache.kafka.streams.scala._
import ImplicitConversions._
import Serdes._
val builder = new StreamsBuilder
val windowedKStream = builder
  .stream[String, String]("events")
  .groupByKey
  .windowedBy(everyMinute)
scala> :type windowedKStream
org.apache.kafka.streams.scala.kstream.TimeWindowedKStream[String,String]

// Print out counts over 1-minute time windows to stdout
import org.apache.kafka.streams.kstream.Printed
import org.apache.kafka.streams.kstream.Windowed
val stdout = Printed
  .toSysOut[Windowed[String], Long]
  .withLabel("[time-windows]")
windowedKStream
  .count
  .toStream
  .print(stdout)

// Use println(builder.build.describe) to see the whole topology
Table 1. TimeWindowedKStream API / Windowed Operators
Method Description

aggregate

KTable<Windowed<K>, VR> aggregate(
  final Initializer<VR> initializer,
  final Aggregator<? super K, ? super V, VR> aggregator)
KTable<Windowed<K>, VR> aggregate(
  final Initializer<VR> initializer,
  final Aggregator<? super K, ? super V, VR> aggregator,
  final Materialized<K, VR, WindowStore<Bytes, byte[]>> materialized)

Creates a KTable with a given Initializer, Aggregator and Materialized (view of a WindowStore)

count

KTable<Windowed<K>, Long> count()
KTable<Windowed<K>, Long> count(
  final Materialized<K, Long, WindowStore<Bytes, byte[]>> materialized)

Creates a KTable with a given Materialized (view of a WindowStore)

reduce

KTable<Windowed<K>, V> reduce(final Reducer<V> reducer)
KTable<Windowed<K>, V> reduce(
  final Reducer<V> reducer,
  final Materialized<K, V, WindowStore<Bytes, byte[]>> materialized)

Combining record stream (by a grouped key)

Creates a KTable with a given Reducer and Materialized (view of a WindowStore)

Note
TimeWindowedKStreamImpl is the one and only known implementation of the TimeWindowedKStream Contract in Kafka Streams 2.3.0.

results matching ""

    No results matching ""