gpt4 book ai didi

python - 在 neo4j 中创建关系的有效方法

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:33 24 4
gpt4 key购买 nike

我有一个 Neo4j 数据库,其中填充了数千个节点,但没有定义任何关系。我有一个包含节点之间关系的文件,因此我想在数据库中创建的这些节点之间创建关系。我目前的做法是:

from py2neo import NodeSelector,Graph,Node,Relationship
graph = Graph('http://127.0.0.1:7474/db/data')
tx = graph.begin()
selector = NodeSelector(graph)
with open("file","r") as relations:
for line in relations:
line_split=line.split(";")
node1 = selector.select("Node",unique_name=line_split[0]).first()
node2 = selector.select("Node",unique_name=line_split[1]).first()
rs = Relationship(node1,"Relates to",node2)
tx.create(rs)
tx.commit()

当前的方法需要两次数据库查询才能获取节点以形成关系+创建关系。鉴于数据库中当前存在节点,是否有更有效的方法?

最佳答案

您可以在填充关系时使用某种形式的节点缓存:

from py2neo import NodeSelector,Graph,Node,Relationship
graph = Graph('http://127.0.0.1:7474/db/data')
tx = graph.begin()
selector = NodeSelector(graph)
node_cache = {}

with open("file","r") as relations:
for line in relations:
line_split=line.split(";")

# Check if we have this node in the cache
if line_split[0] in node_cache:
node1 = node_cache[line_split[0]]
else:
# Query and store for later
node1 = selector.select("Node",unique_name=line_split[0]).first()
node_cache[line_split[0]] = node1

if line_split[1] in node_cache:
node2 = node_cache[line_split[1]]
else:
node2 = selector.select("Node",unique_name=line_split[1]).first()
node_cache[line_split[1]] = node2

rs = Relationship(node1,"Relates to",node2)
tx.create(rs)

tx.commit()

通过上述内容,您只会加载每个节点一次,并且仅当该节点出现在您的输入文件中时。

关于python - 在 neo4j 中创建关系的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49423946/

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