简体   繁体   English

如何将*窗口化的* KTable实现为Kafka主题

[英]How to materialize a *windowed* KTable into a Kafka topic

I'm writing a KafkaStreams application that takes string values from a topic, where I would like to ouput a concatenation of the values of a certain key of the last 5 minutes, updating every minute into another (compacted) Kafka topic. 我正在编写一个KafkaStreams应用程序,该应用程序从某个主题获取字符串值,在该应用程序中,我想输出最近5分钟某个键的值的串联,将每分钟更新为另一个(紧凑的)Kafka主题。 I have a feeling I'm almost there, but I haven't succeeded yet. 我有种感觉,我快要走了,但还没有成功。 I've tested with a simple: 我已经用一个简单的测试:

grouped_transactions.toStream().foreach((key, value) -> {
    System.out.println(key.window().toString()+ key.key() + "    "+ value);
});

Which gives me something like what you see below (I've filtered by source topic key 00909 to simplify the debugging) What I don't want is all the different Windows with the same concatenated value, I just want my expanding string concatenation. 这给了我类似您在下面看到的内容(我已经通过源主题键00909进行了过滤,以简化调试)我不想要的是具有相同串联值的所有不同Windows,我只希望扩展字符串串联。

Window{start=1525437120000, end=1525437420000}00909    "ABC",-554.53
Window{start=1525437360000, end=1525437660000}00909    "ABC",-554.53
Window{start=1525437240000, end=1525437540000}00909    "ABC",-554.53
Window{start=1525437300000, end=1525437600000}00909    "ABC",-554.53
Window{start=1525437180000, end=1525437480000}00909    "ABC",-554.53
Window{start=1525437120000, end=1525437420000}00909    "ABC",-554.53;"ABC",646.03
Window{start=1525437180000, end=1525437480000}00909    "ABC",-554.53;"ABC",646.03
Window{start=1525437240000, end=1525437540000}00909    "ABC",-554.53;"ABC",646.03
Window{start=1525437300000, end=1525437600000}00909    "ABC",-554.53;"ABC",646.03
Window{start=1525437360000, end=1525437660000}00909    "ABC",-554.53;"ABC",646.03

Below is all the code. 以下是所有代码。 Anyone knows how to do this ? 有人知道该怎么做吗? Thanks in advance! 提前致谢!

Properties props = new Properties();
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());

StreamsBuilder builder = new StreamsBuilder();

long windowSizeMs = TimeUnit.MINUTES.toMillis(5); // 5 * 60 * 1000L
long advanceMs =    TimeUnit.MINUTES.toMillis(1); // 1 * 60 * 1000L
TimeWindows window = TimeWindows.of(windowSizeMs).advanceBy(advanceMs);
KTable<Windowed<String>, String> grouped_transactions = source
        .filter((k,v)->k.equals("00909"))
        .groupByKey()
        .windowedBy(window)
        .reduce((v1, v2) -> v1 + ";" + v2, Materialized.as("grouped_transactions_5_1"));

// THIS FAILS on runtime with
// java.lang.ClassCastException: org.apache.kafka.streams.kstream.Windowed  
// cannot be cast to java.lang.String
grouped_transactions.toStream().to(GROUPEDTRANSACTIONS);


final KafkaStreams streams = new KafkaStreams(builder.build(), props);

What I don't want is all the different Windows with the same concatenated value, I just want my expanding string concatenation. 我不想要所有具有相同串联值的Windows,我只想扩展字符串串联。

Because you specify overlapping windows, a single record can be contained in multiple window instances. 因为您指定了重叠的窗口,所以一条记录可以包含在多个窗口实例中。 Maybe, you want to specify non-overlapping windows, ie, windows with size == advance . 也许您想指定不重叠的窗口,即, size == advance窗口。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM