I take your question for setting VertexId in the original graph, creating a new graph with the nodes and edges associated with this VertexId from the original graph.
Given what I will do:
val targetVertexId = ... val graph = Graph(..., ...) val newGraph = Graph( graph.vertices.filter{case (vid,attr) => vid == targetVertexId} ++ graph.collectNeighbors(EdgeDirection.Either) .filter{ case (vid,arr) => vid == targetVertexId} .flatMap{ case (vid,arr) => arr}, graph.edges ).subgraph(vpred = { case (vid,attr) => attr != null})
A few notes:
You can change EdgeDirection.Either to EdgeDirection.In or EdgeDirection.Out as needed.
At the end of .subgraph , all vertices for which the attribute is null are deleted. If the original val graph has Vertices with attributes set to null , this will not work. Otherwise, this works without first recognizing the type of the Vertex attribute.
David Griffin
source share