具有自定义值类型和已知状态存储的KStream聚合

时间:2019-06-12 00:16:32

标签: apache-kafka-streams

我正在尝试对具有String类型的键和自定义类型的值的流执行聚合-如下

stream.groupByKey(Grouped.with(Serdes.String(),barSerde))
      .windowedBy(TimeWindows.of(Duration.ofSeconds(30)))
      .aggregate(Foo::new,
                 (String key, Bar bar, Foo foo) -> {
                   foo.updateMap(bar.getKey(), bar.getValue());
                   return foo;
                 }, 
                 Materialized.with(Serdes.String(), fooSerde));

直到开始按如下所述在Materialized函数中指定状态存储之前,我都能获得所需的结果

stream.groupByKey(Grouped.with(Serdes.String(),barSerde))
      .windowedBy(TimeWindows.of(Duration.ofSeconds(30)))
      .aggregate(Foo::new,
                 (String key, Bar bar, Foo foo) -> {
                   foo.updateMap(bar.getKey(), bar.getValue());
                   return foo;
                 }, 
                 Materialized.<String, Foo, KeyValueStore<Bytes, byte[]>>as(storeTopic))
                   .withKeySerde(Serdes.String())
                   .withValueSerde(fooSerde));

我收到一个编译错误

Error:(122, 17) java: no suitable method found for aggregate(Foo::new,(key,daBea[...]an; },org.apache.kafka.streams.kstream.Materialized<java.lang.String,com.test.bean.Foo,org.apache.kafka.streams.state.KeyValueStore<org.apache.kafka.common.utils.Bytes,byte[]>>)
method org.apache.kafka.streams.kstream.TimeWindowedKStream.<VR>aggregate(org.apache.kafka.streams.kstream.Initializer<VR>,org.apache.kafka.streams.kstream.Aggregator<? super java.lang.String,? super com.test.bean.Bar,VR>) is not applicable
  (cannot infer type-variable(s) VR
    (actual and formal argument lists differ in length))
method org.apache.kafka.streams.kstream.TimeWindowedKStream.<VR>aggregate(org.apache.kafka.streams.kstream.Initializer<VR>,org.apache.kafka.streams.kstream.Aggregator<? super java.lang.String,? super com.test.bean.Bar,VR>,org.apache.kafka.streams.kstream.Materialized<java.lang.String,VR,org.apache.kafka.streams.state.WindowStore<org.apache.kafka.common.utils.Bytes,byte[]>>) is not applicable
  (cannot infer type-variable(s) VR
    (argument mismatch; org.apache.kafka.streams.kstream.Materialized<java.lang.String,com.test.bean.Foo,org.apache.kafka.streams.state.KeyValueStore<org.apache.kafka.common.utils.Bytes,byte[]>> cannot be converted to org.apache.kafka.streams.kstream.Materialized<java.lang.String,VR,org.apache.kafka.streams.state.WindowStore<org.apache.kafka.common.utils.Bytes,byte[]>>))

如何用Materialized指定Serdes和stateStore主题?

1 个答案:

答案 0 :(得分:0)

由于您正在执行窗口聚合,因此预期的存储类型不是KeyValueStore,而是WindowStore