我正在按键对流进行分组,并尝试按分组的键对值进行汇总。我正在关注streams-developer-guide
我在withValueSerde
上遇到错误。它说:
The method withValueSerde(Serde<Object>) in the type Materialized<Object,Object,StateStore> is not applicable for the arguments (Serde<Long>)
代码:
KStream<String, String> inputStream = builder.stream("input_topic");
KStream<String, Integer> transformedStream = inputStream.map(
(key, value) -> KeyValue.pair(getKey(value), getValue(value)));
KGroupedStream<String, Integer> groupedStream = transformedStream.groupByKey();
KTable<String, Long> aggregatedStream = groupedStream.aggregate(() -> 0L,
(aggKey, newValue, aggValue) -> aggValue + newValue,
Materialized.as("aggregated-stream-store").withValueSerde(Serdes.Long()));
答案 0 :(得分:2)
您需要指定通用类型。 Java无法自动推断它们(如果查看错误消息,它只会说Materialized<Object,Object,StateStore>
指示未知类型):
Materialized.<String, Long, KeyValueStore<Bytes, byte[]>as("aggregated-stream-store")
.withValueSerde(Serdes.Long())