用java在graphframes中获取最短路径

fae0ux8s  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(861)

我不熟悉Spark和笔架。
当我想学习graphframe中的shortestpaths方法时,graphframes文档给了我一个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中关于GraphFrame的示例代码或stackoverflow问题的数量有限,因此在四处查找时,我找不到任何有用的信息。
在这方面有经验的人能帮我解决这个问题吗?

kulphzqa

kulphzqa1#

这似乎是graphframes0.8.0中的一个bug。
参见github.com上的第367期

相关问题