我有一些想要查询的家族图上的样本数据。
我想在GraphFrames对象上使用find方法来查询motif A->B,其中边是“母亲”类型。
由于GraphFrames使用Neo4J的cypher语言的一个子集,我想知道下面的查询是否正确?
graph.find("(A)-[edge:Mother]->(B)").show或者,在GraphFrames中实现这一点的最佳方式是什么?
GraphFrame(vertex, graph.edges.filter("attr=='Mother'")).vertices.show这不起作用,因为我不能过滤方向,所以我只想得到母亲:)
有什么想法吗?
发布于 2017-09-14 05:01:24
假设这是您的测试数据:
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|
+---+---+------------+您可以使用motif查询并对其应用过滤器:
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()您提到了方向过滤,但每个关系的方向都编码在图本身中(即从源到目标)。
https://stackoverflow.com/questions/44595370
复制相似问题