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

Efficient way to create relationships in neo4j

我有一个 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 次查询以获取节点以形成关系 + 关系创建。鉴于数据库中当前存在节点,是否有更有效的方法?

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

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

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