Reputation: 691
I have two arrays in Scala both with the same numbers
val v = myGraph.vertices.collect.map(_._1)
which gives:
Array[org.apache.spark.graphx.VertexId] = Array(-7023794695707475841, -44591218498176864, 757355101589630892, 21619280952332745)
and another
val w = myGraph.vertices.collect.map(_._2._2)
which gives:
Array[String] = Array(2, 3, 1, 2)
and i want to create a string using
val z = v.map("{id:" + _ + "," + "group:" + "1" + "}").mkString(",")
which gives:
String = {id:-7023794695707475841,group:1},{id:-44591218498176864,group:1},{id:757355101589630892,group:1},{id:21619280952332745,group:1}
But now instead of the hardcoded group of "1", i want to instead map in the numbers from the w array to give:
String = {id:-7023794695707475841,group:2},{id:-44591218498176864,group:3},{id:757355101589630892,group:1},{id:21619280952332745,group:2}
How do i do this?
Upvotes: 0
Views: 1661
Reputation: 9698
There's a method in Scala collections called zip which pairs up two collections just the way you need.
val v = Array(-37581, -44864, 757102, 21625)
val w = Array(2, 3, 1, 2)
val z = v.zip(w).map {
case (v, w) => "{id:" + v + "," + "group:" + w + "}"
}.mkString(",")
Value z
becomes:
{id:-37581,group:2},{id:-44864,group:3},{id:757102,group:1},{id:21625,group:2}
Upvotes: 3