1

编译错误:

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 具有以下推荐的可用签名:

在此处输入图像描述

4

1 回答 1

2

请检查您导入哪个包以使用 Optional。Spark 使用 com.google.common.base.Optional 而不是 jdk 默认包 java.util.Optional。

于 2015-08-28T09:22:05.863 回答