spark2.0报错求大神帮忙!!!谢谢!! 30C

代码如下(网上摘录代码):
package com.gree.test;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import com.google.common.base.Optional;
import scala.Tuple2;
public class OnlineWordCount {
public static void main(String[] args) {
SparkConf conf = new SparkConf().setAppName("wordcount").setMaster("local[2]");
JavaStreamingContext jssc = new JavaStreamingContext(conf,Durations.seconds(5));
jssc.checkpoint("hdfs://spark001:9000/wordcount_checkpoint");
JavaReceiverInputDStream lines = jssc.socketTextStream("spark001", 9999);
JavaDStream words = lines.flatMap(new FlatMapFunction(){
private static final long serialVersionUID = 1L;
@Override
public Iterator call(String line) throws Exception {
return Arrays.asList(line.split(" ")).iterator();
}
});
JavaPairDStream pairs = words.mapToPair(new PairFunction(){
private static final long serialVersionUID = 1L;
@Override
public Tuple2 call(String word) throws Exception {
return new Tuple2(word, 1);
}
});
JavaPairDStream wordcounts = pairs.updateStateByKey(
new Function2, Optional, Optional>(){
private static final long serialVersionUID = 1L;
@Override
public Optional call(List values, Optional state) throws Exception {
Integer newValue = 0;
if(state.isPresent()){
newValue = state.get();
}
for(Integer value : values){
newValue += value;
}
return Optional.of(newValue);
}
});
wordcounts.print();
jssc.start();
try {
jssc.awaitTermination();
} catch (InterruptedException e) {
e.printStackTrace();
}
jssc.close();
}

}

报错位置为updateStateByKey位置:
The method updateStateByKey(Function2,Optional,Optional>) in the type JavaPairDStream is not applicable for the arguments (new Function2,Optional,Optional>(){})

跪求大神解决。。。谢谢

4个回答

同样的代码在1.6能够正常运行。。但是到2.0就包下面的错。。本身未引入多余Jar包我。。。

大神们能不能帮忙解决下啊!!!急急急!!!!!!

试试mapWithState方法

JavaPairDStream updatekey = wordcount.updateStateByKey(new Function2, Optional, Optional>() {
public Optional call(List values, Optional state) throws Exception {
int sum=0;
if(state.isPresent()){
sum=state.get();
}
for(Integer one:values){
sum+=one;
}
return Optional.of(sum);
}
});
你那个参数写的方式

kxg916361108
kxg916361108 已解决。。。不是参数的问题。。jar包变了。谢谢哇!
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问