当前位置:   article > 正文

py2neo的基本操作_py2neo relationship

py2neo relationship

 py2neo v4手册:https://py2neo.org/v4/#library-reference

  1. # -*- coding: UTF-8 -*-
  2. from py2neo import Graph, Node, Relationship, walk, NodeMatcher, RelationshipMatcher
  3. import pandas as pd
  4. import json
  5. # 连接数据库 输入地址、用户名、密码
  6. test_graph = Graph(
  7. "http://localhost:7474",
  8. username="neo4j",
  9. password="52151"
  10. )
  11. # 建立节点
  12. test_node1 = Node("人", name="杨露")
  13. test_node2 = Node("人", name="莎莎")
  14. test_node3 = Node('人', name='ss')
  15. # 建立关系
  16. node1_call_node2 = Relationship(test_node1, "喜欢", test_node2)
  17. # print(test_node1, test_node2, node1_call_node2)
  18. node2_call_node1 = Relationship(test_node2, '非常喜欢', test_node1)
  19. # 设置属性 方法1:类似字典的操作 2:setdefault()方法赋值 3:使用update()方法对属性批量更新
  20. test_node1['age'] = 21 # 1
  21. test_node2['age'] = 20 # 1
  22. node1_call_node2['time'] = node2_call_node1['time'] = '20191125' # 1
  23. test_node1['location'] = 'TJ' # 1
  24. test_node1.setdefault('location', '郑州') # 默认属性 2
  25. data = {
  26. 'name': 'Amy',
  27. 'age': 40
  28. }
  29. test_node3.update(data) # 3
  30. print(test_node1, test_node2, node1_call_node2, test_node3)
  31. # Subgraph 子图,是 Node 和 Relationship 的集合,最简单的构造子图的方式是通过关系运算符
  32. s = test_node1 | test_node2 | node1_call_node2
  33. print(s)
  34. # print(s.keys())
  35. # print(s.labels())
  36. # print(s.nodes())
  37. # print(s.relationships())
  38. # print(s.types())
  39. # Walkable是增加了遍历信息的 Subgraph,我们通过 + 号便可以构建一个 Walkable 对象
  40. a = Node('Person', name='Alice')
  41. b = Node('Person', name='Bob')
  42. c = Node('Person', name='Mike')
  43. ab = Relationship(a, "KNOWS", b)
  44. ac = Relationship(a, "KNOWS", c)
  45. w = ab + Relationship(b, "LIKES", c) + ac
  46. print(w)
  47. for item in walk(w):
  48. print(item)
  49. # 利用 create () 方法传入 Subgraph 对象来将关系图添加到数据库中
  50. test_graph.create(w)
  51. test_graph.create(s)
  52. test_graph.create(node2_call_node1)
  53. # 删
  54. test_node3 = Node("人", name="露露")
  55. test_graph.create(test_node3)
  56. # delete
  57. test_graph.delete(test_node3)
  58. # 添加 或者 修改属性
  59. test_node2['age'] = 20
  60. test_graph.push(test_node2)
  61. # 关系 属性
  62. node1_call_node2['程度'] = '超级'
  63. test_graph.push(node1_call_node2)
  64. # 全部删除
  65. # test_graph.delete_all()
  66. # 图的检索其实是有两种方式的,第一种就是依据节点label属性来搜索,第二种就是依据关系属性来检索。
  67. # 查
  68. data1 = test_graph.run('MATCH (a:人) RETURN a') # 返回的是cursor对象
  69. data1 = data1.data() # 返回的是list
  70. print(data1, type(data1))
  71. # 查节点
  72. print(pd.DataFrame(test_graph.nodes.match('人')))
  73. print(pd.DataFrame(test_graph.nodes.match('人', name='莎莎')))
  74. # 查关系
  75. print(list(test_graph.match(r_type='喜欢')))
  76. # py2neo提供了专门的查询模块 NodeMatcher节点 RelationshipMatcher关系
  77. # ================== 测试NodeMatcher
  78. nodeMatcher = NodeMatcher(test_graph)
  79. node = nodeMatcher.match('人')
  80. print(pd.DataFrame(list(node)))
  81. # 返回列表的第一个节点
  82. node = nodeMatcher.match('人').first()
  83. print(node)
  84. # 返回列表中age为21的节点
  85. node = nodeMatcher.match('人').where(age=21)
  86. print(list(node))
  87. # ================== 测试RelationshipMatcher
  88. node0 = Node('Person', name='Alice')
  89. node1 = Node('Person', name='Bob')
  90. node2 = Node('Person', name='Jack')
  91. node0['age'] = 20
  92. node1['age'] = 25
  93. node2['age'] = 50
  94. node0_know_node1 = Relationship(node1, 'know', node0)
  95. node2_know_node1 = Relationship(node1, 'know', node2)
  96. test_graph.create(node0)
  97. test_graph.create(node1)
  98. test_graph.create(node0_know_node1)
  99. test_graph.create(node2_know_node1)
  100. rlMatcher = RelationshipMatcher(test_graph)
  101. res = rlMatcher.match({node1}, 'know')
  102. print(list(res))
  103. for x in res:
  104. for y in walk(x):
  105. print(y)
  106. print('---------')
  107. for x in res:
  108. for y in walk(x):
  109. if type(y) is Node and y['age'] < 25:
  110. print(y['name'])
  111. print("===========")
  112. # 参考资料:
  113. # https://blog.csdn.net/jian_qiao/article/details/100557985
  114. # http://foreversong.cn/archives/1271
  115. # https://zhuanlan.zhihu.com/p/81175725
  116. # https://cuiqingcai.com/4778.html

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/647916
推荐阅读
相关标签
  

闽ICP备14008679号