Graphx Scala:在具有属性继承的顶点上应用过滤器 [英] Graphx Scala: Applying filter on Vertex with Property Inheritance
问题描述
我的顶点具有不同的属性.现在,我想过滤掉具有特定属性值的对象.这是我的代码的样子:
I've vertices with different properties. Now I want to filter out those with a specific property value. Here's how my code looks like:
// 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
,并且其描述不为空,我尝试通过以下方式进行操作:
Now I want to filter vertices, which property is Property2
and whose description is not empty, I tried in these ways:
-
没有给出预期的结果
It's not giving expected result
graph.vertices.filter { case (id, (descrition)) => descrition !="" }.foreach{
case (id, (descrition)) => println(s"$descrition")
}
此代码不起作用
This code is not working
graph.vertices.filter { case (id, Property2(descrition)) => descrition !=""}.foreach{
case (id, (descrition)) => println(s"$descrition")
}
推荐答案
您尝试过的代码很接近,但并不完全正确.您需要在 filter
中使用两种情况;一种用于 Property2
,另一种用于所有其他情况.
The code you have tried is close but not exactly correct. You need two cases in the filter
; one for the Property2
and one for all other cases.
val vertices = graph.vertices.filter{
case (id, vp: Property2) => vp.description != ""
case _ => false
}
这将为您提供一个 VertexRDD [VertexProperty]
,其中包含满足要求的所有顶点.
This will give you a VertexRDD[VertexProperty]
with all the vertices that fulfill the requirements.
这篇关于Graphx Scala:在具有属性继承的顶点上应用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!