My graph contains vertices with different properties classes. I want to filter vertices with a specific property and then sort them. Here is how my code looks like:
class VertexProperty()
case class Property1(val name: String, val servings: Int) extends VertexProperty
case class Property2(val description: String) extends VertexProperty
val vertexArray = Array(
(1L, Property1("propertyName",8)),
(2L, Property1("propertyName",4)),
(3L, Property2("description"))
)
val edgeArray = Array(
Edge(1L, 2L, "step1"),
Edge(1L, 3L, "step2")
)
val vertexRDD: RDD[(Long, VertexProperty)] = sc.parallelize(vertexArray)
val edgeRDD: RDD[Edge[String]] = sc.parallelize(edgeArray)
val graph: Graph[VertexProperty, String] = Graph(vertexRDD, edgeRDD)
I want to get vertices with property1 only and this code is working fine:
val vertices = graph.vertices.filter{
case (id, vp: Property1) => vp.description != ""
case _ => false
}
That is the result:
(1L, Property1("propertyName",8)), (2L, Property1("propertyName",4))
Now, problem is that I want to get these vertices sorted by "servings" that is 2nd parameter of Property1 class. I can sort this result by vertex id:
vertices.collect().sortBy(_._1).foreach(println)
but this don't work.
vertices.collect().sortBy(_._2._2).foreach(println)