Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka 访问Kafka Streams中聚合器内的时间窗口属性_Apache Kafka_Apache Kafka Streams - Fatal编程技术网

Apache kafka 访问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)

我想用Kafka Streams在一个时间窗口内对一个主题的最新记录进行流式处理,并且我想将输出记录的时间戳设置为等于记录注册的时间窗口的结束

我的问题是,我无法在聚合器内访问窗口属性

以下是我目前掌握的代码:

    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();
}