Apache kafka 如何将日期和时间戳添加到kafka流输出
我目前正在聚合卡夫卡流并将计数发送到一个主题。我还需要添加计数的日期和时间戳。最好的方法是什么 以下是我使用的配置:Apache kafka 如何将日期和时间戳添加到kafka流输出,apache-kafka,apache-kafka-streams,apache-kafka-connect,Apache Kafka,Apache Kafka Streams,Apache Kafka Connect,我目前正在聚合卡夫卡流并将计数发送到一个主题。我还需要添加计数的日期和时间戳。最好的方法是什么 以下是我使用的配置: streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 60 * 1000); 代码: 您可以使用KStreamtransform获取时间戳: 请注意,此时间戳是触发计算的输入记录的时间戳。我想这就是你要找的 否则,如果您只需要使用System.currentTimeMillis之类的工具转换计
streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 60 * 1000);
代码:
您可以使用KStreamtransform获取时间戳: 请注意,此时间戳是触发计算的输入记录的时间戳。我想这就是你要找的
否则,如果您只需要使用System.currentTimeMillis之类的工具转换计数,而不需要记录中的时间戳,正如Matthias在其评论中指出的那样,您可以使用KTablemapValue。您尝试过KTablemapValues吗?谢谢Dmitry,这有助于获得时间戳。我编辑了我的帖子,以包含代码和输出,虽然我使用groupby,但不确定为什么它会同时打印计数。你知道为什么会发生这种情况吗?Hi Dmittry还没有,尽管我正在分组,但我正在重复看到相同时间戳的计数。不确定为什么?有什么想法吗?
final KTable<Windowed<String>, Long> aggregated = feeds
.map((key, value) -> new KeyValue<>(value.getUserId().toString(), value))
.groupByKey()
.count(TimeWindows.of(TimeUnit.MINUTES.toMillis(1),STATE_STORE);
// We want to compute the total count of users, so we must re-key all records to the same key.
aggregated.selectKey((k, v) -> "user_count as of ")
.transform(() -> new Transformer<String, Long, KeyValue<String, Long>>() {
private ProcessorContext context;
@Override
public void init(ProcessorContext context) {
this.context = context;
}
@Override
public KeyValue<String, Long> transform(String key, Long value) {
long timestamp = context.timestamp();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm.ss");
key = key + sdf.format(timestamp);
// transform value using timestamp
return new KeyValue<>(key, value);
}
@Override
public KeyValue<String, Long> punctuate(long timestamp) {
return null;
}
@Override
public void close() {
}
})
.groupByKey(stringSerde, longSerde)
.count("test_store1").toStream()
.print();
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32541
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32542
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32543
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32544
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32545
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32546
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32547
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32548
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32549
[KTABLE-TOSTREAM-0000000015]:user_count as of 2018-02-02 12:38 , 32550
table
.toStream()
.transform(() -> new Transformer<Object, Object, KeyValue<Object,Object>>() {
private ProcessorContext context;
@Override
public void init(ProcessorContext context) {
this.context = context;
}
@Override
public KeyValue<Object, Object> transform(Object key, Object value) {
long timestamp = context.timestamp();
// transform value using timestamp
return new KeyValue<>(key, value);
}
@Override
public KeyValue<Object, Object> punctuate(long timestamp) {
return null;
}
@Override
public void close() {
}
})
.to("output");