编译错误:
The method updateStateByKey(Function2<List<Integer>,Optional<S>,Optional<S>>) in the type JavaPairDStream<String,Integer> is not applicable for the arguments (Function2<List<Integer>,Optional<Integer>,Optional<Integer>>)
在一个简单的单词计数示例中,使用1
映射单词JavaPairDStream<String, Integer> wordCounts = words.mapToPair(s -> new Tuple2<>(s,1));
然后在updateStateByKey
wordCounts
JavaPairDStream<String, Integer> finalcount = wordCounts.updateStateByKey(updateFunction);
updateFunction
的定义如下:
final Function2<List<Integer>, Optional<Integer>, Optional<Integer>> updateFunction =
new Function2<List<Integer>, Optional<Integer>, Optional<Integer>>() {
@Override
public Optional<Integer> call(List<Integer> values, Optional<Integer> state) {
Integer newSum = state.orElse(0);
for (Integer value : values) {
newSum += value;
}
return Optional.of(newSum);
}
};
updateStateByKey具有以下推荐的可用签名:
答案 0 :(得分:2)
请使用“可选”检查导入的包。 Spark使用com.google.common.base.Optional而不是jdk默认包java.util.Optional。