以下代码,虽然理论上不是很通用,但可视化来自 GraphFrame 对象的数据(不要在巨型图上使用它)。但是,尽管它生成正确的数据,在 UI 中可见,但在可视化过程中存在 JavaScript 错误(我尝试过 0.9.0-preview1) - 我创建了ZEPPELIN-4793来处理它。
import org.graphframes._
import org.apache.spark.sql.functions._
def graphframeToNetworkText(g: GraphFrame, vertexLabel: String, edgeLabel: String): String = {
val v = g.vertices
val vDataFields = v.schema.fields.map{_.name}.filterNot(_ == "id").map(col(_))
val vJson = v.select(col("id"), lit(vertexLabel).as("label"),
struct(vDataFields: _*).as("data"))
.toJSON.collect.mkString("[",", ","]")
val e = g.edges
val eDataFields = e.schema.fields.map{_.name}
.filterNot(x => x == "src" || x == "dst").map(col(_))
val timeUUID = udf(() => java.util.UUID.randomUUID().toString)
val eJson = e.select(col("src").as("source"),
col("dst").as("target"),
lit(edgeLabel).as("label"),
struct(eDataFields: _*).as("data"))
.withColumn("id", timeUUID())
.toJSON.collect.mkString("[",", ","]")
val sb = new StringBuilder
sb.append("%network {")
.append("\n\"nodes\": ")
.append(vJson)
.append(",\n\"edges\": ")
.append(eJson)
.append(",\n\"directed\": false,\n\"types\":[\"")
.append(edgeLabel)
.append("\"],\n\"labels\":{\"")
.append(vertexLabel)
.append("\": \"#3071A9\"}\n}")
sb.toString()
}
可以这样调用:
val g: GraphFrame = examples.Graphs.friends
print(graphframeToNetworkText(g, "person", "rel"))