Apache kafka 访问Kafka Streams中聚合器内的时间窗口属性
我想用Kafka Streams在一个时间窗口内对一个主题的最新记录进行流式处理,并且我想将输出记录的时间戳设置为等于记录注册的时间窗口的结束 我的问题是,我无法在聚合器内访问窗口属性 以下是我目前掌握的代码:Apache kafka 访问Kafka Streams中聚合器内的时间窗口属性,apache-kafka,apache-kafka-streams,Apache Kafka,Apache Kafka Streams,我想用Kafka Streams在一个时间窗口内对一个主题的最新记录进行流式处理,并且我想将输出记录的时间戳设置为等于记录注册的时间窗口的结束 我的问题是,我无法在聚合器内访问窗口属性 以下是我目前掌握的代码: KS0 .groupByKey() .windowedBy( TimeWindows.of(Duration.ofSeconds(this.periodicity)).grace(Duration.ZERO)
KS0
.groupByKey()
.windowedBy(
TimeWindows.of(Duration.ofSeconds(this.periodicity)).grace(Duration.ZERO)
)
.aggregate(
Constants::getInitialAssetTimeValue,
this::aggregator,
Materialized.<AssetKey, AssetTimeValue, WindowStore<Bytes, byte[]>>as(this.getStoreName()) /* state store name */
.withValueSerde(assetTimeValueSerde) /* serde for aggregate value */
)
.suppress(Suppressed.untilWindowCloses(Suppressed.BufferConfig.unbounded()))
.toStream()
.peek((key, value) -> log.info("key={}, value={}", key, value.toString()))
.to(this.toTopic);
非常感谢你的帮助 您只能通过处理器API操作时间戳。但是,您可以轻松地使用DSL中嵌入的处理器API
对于您的情况,可以在
toStream()
和to()
之间插入transform()
。在转换器中
调用context.forward(key、value、To.all().withTimestamp(…)
设置新的时间戳。此外,您将在结尾处返回null
(null
意味着不发出任何记录,因为您已经为此使用了context.forward
)太好了!这就是我最终所做的,它正如我所期望的那样工作:)
private AssetTimeValue aggregator(AssetKey aggKey, AssetTimeValue newValue, AssetTimeValue aggValue){
// I want to do something like that, but this only works with windowed Keys to which I do
// not have access through the aggregator
// windowEndTime = aggKey.window().endTime().getEpochSecond();
return AssetTimeValue.newBuilder()
.setTimestamp(windowEndTime)
.setName(newValue.getName())
.setValue(newValue.getValue())
.build();
}