kxg916361108
kxg916361108
采纳率0%
2017-08-01 07:37 阅读 1.2k

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

30

代码如下(网上摘录代码):
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条回答 默认 最新

  • kxg916361108 kxg916361108 2017-08-01 07:38

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

    点赞 评论 复制链接分享
  • kxg916361108 kxg916361108 2017-08-02 01:55

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

    点赞 评论 复制链接分享
  • u014034934 逗比123号 2017-08-28 09:13

    试试mapWithState方法

    点赞 评论 复制链接分享
  • mrd657706331 小鱼骨头 2017-12-27 09:18

    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);
    }
    });
    你那个参数写的方式

    点赞 评论 复制链接分享

相关推荐