类型为Materialized <>的withValueSerde()方法不适用

时间:2018-06-26 10:24:21

标签: apache-kafka-streams

我正在按键对流进行分组,并尝试按分组的键对值进行汇总。我正在关注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()));

1 个答案:

答案 0 :(得分:2)

您需要指定通用类型。 Java无法自动推断它们(如果查看错误消息,它只会说Materialized<Object,Object,StateStore>指示未知类型):

Materialized.<String, Long, KeyValueStore<Bytes, byte[]>as("aggregated-stream-store")
    .withValueSerde(Serdes.Long())