py2neo 2021.0.1 文档
https://pypi.org/project/py2neo/
g = Graph('http://localhost:7474/',auth=("neo4j", "password"))
一键删除所有实体关系
match (n) detach delete n
py2neo使用参考
https://www.icode9.com/content-4-867163.html
py2neo三元组导入器
"""py2neo导入器- 可识别已导入实体,避免出现同一标签下的 实体重复导入- 支持已pandas批量导入三元组数据- 关系导入修正,可识别已存在实体和自动新增缺损实体"""class neo4jImportExe(object):"""将txt中数据存入neo4j"""def __init__(self):# 定义neo4j对象self.g = Graph('http://localhost:7474/',name="neo4j",password="123456")# 创建实体函数def importNode(self,df,verbose=False):for index,row in df.iterrows():node = Node(row["label"],name=row["name"])node_matcher = NodeMatcher(self.g ).match(row["label"],name=row["name"]).first()if node_matcher is None:self.g .create(node)else:if verbose: print("结点",row["name"],"已存在")# 创建关系函数def importRelation(self,df,verbose=False):for index,row in tqdm(df.iterrows()):data1 = str(row["subjectname"])data2 = str(row["objectname"])label1 = str(row["subjectlabel"])label2 = str(row["objectlabel"])relation = row["relation"]node1 = Node(label1,name=data1)node2 = Node(label2,name=data2)node1_matcher = NodeMatcher(self.g ).match(label1,name=data1).first()node2_matcher = NodeMatcher(self.g ).match(label2,name=data2).first()if node1_matcher is None:self.g .create(node1)else:print("结点",data1,"已存在",end=" ")if node2_matcher is None:self.g .create(node2)else:print("结点",data2,"已存在",end=" ")# 重新获取结点get_node1_matcher = NodeMatcher(self.g ).match(label1, name=data1).first()get_node2_matcher = NodeMatcher(self.g ).match(label2, name=data2).first()re_matcher = RelationshipMatcher(self.g ).match((get_node1_matcher,get_node2_matcher),relation).first()if node1_matcher and node2_matcher and re_matcher is not None:print("关系",relation,"已存在")else:if node1_matcher is None or node2_matcher is None or re_matcher is None:rel = Relationship(get_node1_matcher,relation,get_node2_matcher)elif node2_matcher is None and node1_matcher is None:rel = Relationship(node1,relation,node2)self.g .create(rel)if verbose: print("创建新关系",data1,"--",relation,"--",data2)# 创建三元组def importTriple(self,df_data,verbose=True):pass
