python 與neo4j 數據庫交互
windows 安裝包: 鏈接:https://pan.baidu.com/s/1vYnjO3I3b0qvdI9AttgBQw 提取碼:l2ow
py2neo版本 2020.1.0
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()