【问题标题】:Getting matchError with custom vertex type in graphx在graphx中使用自定义顶点类型获取matchError
【发布时间】:2020-12-24 16:11:25
【问题描述】:

我正在尝试使用自定义顶点类型创建一个简单的图形。创建成功,但顶点操作因 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

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

【问题讨论】:

    标签: scala apache-spark spark-graphx custom-type


    【解决方案1】:

    尝试匹配对象的类型:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多