Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spark Java-为什么Tuple2不能作为映射中函数的参数?_Java_Apache Spark - Fatal编程技术网

Spark Java-为什么Tuple2不能作为映射中函数的参数?

Spark Java-为什么Tuple2不能作为映射中函数的参数?,java,apache-spark,Java,Apache Spark,我是新手。我已经编写了以下Java代码 JavaPairRDD<Detection, Detection> allCombinations = currentLevelNodes.cartesian(nextLevelNodes); allCombinations.map(new Function<Tuple2<Detection, Detection>, Segment>(){ public Segment call(Tuple2<Detecti

我是新手。我已经编写了以下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;
  }
});
javapairdd allcombines=currentLevelNodes.cartesian(nextLevelNodes);
map(新函数(){
公共段呼叫(Tuple2组合){
段=新段();
检测a=组合。_1();
检测b=组合。_2();
段距离=数学sqrt(数学功率((a.x)-(b.x),2)+数学功率((a.y)-(b.y),2));
返回段;
}
});

IDE(EclipseNeon)显示以下消息

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>)
此行有多个标记
-类型中的方法映射(函数)
AbstractJavaRDDLike不适用于参数(新的
函数(){})
-类型new Function(){}必须实现继承的抽象方法
Function.apply(Tuple2)


如何解决此问题?

我使用Lambda表达式解决了此问题

这是我的示例代码

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

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


它可以工作。

Error说您正在实现call(),但您应该实现apply()。使用lambda将使您的生活更轻松。此外,您确定可以使用Tuple2而不是javapairdd作为映射函数的参数吗?我尝试将函数名“call”修改为“apply”,但它仍然显示第一行消息。我将javapairdd转换为JavaRDD并使用map函数,这种情况也存在。希望这个答案能帮助像我这样的人
public Segment(Detection a, Detection b){
  this.distance = Math.sqrt(Math.pow(a.x-b.x, 2)+Math.pow(a.y-b.y, 2));
}