如何发送时间窗 KTable 的最终 kafka-streams 聚合结果?

问题描述 投票:0回答:3

我想做的是:

  1. 从数字主题(Long's)中消费记录
  2. 聚合(计数)每个 5 秒窗口的值
  3. 将最终聚合结果发送到另一个主题

我的代码是这样的:

KStream<String, Long> longs = builder.stream(
            Serdes.String(), Serdes.Long(), "longs");

// In one ktable, count by key, on a five second tumbling window.
KTable<Windowed<String>, Long> longCounts = 
            longs.countByKey(TimeWindows.of("longCounts", 5000L));

// Finally, sink to the long-avgs topic.
longCounts.toStream((wk, v) -> wk.key())
          .to("long-counts");

看起来一切都按预期工作,但是聚合被发送到每个传入记录的目标主题。我的问题是如何只发送每个窗口的最终聚合结果?

apache-kafka apache-kafka-streams
3个回答
34
投票

更新 2

使用KIP-825(Apache Kafka 3.3),您可以通过

windowedBy(...)
指定“发射策略”。默认为
EMIT_EAGER
,但您也可以指定
EMIT_FINAL
在窗口关闭时(即在点
window-end + grace-period
.

)仅获得每个键和窗口的单个结果

更新一

使用 KIP-328(Apache Kafka 2.1),添加了一个

KTable#suppress()
运算符,这将允许以严格的方式抑制连续更新并为每个窗口发出单个结果记录;权衡是增加延迟。

原答案

在 Kafka Streams 中没有“最终聚合”这样的东西。窗口始终保持打开状态,以处理窗口结束时间过后到达的无序记录。但是,窗口不会永远保留。一旦保留时间到期,它们就会被丢弃。没有关于何时丢弃窗口的特殊操作。

有关详细信息,请参阅 Confluent 文档:http://docs.confluent.io/current/streams/

因此,对于聚合的每次更新,都会产生一个结果记录(因为 Kafka Streams 也会在乱序记录上更新聚合结果)。您的“最终结果”将是最新的结果记录(在窗口被丢弃之前)。根据您的用例,手动重复数据删除将是解决问题的一种方法(使用较低级别的 API,

transform()
process()

这篇博文也可能有帮助:https://timothyrenner.github.io/engineering/2016/08/11/kafka-streams-not-looking-at-facebook.html

另一篇不使用标点符号解决此问题的博客文章:http://blog.inovatrend.com/2018/03/making-of-message-gateway-with-kafka.html


7
投票

从 Kafka Streams 2.1 版开始,您可以使用

suppress
.

提到的 apache Kafka Streams 文档中有一个示例,当用户在一个小时内发生的事件少于三个时,它会发送警报:

KGroupedStream<UserId, Event> grouped = ...;
grouped
  .windowedBy(TimeWindows.of(Duration.ofHours(1)).grace(ofMinutes(10)))
  .count()
  .suppress(Suppressed.untilWindowCloses(unbounded()))
  .filter((windowedUserId, count) -> count < 3)
  .toStream()
  .foreach((windowedUserId, count) -> sendAlert(windowedUserId.window(), windowedUserId.key(), count));

正如 this 答案的更新中提到的,您应该意识到权衡。此外,note suppress() 是基于事件时间的。


0
投票

我遇到了这个问题,但我解决了这个问题,在固定窗口后添加 grace(0) 并使用 Suppressed API

public void process(KStream<SensorKeyDTO, SensorDataDTO> stream) {

        buildAggregateMetricsBySensor(stream)
                .to(outputTopic, Produced.with(String(), new SensorAggregateMetricsSerde()));

    }

private KStream<String, SensorAggregateMetricsDTO> buildAggregateMetricsBySensor(KStream<SensorKeyDTO, SensorDataDTO> stream) {
        return stream
                .map((key, val) -> new KeyValue<>(val.getId(), val))
                .groupByKey(Grouped.with(String(), new SensorDataSerde()))
                .windowedBy(TimeWindows.of(Duration.ofMinutes(WINDOW_SIZE_IN_MINUTES)).grace(Duration.ofMillis(0)))
                .aggregate(SensorAggregateMetricsDTO::new,
                        (String k, SensorDataDTO v, SensorAggregateMetricsDTO va) -> aggregateData(v, va),
                        buildWindowPersistentStore())
                .suppress(Suppressed.untilWindowCloses(unbounded()))
                .toStream()
                .map((key, value) -> KeyValue.pair(key.key(), value));
    }


    private Materialized<String, SensorAggregateMetricsDTO, WindowStore<Bytes, byte[]>> buildWindowPersistentStore() {
        return Materialized
                .<String, SensorAggregateMetricsDTO, WindowStore<Bytes, byte[]>>as(WINDOW_STORE_NAME)
                .withKeySerde(String())
                .withValueSerde(new SensorAggregateMetricsSerde());
    }

在这里你可以看到结果

© www.soinside.com 2019 - 2024. All rights reserved.