使用 Java 在 GraphFrames 中获取最短路径

Getting shortestPaths in GraphFrames with Java

我是 Spark 和 GraphFrames 的新手。

当我想了解 GraphFrame 中的 shortestPaths 方法时,GraphFrames documentation 给了我一个 Scala 中的示例代码,但 Java 中没有。

在他们的文档中,他们提供了以下(Scala 代码):

import org.graphframes.{examples,GraphFrame}
val g: GraphFrame = examples.Graphs.friends  // get example graph

val results = g.shortestPaths.landmarks(Seq("a", "d")).run()
results.select("id", "distances").show()

在 Java 中,我试过:

import org.graphframes.GraphFrames;
import scala.collection.Seq;
import scala.collection.JavaConverters;

GraphFrame g = new GraphFrame(...,...);
Seq landmarkSeq = JavaConverters.collectionAsScalaIterableConverter(Arrays.asList((Object)"a",(Object)"d")).asScala().toSeq();
g.shortestPaths().landmarks(landmarkSeq).run().show();

g.shortestPaths().landmarks(new ArrayList<Object>(List.of((Object)"a",(Object)"d"))).run().show();

转换为 java.lang.Object 是必要的,因为 API 需要 Seq 或 ArrayList 而我无法传递 ArrayList 来正确编译它。

在运行代码之后,我看到了消息:

Exception in thread "main" org.apache.spark.sql.AnalysisException: You're using untyped Scala UDF, which does not have the input type information. Spark may blindly pass null to the Scala closure with primitive-type argument, and the closure will see the default value of the Java type for the null argument, e.g. `udf((x: Int) => x, IntegerType)`, the result is 0 for null input. To get rid of this error, you could:
1. use typed Scala UDF APIs(without return type parameter), e.g. `udf((x: Int) => x)`
2. use Java UDF APIs, e.g. `udf(new UDF1[String, Integer] { override def call(s: String): Integer = s.length() }, IntegerType)`, if input types are all non primitive
3. set spark.sql.legacy.allowUntypedScalaUDF to true and use this API with caution;

为了遵循3.,我添加了代码:

System.setProperty("spark.sql.legacy.allowUntypedScalaUDF","true");

但情况没有改变。

由于Java中关于GraphFrames的示例代码或Whosebug问题数量有限,我四处寻找也没有找到任何有用的信息。

有这方面经验的人可以帮我解决这个问题吗?

这似乎是 GraphFrames 0.8.0 中的错误。

参见 github.com

中的 Issue #367