Graphx Scala:在具有 属性 继承的顶点上应用过滤器

Graphx Scala: Applying filter on Vertex with Property Inheritance

我有不同属性的顶点。现在我想过滤掉具有特定 属性 值的那些。我的代码如下所示:

// Different property classes for different vertex
class VertexProperty()
case class Property1(val name: String, val servings: Int) extends VertexProperty
case class Property2(val description: String) extends VertexProperty
case class Property3(val name: String, val quantity: Double, val measurementUnit: String) extends VertexProperty

val vertexArray = Array(
    (1L, Property1("propertyName",8)),
    (2L, Property2("property description")),
    (3L, Property3("", 1,"lb."))
    )

 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)

现在我想过滤顶点,属性是Property2并且描述不为空,我尝试了这些方法:

  1. 它没有给出预期的结果

    graph.vertices.filter { case (id, (descrition)) => descrition !="" }.foreach{
      case (id, (descrition)) => println(s"$descrition")
    }
    
  2. 此代码无效

    graph.vertices.filter { case (id, Property2(descrition)) => descrition !=""}.foreach{
      case (id, (descrition)) => println(s"$descrition")
    }
    

您试过的代码很接近但不完全正确。 filter; 你需要两个案例一种用于 Property2,另一种用于所有其他情况。

val vertices = graph.vertices.filter{
  case (id, vp: Property2) => vp.description != ""
  case _ => false
}

这将为您提供一个 VertexRDD[VertexProperty],其中包含满足要求的所有顶点。