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

时间:2018-01-11 16:58:46

标签: scala apache-spark spark-graphx

我有不同属性的顶点。现在我想过滤掉具有特定属性值的那些。以下是我的代码的样子:

<!DOCTYPE html>
<html>
<head>
  <title>js</title>
</head>
<body>
  <canvas id="canvas" width="400" height="300">
    This text is displayed if your browser does not support HTML5 Canvas.
   </canvas>

</body>
</html>

现在我想过滤顶点,属性为// 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) 且其描述不为空,我尝试过以下方式:

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

    Property2
  2. 此代码无效

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

1 个答案:

答案 0 :(得分:1)

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

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

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