gpt4 book ai didi

apache-spark - GraphFrames 主题搜索的边缘属性过滤器不起作用

转载 作者:行者123 更新时间:2023-12-04 16:09:49 25 4
gpt4 key购买 nike

我有一些关于家庭图的样本数据,我想查询。

我想在 GraphFrames 对象上使用 find 方法来查询边缘类型为“Mother”的主题 A->B。

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

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

或者在 GraphFrames 中实现它的最佳方式是什么?

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

这行不通,因为我无法过滤方向,所以我只想找妈妈们:)

有什么想法吗?

最佳答案

假设这是您的测试数据:

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()

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

关于apache-spark - GraphFrames 主题搜索的边缘属性过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44595370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com