python操作neo4j

Neo4j是图数据库,即数据不是保存在表或集合中,而是保存为节点以及节点之间的关系。Neo4j数据主要由节点、边、属性构成。

在Neo4j中,节点以及边都能够包含保存值的属性
1、安装
py2neo的安装:
pip install py2neo
2、例子1


   
   
  1. #coding:utf-8
  2. from py2neo import Graph,Node,Relationship
  3. ##连接neo4j数据库,输入地址、用户名、密码
  4. graph = Graph('http://localhost:7474',username='neo4j',password='test')
  5. ##创建结点
  6. test_node_1 = Node(label='ru_yi_zhuan',name='皇帝')
  7. test_node_2 = Node(label='ru_yi_zhuan',name='皇后')
  8. test_node_3 = Node(label='ru_yi_zhuan',name='公主')
  9. graph.create(test_node_1)
  10. graph.create(test_node_2)
  11. graph.create(test_node_3)
  12. ##创建关系
  13. #分别建立了test_node_1指向test_node_2和test_node_2指向test_node_1两条关系,关系的类型为"丈夫、妻子",两条关系都有属性count,且值为1。
  14. node_1_zhangfu_node_1 = Relationship(test_node_1,'丈夫',test_node_2)
  15. node_1_zhangfu_node_1['count'] = 1
  16. node_2_qizi_node_1 = Relationship(test_node_2,'妻子',test_node_1)
  17. node_2_munv_node_1 = Relationship(test_node_2,'母女',test_node_3)
  18. node_2_qizi_node_1['count'] = 1
  19. graph.create(node_1_zhangfu_node_1)
  20. graph.create(node_2_qizi_node_1)
  21. graph.create(node_2_munv_node_1)
  22. print(graph)
  23. print(test_node_1)
  24. print(test_node_2)
  25. print(node_1_zhangfu_node_1)
  26. print(node_2_qizi_node_1)
  27. print(node_2_munv_node_1)

打印出来的结果如下所示:

注意一下:

想要使该程序代码执行起来,必须将neo4j启动,启动成功之后如下所示:

不启动的话,程序会报错

 

在其他博客中,看到查询代码,特地试了一下:


   
   
  1. find_code_1 = test_graph.find_one(
  2. label="ru_yi_zhuan",
  3. property_key="name",
  4. property_value="test_node_1"
  5. )
  6. print(find_code_1['name'])

但是会报下面的错误,不知道是什么原因造成的

希望有知道的同道者可以为之解答一二

3、例子2

Neo4j里面节点和关系是两个比较重要的数据结构,即Node、Relationship,创建代码如下所示:


   
   
  1. #coding:utf-8
  2. from py2neo import Graph,Node, Relationship
  3. from pandas import DataFrame
  4. a = Node('Person', name='fengling')
  5. b = Node('Person', name='yingying')
  6. r = Relationship(a, 'KNOWS', b)
  7. print(a, b, r)

运行结果:

(fengling:Person {name:"fengling"}) (yingying:Person {name:"yingying"}) (fengling)-[:KNOWS]->(yingying)

上面的代码中创建了两个Node以及两者之间的Relationship

Node和Relationship都继承了PropertyDict类,类似于字典的形式,可以通过下面的代码对属性进行赋值操作:


   
   
  1. a['age'] = 20
  2. b['age'] = 21
  3. r['time'] = '2018/09/03'
  4. print(a, b, r)

运行结果:

(fengling:Person {age:20,name:"fengling"}) (yingying:Person {age:21,name:"yingying"}) (fengling)-[:KNOWS {time:"2018/09/03"}]->(yingying)


   
   
  1. 可以通过setdefault()方法赋值默认属性
  2. a.setdefault('location','nanjing')
  3. print(a)
  4. ##当给location赋值了属性,则它会覆盖默认的属性
  5. a['location'] = 'beijing'
  6. a.setdefault('location', 'nanjing')
  7. print(a)

运行结果:

(fengling:Person {age:20,location:"nanjing",name:"fengling"})
(fengling:Person {age:20,location:"beijing",name:"fengling"})

从运行结果可以看出来,当属性的值有赋值,即使重新进行setdefault(),这时属性的值是自己本身的值,而不是设置的默认值,这一点需要注意一下

update()方法可以实现对属性批量的更新,代码如下所示:


   
   
  1. data = {'name':'Amy','age':21}
  2. a.update(data)
  3. print(a)

运行结果:

(fengling:Person {age:21,location:"beijing",name:"Amy"})

