Python 在neo4j中创建关系的有效方法
我有一个neo4j数据库,其中有数千个节点,没有定义任何关系。我有一个包含节点之间关系的文件,所以我想在数据库中创建的这些节点之间的关系。我目前的做法是:Python 在neo4j中创建关系的有效方法,python,neo4j,py2neo,Python,Neo4j,Py2neo,我有一个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
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()
使用上述方法,您将只加载每个节点一次,并且仅当该节点出现在输入文件中时才加载。您可以为Cypher查询创建一个字符串,然后将其加载。