py2neo的简单使用(一)

python 与neo4j 数据库交互
windows 安装包: 链接:https://pan.baidu.com/s/1vYnjO3I3b0qvdI9AttgBQw 提取码:l2ow

py2neo版本 2020.1.0

文档:https://py2neo.org/2021.1/

https://www.jianshu.com/p/febe8a248582

创建图对象

from py2neo import Graph
import time

graph = Graph('http://localhost:7474', username='neo4j', password='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', username='neo4j', password='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
g = Graph('http://localhost:7474', username='neo4j', password='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

g = Graph('http://localhost:7474', username='neo4j', password='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)
tx.commit()

query

match(nodes=None, r_type=None, limit=None)
匹配所有节点
from py2neo import Graph, Node, Relationship

g = Graph('http://localhost:7474', username='neo4j', password='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', username='neo4j', password='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', username='neo4j', password='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)
# tx.commit()

# 修改多个节点
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)
tx.commit()
两个节点新加关系
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph

g = Graph('http://localhost:7474', username='neo4j', password='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', username='neo4j', password='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()
print(relation)
g.delete(relation)
只删除关系
sepatate 方法
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher

g = Graph('http://localhost:7474', username='neo4j', password='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', username='neo4j', password='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)
tx.commit()
删除所有的关系
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher

g = Graph('http://localhost:7474', username='neo4j', password='123456')
matcher = RelationshipMatcher(g)
tx = g.begin()
relationship_list = matcher.match().all()

node_list = Subgraph(relationships=relationship_list)

tx.separate(node_list)
tx.commit()

posted @ 2022-04-08 23:11  时光如你般美好  阅读(802)  评论(0编辑  收藏  举报