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

2024-04-28 05:46:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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()

目前的方法需要对数据库进行2次查询,以获取节点以形成关系+关系创建。考虑到数据库中当前存在的节点,有没有更有效的方法?在


Tags: 数据库node节点关系lineselectselectorgraph
1条回答
网友
1楼 · 发布于 2024-04-28 05:46:07

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

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

使用上面的方法,您将只加载每个节点一次,并且仅当该节点出现在您的输入文件中时。在

相关问题 更多 >