当前位置:   article > 正文

知识图谱—知识存储—py2neo使用_py2neo nodeselector

py2neo nodeselector

    使用py2neo实现python对Neo4j的操作,首先是搭建环境,选用合适的版本对实验的成功至关重要。我选用的版本是Neo4j—3.0.7、py2neo—3.1.2、python2.7。需要注意的是如果使用默认的pip install py2neo则在进行create node时会出现报错问题,因此需选择合适的版本。

       程序代码及注释如下

  1. # -*- coding: utf-8 -*-
  2. from py2neo import Graph, Node, Relationship, NodeSelector
  3. graph = Graph("http://localhost:7474", username="neo4j", password='password')
  4. graph.delete_all()
  5. '''
  6. 1 —— 创建node,函数第一个参数是节点类型,第二个参数是value值
  7. '''
  8. a = Node('PersonTest', name='张三')
  9. b = Node('PersonTest', name='李四')
  10. r = Relationship(a, 'KNOWNS', b)
  11. s = a | b | r
  12. graph.create(s)
  13. '''
  14. 2 —— Node查询
  15. '''
  16. # 用CQL进行查询,返回的结果是list
  17. data1 = graph.data('MATCH(p:PersonTest) return p')
  18. print "data1 = ", data1, type(data1)
  19. print
  20. # 用find_one()方法进行node查找,返回的是查找node的第一个node
  21. data2 = graph.find_one(label='PersonTest', property_key='name', property_value="李四")
  22. print "data2 = ", data2, type(data2)
  23. print
  24. # 用find()方法进行node查找
  25. data3 = graph.find(label='PersonTest')
  26. for data in data3:
  27. print "data3 = ", data
  28. print
  29. '''
  30. 3 —— Relationship查询
  31. '''
  32. relationship = graph.match_one(rel_type='KNOWNS')
  33. print relationship, type(relationship)
  34. print
  35. '''
  36. 4 —— 更新Node的某个属性值,若node没有该属性,则新增该属性
  37. '''
  38. node1 = graph.find_one(label='PersonTest', property_key='name', property_value="张三")
  39. node1['age'] = 21
  40. graph.push(node1)
  41. data4 = graph.find(label='PersonTest')
  42. for data in data4:
  43. print "data4 = ", data
  44. print
  45. node1['age'] = 99
  46. graph.push(node1)
  47. data5 = graph.find(label='PersonTest')
  48. for data in data5:
  49. print "data5 = ", data
  50. print
  51. '''
  52. 5 —— 删除某node,在删除node之前需要先删除relationship
  53. '''
  54. node = graph.find_one(label='PersonTest', property_key='name', property_value="李四")
  55. relationship = graph.match_one(rel_type='KNOWNS')
  56. graph.delete(relationship)
  57. graph.delete(node)
  58. data6 = graph.find(label='PersonTest')
  59. for data in data6:
  60. print "data6 = ", data
  61. '''
  62. 6 —— 多条件查询
  63. '''
  64. a = Node('PersonTest', name='张三', age=21, location='广州')
  65. b = Node('PersonTest', name='李四', age=22, location='上海')
  66. c = Node('PersonTest', name='王五', age=21, location='北京')
  67. r1 = Relationship(a, 'KNOWS', b)
  68. r2 = Relationship(b, 'KNOWS', c)
  69. s = a | b | c | r1 | r2
  70. graph.create(s)
  71. data7 = graph.find(label='PersonTest')
  72. for data in data7:
  73. print "data7 = ", data
  74. print
  75. # 单条件查询,返回的是多个结果
  76. selector = NodeSelector(graph)
  77. persons = selector.select('PersonTest', age=21)
  78. print("data8 = ", list(persons))
  79. print
  80. # 多条件查询
  81. selector = NodeSelector(graph)
  82. persons = selector.select('PersonTest', age=21, location='广州')
  83. print("data9 = ", list(persons))
  84. print
  85. # orderby进行更复杂的查询
  86. selector = NodeSelector(graph)
  87. persons = selector.select('PersonTest').order_by('_.age')
  88. for data in persons:
  89. print "data10 = ", data
  90. print

改代码运行结果如下

data1 =  [{u'p': (`张三`:PersonTest {name:"张三"})}, {u'p': (`李四`:PersonTest {name:"李四"})}] <type 'list'>

data2 =  (`李四`:PersonTest {name:"李四"}) <class 'py2neo.types.Node'>

data3 =  (`张三`:PersonTest {name:"张三"})
data3 =  (`李四`:PersonTest {name:"李四"})

(`张三`)-[:KNOWNS]->(`李四`) <class 'py2neo.types.Relationship'>

data4 =  (`张三`:PersonTest {age:21,name:"张三"})
data4 =  (`李四`:PersonTest {name:"李四"})

data5 =  (`张三`:PersonTest {age:99,name:"张三"})
data5 =  (`李四`:PersonTest {name:"李四"})

data7 =  (`张三`:PersonTest {age:21,location:"广州",name:"张三"})
data7 =  (`李四`:PersonTest {age:22,location:"上海",name:"李四"})
data7 =  (`王五`:PersonTest {age:21,location:"北京",name:"王五"})

('data8 = ', [(`张三`:PersonTest {age:21,location:"广州",name:"张三"}), (`王五`:PersonTest {age:21,location:"北京",name:"王五"})])

('data9 = ', [(`张三`:PersonTest {age:21,location:"广州",name:"张三"})])

data10 =  (`张三`:PersonTest {age:21,location:"广州",name:"张三"})
data10 =  (`王五`:PersonTest {age:21,location:"北京",name:"王五"})
data10 =  (`李四`:PersonTest {age:22,location:"上海",name:"李四"})

下一篇:知识图谱—关系抽取—deepdive 

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

闽ICP备14008679号