python操作neo4j

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

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

#coding:utf-8
from py2neo import Graph,Node,Relationship

##连接neo4j数据库,输入地址、用户名、密码
graph = Graph('http://localhost:7474',username='neo4j',password='test')

##创建结点
test_node_1 = Node(label='ru_yi_zhuan',name='皇帝')
test_node_2 = Node(label='ru_yi_zhuan',name='皇后')
test_node_3 = Node(label='ru_yi_zhuan',name='公主')
graph.create(test_node_1)
graph.create(test_node_2)
graph.create(test_node_3)

##创建关系
#分别建立了test_node_1指向test_node_2和test_node_2指向test_node_1两条关系,关系的类型为"丈夫、妻子",两条关系都有属性count,且值为1。
node_1_zhangfu_node_1 = Relationship(test_node_1,'丈夫',test_node_2)
node_1_zhangfu_node_1['count'] = 1
node_2_qizi_node_1 = Relationship(test_node_2,'妻子',test_node_1)
node_2_munv_node_1 = Relationship(test_node_2,'母女',test_node_3)

node_2_qizi_node_1['count'] = 1

graph.create(node_1_zhangfu_node_1)
graph.create(node_2_qizi_node_1)
graph.create(node_2_munv_node_1)

print(graph)
print(test_node_1)
print(test_node_2)
print(node_1_zhangfu_node_1)
print(node_2_qizi_node_1)
print(node_2_munv_node_1)

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

注意一下:

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

不启动的话,程序会报错

 

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

find_code_1 = test_graph.find_one(
  label="ru_yi_zhuan",
  property_key="name",
  property_value="test_node_1"
)
print(find_code_1['name'])

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

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

3、例子2

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

#coding:utf-8
from py2neo import Graph,Node, Relationship
from pandas import DataFrame

a = Node('Person', name='fengling')
b = Node('Person', name='yingying')
r = Relationship(a, 'KNOWS', b)
print(a, b, r)

运行结果:

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

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

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

a['age'] = 20
b['age'] = 21
r['time'] = '2018/09/03'
print(a, b, r)

运行结果:

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

可以通过setdefault()方法赋值默认属性

a.setdefault('location','nanjing')
print(a)
##当给location赋值了属性,则它会覆盖默认的属性
a['location'] = 'beijing'
a.setdefault('location', 'nanjing')
print(a)

运行结果:

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

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

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

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

运行结果:

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

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

Graph  图

test_graph = Graph('http://localhost:7474',username='neo4j',password='test')
##这里调用create()方法即可完成图的创建
a = Node('Person',name='fengling')
test_graph.create(a)
b = Node('Person',name='yingying')
test_graph.create(b)
r = Relationship(a,'KNOWS',b)
print(a,b,r)
运行结果
(fengling:Person {name:"fengling"}) (yingying:Person {name:"yingying"}) (fengling)-[:KNOWS]->(yingying)
s = a | b | r
test_graph.create(s)
print(s)
运行结果:
({(yingying:Person {name:"yingying"}), (fengling:Person {name:"fengling"}), (fengling:Person {name:"fengling"}), (yingying:Person {name:"yingying"})}, {(fengling)-[:KNOWS]->(yingying)})
##可以使用data()方法获取查获结果
data_1 = test_graph.data("MATCH(p:Person) return p")
print(data_1)
运行结果:

##run()方法查询
data_2 = test_graph.run("MATCH(p:Person) return p").data()
print(data_2)
运行结果:

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中的代码修改成下面即可

# coding:utf-8
from py2neo import Graph, Node, Relationship

# 连接neo4j数据库,输入地址、用户名、密码
graph = Graph('http://localhost:7474', username='neo4j', password='test')
graph.delete_all()
# 创建结点
test_node_1 = Node('ru_yi_zhuan', name='皇帝') # 修改的部分
test_node_2 = Node('ru_yi_zhuan', name='皇后') # 修改的部分
test_node_3 = Node('ru_yi_zhuan', name='公主') # 修改的部分


graph.create(test_node_1)
graph.create(test_node_2)
graph.create(test_node_3)

# 创建关系
# 分别建立了test_node_1指向test_node_2和test_node_2指向test_node_1两条关系,关系的类型为"丈夫、妻子",两条关系都有属性count,且值为1。
node_1_zhangfu_node_1 = Relationship(test_node_1, '丈夫', test_node_2)
node_1_zhangfu_node_1['count'] = 1
node_2_qizi_node_1 = Relationship(test_node_2, '妻子', test_node_1)
node_2_munv_node_1 = Relationship(test_node_2, '母女', test_node_3)

node_2_qizi_node_1['count'] = 1

graph.create(node_1_zhangfu_node_1)
graph.create(node_2_qizi_node_1)
graph.create(node_2_munv_node_1)

print(graph)
print(test_node_1)
print(test_node_2)
print(node_1_zhangfu_node_1)
print(node_2_qizi_node_1)
print(node_2_munv_node_1)

同道者可以互相交流

  • 35
    点赞
  • 224
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 29
    评论
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨算法屋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值