Spark Java - 为什么Tuple2不能成为map中函数的参数?

时间:2017-03-01 09:44:09

标签: java apache-spark

我是Spark的新手。我编写了以下Java代码

JavaPairRDD<Detection, Detection> allCombinations = currentLevelNodes.cartesian(nextLevelNodes);

allCombinations.map(new Function<Tuple2<Detection, Detection>, Segment>(){
  public Segment call(Tuple2<Detection, Detection> combination){
     Segment segment = new Segment();
     Detection a = combination._1();
     Detection b = combination._2();
     segment.distance = Math.sqrt( Math.pow((a.x)-(b.x), 2) + Math.pow((a.y)-(b.y), 2) );

     return segment;
  }
});


IDE(Eclipse Neon)显示以下消息

Multiple markers at this line
- The method map(Function<Tuple2<Detection,Detection>,R>) in the type 
 AbstractJavaRDDLike<Tuple2<Detection,Detection>,JavaPairRDD<Detection,Detection>> is not applicable for the arguments (new 
 Function<Tuple2<Detection,Detection>,Segment>(){})
- The type new Function<Tuple2<Detection,Detection>,Segment>(){} must implement the inherited abstract method 
 Function<Tuple2<Detection,Detection>,Segment>.apply(Tuple2<Detection,Detection>)


如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我使用了Lambda表达式并解决了这个问题。

这是我的示例代码

JavaPairRDD<Detection,Detection> allCombinations = currentLevelNodes.cartesian(nextLevelNodes);

JavaRDD<Segment> segmentRDD = allCombinations.map(tuple -> new Segment(tuple._1, tuple._2));


然后我添加了构造函数

public Segment(Detection a, Detection b){
  this.distance = Math.sqrt(Math.pow(a.x-b.x, 2)+Math.pow(a.y-b.y, 2));
}


它的作品。