调用updateStateByKey时编译错误

时间:2015-08-03 05:07:47

标签: spark-streaming apache-spark-1.4

编译错误:

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

enter image description here

1 个答案:

答案 0 :(得分:2)

请使用“可选”检查导入的包。 Spark使用com.google.common.base.Optional而不是jdk默认包java.util.Optional。