假设这是您的测试数据:
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()
您提到了方向过滤,但是每个关系的方向都在图形本身中编码(即从源到目的地)。