2017-06-16 64 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() 

你提到過濾方向,但每個關係的方向編碼在圖本身(即從源到目的地)。