简体   繁体   English

如何仅在窗口完成时将窗口聚合结果发送到输出主题?

[英]How to send the windowed aggregations result to the output topic only when window is finished?

I am facing an issue while doing the window aggregations.我在进行窗口聚合时遇到了一个问题。 I want to perform sum of the values per key and the result is sent to the output topic only when window is finished.我想对每个键的值进行求和,并且仅在窗口完成时将结果发送到输出主题。 The problem is that every event in "input" topic will produce an event to "output" topic.问题是“输入”主题中的每个事件都会产生一个“输出”主题的事件。 I would like to publish an event to the output topic only when a window is finished.我想仅在窗口完成时将事件发布到输出主题。 For example if the window is of one minute, send a single event per key per minute.例如,如果窗口为一分钟,则每分钟每个键发送一个事件。 The sample code is as follows:示例代码如下:

.groupByKey()  
.windowedBy(TimeWindows.of(Duration.ofMinutes(2))
.reduce((v1, v2) -> String.valueOf(Integer.parseInt(v1) + Integer.parseInt(v2)))
.suppress(Suppressed.untilWindowCloses(BufferConfig.unbounded()))
.toStream((k,v) -> k.key())
.to("output_topic");

But I am getting the following exception:但我收到以下异常:

Exception in thread "learningtime_application-665cd31a-1957-448b-8cf7-779ab359cfd2-StreamThread-1" org.apache.kafka.streams.errors.ProcessorStateException: task [1_0] Failed to flush state store KSTREAM-REDUCE-STATE-STORE-0000000003 Caused by: java.lang.ClassCastException: class org.apache.kafka.streams.kstream.Windowed cannot be cast to class java.lang.String (org.apache.kafka.streams.kstream.Windowed is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')线程“learningtime_application-665cd31a-1957-448b-8cf7-779ab359cfd2-StreamThread-1”中的异常 org.apache.kafka.streams.errors.ProcessorStateException:任务 [1_0] 无法刷新状态存储 KSTREAM-REDUCE-00003000000引起:java.lang.ClassCastException: class org.apache.kafka.streams.kstream.Windowed 不能转换为 class java.lang.String (org.apache.kafka.streams.kstream.Windowed is in unnamed module of loader ' app'; java.lang.String 位于加载器 'bootstrap' 的模块 java.base 中)

You are hitting a known bug: https://issues.apache.org/jira/browse/KAFKA-9259您遇到了一个已知错误: https : //issues.apache.org/jira/browse/KAFKA-9259

The suppress operator does not correctly pick up the default serdes from the config, ie, it uses the key serde without converting it to windowed-key-serdes. suppress运算符没有正确地从配置中选择默认的 serdes,即它使用密钥 serde 而不将其转换为 windowed-key-serdes。

As a workaround, you need to specify the serdes explicitly in reduce() via Materialized.with(...) .作为一种解决方法,您需要通过Materialized.with(...)reduce()明确指定 serdes。 You pass in plain key and value serdes, and reduce will convert the key-serde into a windowed-key-serde that will than also be passed into suppress() .您传入普通的键和值 serdes, reduce会将键 serde 转换为窗口键 serde,然后也将传递给suppress()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程完成后如何测试结果-Java - how to test result when thread is finished - java Kafka:如何使用(手动),操作,确认和将结果发送到新主题 - Kafka : How to consume, manipulate, acknowledge (manually) and send result to new topic 重新运行测试时如何只输出一个通过/失败结果? - How to only output one Pass/Fail result when test is rerun? 抑制窗口 KTable 的输出时,如何正确实现缓冲区配置? - How do I implement buffer config correctly when suppressing output from a windowed KTable? flink:在窗口流上应用多个聚合 - flink: applying multiple aggregations on a windowed stream 当生产者停止在 Java/Spring 中发送消息时,我如何在 windowedBy+aggregate 中接收最后一个窗口化的 Kafka 消息? - How I receive the last windowed Kafka message in a windowedBy+aggregate when producer stops to send messages in Java/Spring? 如何将窗口大小设置为窗口全屏? - How would I set the window size to windowed fullscreen? 仅在窗口调整大小完成后重新绘制JPanel - Repaint JPanel only after window resize is finished 如何将消息发送到TOPIC然后发送到MDB? - how to send the message to TOPIC then to MDB? 如何仅在doinbackground中的方法完成后才调用onPostExecute? - How to only call onPostExecute when method in doinbackground is finished?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM