2017-06-16 65 views
2

我在我想查询的族图上有一些示例数据。GraphFrames上的边缘属性过滤器主题搜索不起作用

我想在GraphFrames对象上使用find方法来查询母题A-> B,其中边的类型是“Mother”。

由于GraphFrames使用Neo4J的cypher语言的子集,我想知道以下是否是正确的查询?

graph.find("(A)-[edge:Mother]->(B)").show 

或者什么是在GraphFrames中实现它的最好方法?

GraphFrame(vertex, graph.edges.filter("attr=='Mother'")).vertices.show 

这不工作,因为我不能对方向滤波器,所以我只希望得到母亲:)

任何想法?

回答

1

假设这是您的测试数据:

import org.graphframes.GraphFrame 

val edgesDf = spark.sqlContext.createDataFrame(Seq(
    ("a", "b", "Mother"), 
    ("b", "c", "Father"), 
    ("d", "c", "Father"), 
    ("e", "b", "Mother")  
)).toDF("src", "dst", "relationship") 

val graph = GraphFrame.fromEdges(edgesDf) 
graph.edges.show() 

+---+---+------------+ 
|src|dst|relationship| 
+---+---+------------+ 
| a| b|  Mother| 
| b| c|  Father| 
| d| c|  Father| 
| e| b|  Mother| 
+---+---+------------+ 

您可以使用一个主题的查询和过滤器适用于它:

graph.find("()-[e]->()").filter("e.relationship = 'Mother'").show() 

+------------+ 
|   e| 
+------------+ 
|[a,b,Mother]| 
|[e,b,Mother]| 
+------------+ 

或者,因为你的情况是比较简单的,你可以申请一个过滤器到图的边缘:

graph.edges.filter("relationship = 'Mother'").show() 

+---+---+------------+ 
|src|dst|relationship| 
+---+---+------------+ 
| a| b|  Mother| 
| e| b|  Mother| 
+---+---+------------+ 

这里有一些替代语法(每个获得与立即在上面):

graph.edges.filter($"relationship" === "Mother").show() 
graph.edges.filter('relationship === "Mother").show() 

你提到过滤方向,但每个关系的方向编码在图本身(即从源到目的地)。