这里更新了a对象的name、age属性,没有更新location属性,则name和age属性会更新,Location属性则会保留

Graph  图


   
   
  1. test_graph = Graph('http://localhost:7474',username='neo4j',password='test')
  2. ##这里调用create()方法即可完成图的创建
  3. a = Node('Person',name='fengling')
  4. test_graph.create(a)
  5. b = Node('Person',name='yingying')
  6. test_graph.create(b)
  7. r = Relationship(a,'KNOWS',b)
  8. print(a,b,r)
  9. 运行结果
  10. (fengling:Person {name:"fengling"}) (yingying:Person {name:"yingying"}) (fengling)-[:KNOWS]->(yingying)
  11. s = a | b | r
  12. test_graph.create(s)
  13. print(s)
  14. 运行结果:
  15. ({(yingying:Person {name:"yingying"}), (fengling:Person {name:"fengling"}), (fengling:Person {name:"fengling"}), (yingying:Person {name:"yingying"})}, {(fengling)-[:KNOWS]->(yingying)})
  16. ##可以使用data()方法获取查获结果
  17. data_1 = test_graph.data("MATCH(p:Person) return p")
  18. print(data_1)
  19. 运行结果:
  20. ##run()方法查询
  21. data_2 = test_graph.run("MATCH(p:Person) return p").data()
  22. print(data_2)
  23. 运行结果:
  24. print(DataFrame(test_graph.data("MATCH(p:Person) return p")))

运行结果:

注意:

每运行一遍程序代码,则相应的节点、边、关系就会被重复创建一遍

4、例子3

在python中实现下面的图谱:

具体的代码如下所示:

from py2neo import Graph,Node, Relationship,cypher
from pandas import DataFrame


##Graph  图
test_graph = Graph('http://localhost:7474',username='neo4j',password='test')
# a = Node('yule',name='谢娜')
test_graph.create(a)

b = Node('yule',name='张杰')
test_graph.create(b)
r = Relationship(a,'夫妻',b)
test_graph.create(r)

c = Node('yule',name='何炅')
test_graph.create(c)

test_graph.create(Relationship(test_graph.data("MATCH(a:yule{name:'谢娜'}) return a ")[0]['a'],

                               '朋友',test_graph.data("MATCH(b:yule{name:'何炅'}) return b ")[0]['b']))   

需要注意一下:因为“谢娜”这个节点已经在图谱中了,所以“何炅”节点建立之后,需要从图谱中查找这两个节点,之后新建新的关系即可

 

2019.1.30

今天将上面的代码进行重新执行,发现节点建立有问题,将上述例子1中的代码修改成下面即可


   
   
  1. # coding:utf-8
  2. from py 2neo import Graph, Node, Relationship
  3. # 连接neo4j数据库,输入地址、用户名、密码
  4. graph = Graph('http://localhost: 7474', username='neo 4j', password='test')
  5. graph.delete_all()
  6. # 创建结点
  7. test_node_1 = Node('ru_yi_zhuan', name='皇帝') # 修改的部分
  8. test_node_2 = Node('ru_yi_zhuan', name='皇后') # 修改的部分
  9. test_node_3 = Node('ru_yi_zhuan', name='公主') # 修改的部分
  10. graph.create(test_node_ 1)
  11. graph.create(test_node_ 2)
  12. graph.create(test_node_ 3)
  13. # 创建关系
  14. # 分别建立了test_node_1指向test_node_2和test_node_2指向test_node_1两条关系,关系的类型为"丈夫、妻子",两条关系都有属性count,且值为1。
  15. node_1_zhangfu_node_1 = Relationship(test_node_ 1, '丈夫', test_node_ 2)
  16. node_1_zhangfu_node_1['count'] = 1
  17. node_2_qizi_node_1 = Relationship(test_node_ 2, '妻子', test_node_ 1)
  18. node_2_munv_node_1 = Relationship(test_node_ 2, '母女', test_node_ 3)
  19. node_2_qizi_node_1['count'] = 1
  20. graph.create(node_ 1_zhangfu_node_ 1)
  21. graph.create(node_ 2_qizi_node_ 1)
  22. graph.create(node_ 2_munv_node_ 1)
  23. print(graph)
  24. print(test_node_ 1)
  25. print(test_node_ 2)
  26. print(node_ 1_zhangfu_node_ 1)
  27. print(node_ 2_qizi_node_ 1)
  28. print(node_ 2_munv_node_ 1)

同道者可以互相交流

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值