python 与neo4j 数据库交互
py2neo版本 2021.2.3
创建图对象
from py2neo import Graph
'''
host:服务器ip地址,默认为'localhost'
http_port:http协议——服务器监听端口,默认7474
https_port:https协议——服务器监听端口,默认7473
user:登录用户名,默认'neo4j'
password:登录密码,无默认值,故若数据库其他参数都为默认值,则可直接通过密码登录
'''
graph = Graph('http://localhost:7474', auth=("neo4j", "123456"))
数据对象 Object
Node
#获取key对应的property
x=node[key]
#设置key键对应的value,如果value是None就移除这个property
node[key] = value
#也可以专门删除某个property
del node[key]
#返回node里面property的个数
len(node)
#返回所以和这个节点有关的label
labels=node.labels
#删除某个label
node.labels.remove(labelname)
#将node的所有property以dictionary的形式返回
dict(node)
Relationship
#创建Relationship
Relationship`(*start_node*, *type*, *end_node*, ***properties*)
#返回Relationship的property
Relationship[key]
#删除某个property
del Relationship[key]
#将relationship的所有property以dictionary的形式返回
dict(relationship)
创建一段关系链
# 新版
from py2neo import Graph, Node, Relationship
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
a = Node("Person", name="Alice")
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
g.create(ab)
以下代码构造数据后面使用
# 新版
from py2neo import Graph, Node, Relationship, Subgraph
graph = Graph('http://localhost:7474', auth=("neo4j", "123456"))
tx = g.begin()
jiazhen = Node("Person", name="陈家珍", age=66)
fugui = Node("Person", name='徐福贵', age=67)
youqian = Node("Person", name="徐有钱")
renxing = Node("Person", name="徐任性")
cat = Node("Person", name='cat')
dog = Node("Person", name='dog')
wife = Relationship(fugui, "WIFE", jiazhen)
brother_1 = Relationship(fugui, "BROTHER", youqian)
brother_2 = Relationship(fugui, "BROTHER", renxing)
hus = Relationship(jiazhen, 'HUS', fugui)
know = Relationship(cat, 'KNOWS', dog)
relation_list = Subgraph(relationships=[wife, brother_2, brother_1, hus, know])
tx.create(relation_list)
graph.commit(tx)
query
match(nodes=None, r_type=None, limit=None)
匹配所有节点
from py2neo import Graph, Node, Relationship
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
nodes = g.nodes.match()
for node in nodes:
print(node)
print(node.items())
print(node.labels)
匹配符合指定条件的节点
NodeMatcher
from py2neo import Graph, Node, Relationship, NodeMatcher
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
# 用来查找节点的对象
matcher = NodeMatcher(g)
# first 返回第一个符合条件的节点
node1 = matcher.match('Person', name='徐有钱').first()
print(node1)
print(node1['name'])
# all 返回所有符合条件的节点
nodes = matcher.match('Person').all()
for node in nodes:
print(node['name'])
print(node['age'])
nodes = matcher.match('Person', age=66).all()
print('*' * 25 + '年龄66的节点' + '*' * 25)
for node in nodes:
print(node['name'])
print(node['age'])
# 模糊匹配 要用Cypher
nodes = matcher.match("Person").where("_.name =~ '徐.*'").all()
print('*' * 25 + '姓徐的节点' + '*' * 25)
for node in nodes:
print(node['name'])
print(node['age'])
更新 Update
更新先要找出Nodes,再使用事务的push更新
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
from py2neo import Graph, NodeMatcher
tx = g.begin()
# 找到你要找的Nodes
matcher = NodeMatcher(g)
## 修改单个节点
# init_node = matcher.match("Person", name="徐福贵")
# new_node = init_node.first()
# new_node['name'] = "福贵"
# sub = Subgraph(nodes=[new_node])
# tx.push(sub)
# g.commit(tx)
# 修改多个节点
init_node = matcher.match("Person")
new_nodes = []
for node in init_node.all():
node['name'] = '⭐'+node['name']
new_nodes.append(node)
sub = Subgraph(nodes=new_nodes)
tx.push(sub)
g.commit(tx)
两个节点新加关系
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
matcher = NodeMatcher(g)
fugui = matcher.match('Person', name='⭐徐福贵').first()
youqian = matcher.match('Person', name='⭐徐有钱').first()
relation = Relationship(fugui, 'Brother', youqian)
g.create(relation)
删除
删除关系链(节点也会删除)
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
matcher = NodeMatcher(g)
r_matcher = RelationshipMatcher(g)
fugui = matcher.match('Person', name='⭐徐福贵').first()
youqian = matcher.match('Person', name='⭐徐有钱').first()
relation = r_matcher.match(nodes=[fugui, youqian]).first() #也可以是none,表示任意节点。注意前后顺序
print(relation)
g.delete(relation)
只删除关系
sepatate 方法
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
matcher = NodeMatcher(g)
r_matcher = RelationshipMatcher(g)
cat = matcher.match('Person', name='⭐cat').first()
dog = matcher.match('Person', name='⭐dog').first()
relation = r_matcher.match(nodes=[cat, dog]).first()
print(relation)
g.separate(relation)
批处理
对于大量的插入一般是很费时的,首先我们可以使用事务,加快一定速度,而插入的方法一样重要,我们很多时候是遍历一个文件然后生成图,例子中我们生成每个Node后,先把他们放入一个List中,再变为Subgraph实例,然后再create(),耗时比一条条插入至少快10倍以上
创建多个节点
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
tx = g.begin()
node_list = [Node("Num", name=str(i)) for i in range(4)]
node_list = Subgraph(nodes=node_list)
tx.create(node_list)
g.commit(tx)
删除所有的关系
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher
g = Graph('http://localhost:7474', auth=("neo4j", "123456"))
matcher = RelationshipMatcher(g)
tx = g.begin()
relationship_list = matcher.match().all()
node_list = Subgraph(relationships=relationship_list)
tx.separate(node_list)
g.commit(tx)