MapValues

mapValues() maps each vertex attribute, preserving the index so as not to change the vertexId. Changing the vertexId would have changed the index so much that subsequent operations would fail and the vertices will not be reachable anymore. Hence, it is important to not change the vertexIds.

The declaration of this function is shown here:

def mapValues[VD2: ClassTag](f: VD => VD2): VertexRDD[VD2]
//A variant of the mapValues() function accepts a vertexId in addition
to the vertices.
def mapValues[VD2: ClassTag](f: (VertexId, VD) => VD2): VertexRDD[VD2]

mapValues() can also operate on the edges and maps the values in an edge partitioning preserving the structure but changing the values:

def mapValues[ED2: ClassTag](f: Edge[ED] => ED2): EdgeRDD[ED2]

The following is the example code invoking mapValues() in the vertices and edges. MapValues on vertices transforms the vertices to list of pairs of (vertexId , User.name). MapValues on edges transforms the edges to triplets of (srcId, dstId, string):

scala> graph.vertices.mapValues{(id, u) => u.name}.take(10)
res142: Array[(org.apache.spark.graphx.VertexId, String)] = Array((4,Liz), (6,Beth), (8,Mary), (10,Ken), (2,Mark), (1,John), (3,Sam), (7,Larry), (9,Dan), (5,Eric))

scala> graph.edges.mapValues(x => s"${x.srcId} -> ${x.dstId}").take(10)
7), Edge(3,1,3 -> 1), Edge(3,2,3 -> 2), Edge(3,10,3 -> 10), Edge(4,7,4 -> 7))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.233.26