user2883247
user2883247

Reputation: 81

Getting matchError with custom vertex type in graphx

I am trying to create a simple graph with custom vertex type. Creation is successful but operation on vertices are failing due to the matchError. Below contain the steps to replicate the error.

Steps to replicate (On emr, spark-shell):

import org.apache.spark._
import org.apache.spark.graphx._
// To make some of the examples work we will also need RDD
import org.apache.spark.rdd.RDD
class VertexProperty() extends Serializable
case class UserProperty(val name: String) extends VertexProperty
case class ProductProperty(val name: String, val price: Double) extends VertexProperty
val u1 = new UserProperty("u1")
val u2 = new UserProperty("u2")
val p1 = new ProductProperty("p1", 1.0)
val p2 = new ProductProperty("p2", 2.0)
val users: RDD[(VertexId, VertexProperty)] =   sc.parallelize(Seq( (1L, u1), (2L, u2) ))
val relationships: RDD[Edge[String]] = sc.parallelize(Seq(Edge(1L, 2L, "1-2") ))
val graph = Graph(users, relationships)
val c = graph.vertices.collect()
val d = c(0)._2
d match { case e: UserProperty => print(e.name)}

Error:

scala.MatchError: UserProperty(u1) (of class UserProperty)
  ... 53 elided

Please guide me right away to create custom types or share documentation for the same.

Upvotes: 0

Views: 48

Answers (1)

mck
mck

Reputation: 42352

Try matching the types of the object:

d match {
    case e if e.isInstanceOf[UserProperty] => print(e.asInstanceOf[UserProperty].name)
    case e if e.isInstanceOf[ProductProperty] => print(e.asInstanceOf[ProductProperty].price)
}

Upvotes: 1

Related Questions