在graphx中使用自定义顶点类型获取matchError

问题描述

我正在尝试使用自定义顶点类型创建一个简单的图形。创建成功,但由于 matchError 对顶点的操作失败。下面包含复制错误的步骤。

复制步骤(在 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)}

错误

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

请立即指导我创建自定义类型或共享相同的文档。

解决方法

尝试匹配对象的类型:

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