py2neo v4手册:https://py2neo.org/v4/#library-reference
# -*- coding: UTF-8 -*-
from py2neo import Graph, Node, Relationship, walk, NodeMatcher, RelationshipMatcher
import pandas as pd
import json
# 连接数据库 输入地址、用户名、密码
test_graph = Graph(
"http://localhost:7474",
username="neo4j",
password="52151"
)
# 建立节点
test_node1 = Node("人", name="杨露")
test_node2 = Node("人", name="莎莎")
test_node3 = Node('人', name='ss')
# 建立关系
node1_call_node2 = Relationship(test_node1, "喜欢", test_node2)
# print(test_node1, test_node2, node1_call_node2)
node2_call_node1 = Relationship(test_node2, '非常喜欢', test_node1)
# 设置属性 方法1:类似字典的操作 2:setdefault()方法赋值 3:使用update()方法对属性批量更新
test_node1['age'] = 21 # 1
test_node2['age'] = 20 # 1
node1_call_node2['time'] = node2_call_node1['time'] = '20191125' # 1
test_node1['location'] = 'TJ' # 1
test_node1.setdefault('location', '郑州') # 默认属性 2
data = {
'name': 'Amy',
'age': 40
}
test_node3.update(data) # 3
print(test_node1, test_node2, node1_call_node2, test_node3)
# Subgraph 子图,是 Node 和 Relationship 的集合,最简单的构造子图的方式是通过关系运算符
s = test_node1 | test_node2 | node1_call_node2
print(s)
# print(s.keys())
# print(s.labels())
# print(s.nodes())
# print(s.relationships())
# print(s.types())
# Walkable是增加了遍历信息的 Subgraph,我们通过 + 号便可以构建一个 Walkable 对象
a = Node('Person', name='Alice')
b = Node('Person', name='Bob')
c = Node('Person', name='Mike')
ab = Relationship(a, "KNOWS", b)
ac = Relationship(a, "KNOWS", c)
w = ab + Relationship(b, "LIKES", c) + ac
print(w)
for item in walk(w):
print(item)
# 利用 create () 方法传入 Subgraph 对象来将关系图添加到数据库中
test_graph.create(w)
test_graph.create(s)
test_graph.create(node2_call_node1)
# 删
test_node3 = Node("人", name="露露")
test_graph.create(test_node3)
# delete
test_graph.delete(test_node3)
# 添加 或者 修改属性
test_node2['age'] = 20
test_graph.push(test_node2)
# 关系 属性
node1_call_node2['程度'] = '超级'
test_graph.push(node1_call_node2)
# 全部删除
# test_graph.delete_all()
# 图的检索其实是有两种方式的,第一种就是依据节点label属性来搜索,第二种就是依据关系属性来检索。
# 查
data1 = test_graph.run('MATCH (a:人) RETURN a') # 返回的是cursor对象
data1 = data1.data() # 返回的是list
print(data1, type(data1))
# 查节点
print(pd.DataFrame(test_graph.nodes.match('人')))
print(pd.DataFrame(test_graph.nodes.match('人', name='莎莎')))
# 查关系
print(list(test_graph.match(r_type='喜欢')))
# py2neo提供了专门的查询模块 NodeMatcher节点 RelationshipMatcher关系
# ================== 测试NodeMatcher
nodeMatcher = NodeMatcher(test_graph)
node = nodeMatcher.match('人')
print(pd.DataFrame(list(node)))
# 返回列表的第一个节点
node = nodeMatcher.match('人').first()
print(node)
# 返回列表中age为21的节点
node = nodeMatcher.match('人').where(age=21)
print(list(node))
# ================== 测试RelationshipMatcher
node0 = Node('Person', name='Alice')
node1 = Node('Person', name='Bob')
node2 = Node('Person', name='Jack')
node0['age'] = 20
node1['age'] = 25
node2['age'] = 50
node0_know_node1 = Relationship(node1, 'know', node0)
node2_know_node1 = Relationship(node1, 'know', node2)
test_graph.create(node0)
test_graph.create(node1)
test_graph.create(node0_know_node1)
test_graph.create(node2_know_node1)
rlMatcher = RelationshipMatcher(test_graph)
res = rlMatcher.match({node1}, 'know')
print(list(res))
for x in res:
for y in walk(x):
print(y)
print('---------')
for x in res:
for y in walk(x):
if type(y) is Node and y['age'] < 25:
print(y['name'])
print("===========")
# 参考资料:
# https://blog.csdn.net/jian_qiao/article/details/100557985
# http://foreversong.cn/archives/1271
# https://zhuanlan.zhihu.com/p/81175725
# https://cuiqingcai.com/4778.html