py2neo的neo4j数据库增删改查节点node、关系relationship、属性property操作
py2neo版本为4.3.0
设置节点和关系:
# neo4j默认密码是neo4j,如有修改,需要修改第二个‘neo4j’。
graph = Graph("http://localhost:7474", auth=("neo4j", "neo4j"))
a = Node('Man', name='Alice4',desc='2021')
b = Node('Man', name='Bob4',desc='2022')
c = Node('Man', name='Bob4',desc='2023')
r = Relationship(a, 'KNOWS', b)
print(a, b, r)
增添:
# 增加属性
# 节点增加属性
a['age'] = 20
a['location'] = '上海'
b['age'] = 21
c['age'] = 22
# 关系增加属性
r['time'] = '2017/08/31'
# 赋值默认属性
a.setdefault('location','北京')
r.setdefault('location','归属地')
# 属性批量更新
data = {
'name': 'Amy',
'age': 23
}
a.update(data)
# 添加关系relationship
graph.create(r)
y=a|b|r
graph.create(y)
# 添加节点Node
graph.create(c)
print(a, b, r)
# 添加关系
graph.run("match (p:Post),(u:User) \
where p.OwnerUserId = u.Id \
create (u)-[:Own]->(p)")
# 批量添加节点,需修改对应内容
# tx = graph.begin()
# nodes = []
# for line in lineLists:
# oneNode = Node()
# # 这里的循环,一般是把文件的数据存入node中
# nodes.append(oneNode)
# nodes = neo.Subgraph(nodes)
# tx.create(nodes)
# tx.commit()
查询:
print("***查询***")
# 查询节点和关系
s = r+Relationship(b,'LIKE',c)
for item in walk(s):
print(item)
# 查节点
print('***查点***')
data = graph.run('MATCH (p:Man) return p').data()
print(data)
l=list(graph.nodes.match('Man',name="Amy" ))
print(l)
# 查关系,找到所有关系
print("***查关系***")
r=list(graph.match(nodes=None, r_type=None, limit=None))
# 查找一条关系
relationship = graph.match_one(r_type = 'KNOWS')
print(r)
# 查询关系的条数
print(len(r))
# py2neo提供了专门的查询模块 NodeMatcher节点 RelationshipMatcher关系
# 测试NodeMatcher
print("***NodeMatcher***")
nodematcher=NodeMatcher(graph)
# 返回列表
select_nodes=nodematcher.match('Man')
print(list(select_nodes))
# 返回第一个节点
select_nodes=nodematcher.match('Man').first()
print(list(select_nodes))
# 返回年龄为21的label为Man的所有节点
select_nodes=nodematcher.match('Man').where(age=21)
print(list(select_nodes))
# 测试RelationshipMatcher
print("***relationshipmatcher***")
relmatcher=RelationshipMatcher(graph)
# 返回关系属性location为’归属的‘、type为KNOWS的所有关系
res=relmatcher.match(r_type='KNOWS',location = '归属地')
print(list(res))
# 返回与b关系为KNOWS的所有关系
res1=relmatcher.match({b},'KNOWS').limit(None)
print(list(res1))
# 等同于graph.match(nodes=b, r_type=KNOWS, limit=None)
res2=graph.match(nodes={b}, r_type='KNOWS', limit=None)
print(list(res2))
修改:
print("***修改***")
# 修改节点Nodes
alter_data=graph.begin()
nodes_list=list(graph.nodes.match('Man').where(age=21))
for node in nodes_list:
node['desc']='2021.09.15'
# 删除节点的属性
del node['age']
sub=Subgraph(nodes=nodes_list)
# 修改关系
rels_list=list(graph.match(nodes=None,r_type="KNOWS",limit=None))
for rel in rels_list:
rel['time']='2021.09.15'
del rel['time']
sub2=Subgraph(relationships=rels_list)
alter_data.push(sub)
alter_data.push(sub2)
alter_data.commit()
删除:
#在删除Node时必须先删除其对应的Relationship,否则无法删除Node
print("***删除***")
# 删除节点
graph.delete(a)
# 删除标签为phone的所有节点和关系
graph.run('match (n:Man) detach delete n')
# 删除一个节点(根据id)
graph.run('match (r) where id(r) = 63 delete r')
# 删除一个节点(根据属性name)
graph.run('match (n:Man{name:\'Bob4\'}) delete n')
# 删除一个节点及与之相连的关系
graph.run('match (n:Man{name:\'Bob4\'}) detach delete n')
# 删除关系
# 删除type为KNOWS的所有关系
graph.run('match ()-[r:KNOWS]->() delete r;')
# 删除一条type为KNOWS的关系
relationship = graph.match_one(r_type = 'KNOWS')
graph.delete(relationship)
# 删除b节点的type为KNOWS的所有关系
relmatcher=RelationshipMatcher(graph)
res=relmatcher.match({b},'KNOWS')
graph.delete(res)
# 删除标签为Man的节点到标签为Man的节点的所有type为KNOWS的关系
graph.run('match (:Man)-[r:KNOWS]->(:Man) delete r;')
# 可在neo4j上显示关系数:'match (:Man)-[:KNOWS]->(:Man) return count(*);'
# 删除属性
# 专门删除node的某个property,需要配合修改节点操作
# del a['age']
# 删除关系的某个property,需要配合修改关系操作
# del r['LIKES']
# 删除所有
graph.delete_all()
# 等同
graph.run('match (n) detach delete n')
参考:
https://www.jianshu.com/p/febe8a248582
等等,搜索资料时没有记录望谅解。