一種不同的方式來找到val nodesAB
使用GraphOps.collectNeighbors
val nodesAB = graph.collectNeighbors(EdgeDirection.Either)
.filter{case (vid,ns) => ! ns.map(_._2).contains("C2")}.map(_._1)
.intersection(
graph.vertices
.filter{case (vid,attr) => ! attr.toString.startsWith("C") }.map(_._1)
)
其餘的工作你有同樣的方式:
val solution1 = Graph(nodesAB, graph.edges) .
subgraph(vpred = {case(id, label) => label != null})
如果你想使用DataFrames,這可能是更具可擴展性(?) ,那麼首先我們需要將nodesAB變成一個DataFrame:
val newNodes = sqlContext.createDataFrame(
nodesAB,
StructType(Array(StructField("newNode", LongType, false)))
)
而你創造ED和數據框邊緣與此:
val edgeDf = sqlContext.createDataFrame(
graph.edges.map{edge => Row(edge.srcId, edge.dstId, edge.attr)},
StructType(Array(
StructField("srcId", LongType, false),
StructField("dstId", LongType, false),
StructField("attr", LongType, false)
))
)
然後,您可以做到這一點沒有子圖以創建圖表:
val solution1 = Graph(
nodesAB,
edgeDf
.join(newNodes, $"srcId" === $"newNode").select($"srcId", $"dstId", $"attr")
.join(newNodes, $"dstId" === $"newNode")
.rdd.map(row => Edge(row.getLong(0), row.getLong(1), row.getLong(2)))
)
'Edge.attr'是否有用? –