1.公式模式是语法范式
2.代码模式是实例,可按顺序复制执行
3.该教程全部使用py2neo的内置方法,不使用run运行neo4j
4.涉及所有可能的节点、节点类型、属性,关系,关系类型、属性的增删改查
import py2neo
from py2neo import Graph, Node, Relationship, RelationshipMatcher, NodeMatcher
graph = Graph("http://localhost:7474",user ="",password='')
# 用pycharm打开neo4j工作台连接,默认的端口号就是7474,输入自己的账号密码
节点 = N o d e ( 节点标签 , 属性名 1 = 属性值 1 , 属性名 2 = 属性值 2 ) 节点=Node(节点标签, 属性名1=属性值1, 属性名2=属性值2) 节点=Node(节点标签,属性名1=属性值1,属性名2=属性值2)
node1 = Node('Person', name='tony', age=25, weight=55)
node2 = Node('Person', name='tom', age=23, weight=50, hight=170)
node3 = Node('Person','male','Suspect', name='peter', age=22, weight=48, hight=165)
node4 = Node('Person','female',name='lisa', age=21, weight=46)
node5 = Node('Person')
关系 = R e l a t i o n s h i p ( 节点 1 , 关系类型 , 节点 2 , 属性 1 = 属性值 1 , 属性 2 = 属性值 2 ) 关系=Relationship(节点1,关系类型,节点2,属性1=属性值1,属性2=属性值2) 关系=Relationship(节点1,关系类型,节点2,属性1=属性值1,属性2=属性值2)
relation1 = Relationship(node1, 'KNOW', node2)
relation2 = Relationship(node1, 'KNOW', node3, strong=85)
relation3 = Relationship(node1, 'COUPLE', node4, value=90)
relation4 = Relationship(node4, 'MATE', node2, strong=40)
relation5 = Relationship(node2, 'KNOW', node3, strong=60)
g r a p h . c r e a t e ( 节点 ) graph.create(节点) graph.create(节点)
graph.create(node1)
graph.create(node2)
graph.create(node3)
graph.create(node4)
graph.create(node5)
g r a p h . c r e a t e ( 关系 ) graph.create(关系) graph.create(关系)
graph.create(relation1)
graph.create(relation2)
graph.create(relation3)
graph.create(relation4)
graph.create(relation5)
节点 [ 属性名 ] = 属性值 g r a p h . p u s h ( 节点 ) 节点[属性名]=属性值\\ graph.push(节点) 节点[属性名]=属性值graph.push(节点)
node1['hight']=190
node1['age']=26
graph.push(node1)
d e l 节点 [ 属性名 ] g r a p h . p u s h ( 节点 ) del \quad 节点[属性名]\\ graph.push(节点) del节点[属性名]graph.push(节点)
del node1['age']
graph.push(node1)
节点 . a d d _ l a b e l ( 标签 1 ) g r a p h . p u s h ( n o d e 1 ) 节点 . r e m o v e _ l a b e l ( 标签 1 ) g r a p h . p u s h ( n o d e 1 ) 节点.add\_label(标签1)\\ graph.push(node1)\\ 节点.remove\_label(标签1)\\ graph.push(node1) 节点.add_label(标签1)graph.push(node1)节点.remove_label(标签1)graph.push(node1)
node1.add_label('male')
graph.push(node1)
node3.remove_label('Suspect')
graph.push(node3)
节点 . u p d a t e _ l a b e l s ( [ 标签 1 , 标签 2 ] ) g r a p h . p u s h ( 节点 ) 节点.update\_labels( [标签1, 标签2] )\\ graph.push(节点) 节点.update_labels([标签1,标签2])graph.push(节点)
node2.update_labels(['male','Suspect'])
graph.push(node2)
g r a p h . d e l e t e ( 节点 ) # 把关系一同删除 graph.delete(节点) \quad\# 把关系一同删除 graph.delete(节点)#把关系一同删除
graph.delete(node5)
关系 [ 属性名 ] = 属性值 g r a p h . p u s h ( 关系 ) 关系[属性名] = 属性值\\ graph.push(关系) 关系[属性名]=属性值graph.push(关系)
relation2['strong']=90
graph.push(relation2)
d e l 关系 [ 属性名 ] g r a p h . p u s h ( 关系 ) del \quad 关系[属性名]\\ graph.push(关系) del关系[属性名]graph.push(关系)
del relation3['value']
graph.push(relation2)
r e l a t i o n = R e l a t i o n s h i p M a t c h e r ( g r a p h ) 关系组 = r e l a t i o n . m a t c h ( n o d e s = [ 节点 1 , 节点 2 ] , r _ t y p e = 关系类型 , l i m i t = N o n e ) f o r 关系 i n 关系组 : g r a p h . s e p e r a t e ( 关系 ) relation = RelationshipMatcher(graph)\\ 关系组 = relation.match(nodes=[节点1, 节点2], r\_type=关系类型, limit=None)\\ for\ \ 关系 \ \ in \ \ 关系组:\\ graph.seperate(关系) relation=RelationshipMatcher(graph)关系组=relation.match(nodes=[节点1,节点2],r_type=关系类型,limit=None)for 关系 in 关系组:graph.seperate(关系)
relation_matcher = RelationshipMatcher(graph) # 生成关系匹配器
#;例子1 删除所有couple关系
relations = relation_matcher.match(r_type='COUPLE') # 可迭代对象
for relation in relations:
graph.separate(relation)
# 例子2 删除node1和node2的关系
relations = relation_matcher.match([node1,node2]) # 可迭代对象
for relation in relations:
graph.separate(relation)
g r a p h . d e l e t e ( 关系 ) graph.delete(关系) graph.delete(关系)
graph.delete(relation2) # 删除tony和peter关系和节点
# 这个就不示范了
(1) 根据特定属性值查询,结果为字典构成的可迭代:
n o d e _ m a t c h e r = N o d e M a t c h e r ( g r a p h ) n o d e _ m a t c h e r . m a t c h ( 节点类型 1 , 节点类型 2 , 属性 1 = 属性值 1 , 属性 2 = 属性值 2 ) node\_matcher = NodeMatcher(graph)\\ node\_matcher.match(节点类型1, 节点类型2, 属性1=属性值1, 属性2=属性值2)\\ node_matcher=NodeMatcher(graph)node_matcher.match(节点类型1,节点类型2,属性1=属性值1,属性2=属性值2)
node_matcher = NodeMatcher(graph) # 节点匹配器
a = node_matcher.match('female',age=21) # 提取满足属性值的节点
(2) 根据复杂规则遍历查找特定条件的节点,结果为字典构成的可迭代:
n o d e s = n o d e _ m a t c h e r . m a t c h ( ) f o r n o d e i n n o d e s : 规则 nodes = node\_matcher.match()\\ for\ \ node\ \ in\ \ nodes:\\ 规则 nodes=node_matcher.match()for node in nodes:规则
nodes = node_matcher.match() # 直接提取所有节点
for node in nodes:
if node['weight'] < 50:
print(node)
根据关系属性特定值匹配,匹配结果为字典构成的可迭代
r e l a t i o n _ m a t c h e r = R e l a t i o n s h i p M a t c h e r ( g r a p h ) 关系 = r e l a t i o n _ m a t c h e r . m a t c h ( r _ t y p e = 关系类型 , 属性 1 = 属性值 1 ) relation\_matcher = RelationshipMatcher(graph)\\ 关系 = relation\_matcher.match(r\_type=关系类型, 属性1=属性值1) relation_matcher=RelationshipMatcher(graph)关系=relation_matcher.match(r_type=关系类型,属性1=属性值1)
relation_matcher = RelationshipMatcher(graph) # 关系匹配器
relations = relation_matcher.match(r_type='KNOW', strong=60) # 提取特别属性的关系
根据复杂规则遍历查找特定条件关系,匹配结果为字典构成的可迭代
r e l a t i o n s = r e l a t i o n _ m a t c h e r . m a t c h ( ) f o r r e l a t i o n i n r e l a t i o n s : 规则 relations = relation\_matcher.match()\\ for \ \ relation\ \ in\ \ relations:\\ 规则 relations=relation_matcher.match()for relation in relations:规则
relations = relation_matcher.match() # 提取所有关系
for relation in relations:
if relation['strong'] > 20:
print(relation)
g r a p h . m a t c h ( n o d e s = [ 节点 1 , 节点 2 ] , r _ t y p e = 关系类型 , l i m i t = N o n e ) graph.match(nodes=[节点1, 节点2], r\_type=关系类型, limit=None) graph.match(nodes=[节点1,节点2],r_type=关系类型,limit=None)
graph.match(nodes=[node1, node3], r_type='KNOW', limit=None)