Py2Neo 是用来对接 Neo4j 的 Python 库
from py2neo import Graph
test_graph = Graph(
"http://localhost:7474",
username="neo4j",
password="sl123456"
)
from py2neo import Node, Relationship
a = Node('Person', name='Alice')
b = Node('Person', name='Bob')
r = Relationship(a, 'KNOWS', b)
print(a, b, r)
a['age'] = 20
b['age'] = 21
r['time'] = '2017/08/31'
print(a, b, r)
a.setdefault('location', '北京')
print(a)
data = {
'name': 'Amy',
'age': 21
}
a.update(data)
print(a)
Subgraph(子图)是 Node 和 Relationship 的集合,最简单的构造子图的方式是通过关系运算符,实例如下:
from py2neo import Node, Relationship
a = Node('Person', name='Alice')
b = Node('Person', name='Bob')
r = Relationship(a, 'KNOWS', b)
s = a | b | r
print(s)
可以通过 nodes () 和 relationships () 方法获取所有的 Node 和 Relationship
print(s.nodes())
print(s.relationships())
Walkable 是增加了遍历信息的 Subgraph,我们通过 + 号便可以构建一个 Walkable 对象,例如:
from py2neo import Node, Relationship
a = Node('Person', name='Alice')
b = Node('Person', name='Bob')
c = Node('Person', name='Mike')
ab = Relationship(a, "KNOWS", b)
ac = Relationship(a, "KNOWS", c)
w = ab + Relationship(b, "LIKES", c) + ac
print(w)
可以调用 walk () 方法实现遍历,实例如下:
from py2neo import walk
for item in walk(w):
print(item)
可以看到它从 a 这个 Node 开始遍历,然后到 b,再到 c,最后重新回到 a。 另外还可以利用 start_node ()、end_node ()、nodes ()、relationships () 方法来获取起始 Node、终止 Node、所有 Node 和 Relationship,例如:
print(w.start_node())
print(w.end_node())
print(w.nodes())
print(w.relationships())
在 database 模块中包含了和 Neo4j 数据交互的 API,最重要的当属 Graph,它代表了 Neo4j 的图数据库,同时 Graph 也提供了许多方法来操作 Neo4j 数据库。 Graph 在初始化的时候需要传入连接的 URI,初始化参数有 bolt、secure、host、http_port、https_port、bolt_port、user、password,详情说明可以参考:http://py2neo.org/v3/database.html#py2neo.database.Graph。 初始化的实例如下:
from py2neo import Graph
graph_1 = Graph()
graph_2 = Graph(host="localhost")
graph_3 = Graph("http://localhost:7474/db/data/")
可以利用 create () 方法传入 Subgraph 对象来将关系图添加到数据库中,实例如下:
from py2neo import Node, Relationship, Graph
a = Node('Person', name='Alice')
b = Node('Person', name='Bob')
r = Relationship(a, 'KNOWS', b)
s = a | b | r
graph = Graph(password='123456')
graph.create(s)
也可以单独添加单个 Node 或 Relationship,实例如下:
from py2neo import Graph, Node, Relationship
graph = Graph(password='123456')
a = Node('Person', name='Alice')
graph.create(a)
b = Node('Person', name='Bob')
ab = Relationship(a, 'KNOWS', b)
graph.create(ab)
还可以利用 data () 方法来获取查询结果:
from py2neo import Graph
graph = Graph(password='123456')
data = graph.data('MATCH (p:Person) return p')
print(data)
可以使用 find_one () 或 find () 方法进行 Node 的查找,可以利用 match () 或 match_one () 方法对 Relationship 进行查找:
from py2neo import Graph
graph = Graph(password='123456')
node = graph.find_one(label='Person')
print(node)
relationship = graph.match_one(rel_type='KNOWS')
print(relationship)
更新 Node 的某个属性可以使用 push () 方法,例如:
from py2neo import Graph, Node
graph = Graph(password='123456')
a = Node('Person', name='Alice')
node = graph.find_one(label='Person')
node['age'] = 21
graph.push(node)
print(graph.find_one(label='Person'))
删除某个 Node 可以使用 delete () 方法(在删除 Node 时必须先删除其对应的 Relationship,否则无法删除 Node),例如:
from py2neo import Graph
graph = Graph(password='123456')
node = graph.find_one(label='Person')
relationship = graph.match_one(rel_type='KNOWS')
graph.delete(relationship)
graph.delete(node)
Graph 有时候用起来不太方便,比如如果要根据多个条件进行 Node 的查询是做不到的,在这里更方便的查询方法是利用 NodeSelector,我们首先新建如下的 Node 和 Relationship,实例如下:
from py2neo import Graph, Node, Relationship
graph = Graph(password='123456')
a = Node('Person', name='Alice', age=21, location='广州')
b = Node('Person', name='Bob', age=22, location='上海')
c = Node('Person', name='Mike', age=21, location='北京')
r1 = Relationship(a, 'KNOWS', b)
r2 = Relationship(b, 'KNOWS', c)
graph.create(a)
graph.create(r1)
graph.create(r2)
用 NodeSelector 来筛选 age 为 21 的 Person Node,实例如下:
from py2neo import Graph, NodeSelector
graph = Graph(password='123456')
selector = NodeSelector(graph)
persons = selector.select('Person', age=21)
print(list(persons))
也可以使用 where () 进行更复杂的查询,例如查找 name 是 A 开头的 Person Node,实例如下:
from py2neo import Graph, NodeSelector
graph = Graph(password='123456')
selector = NodeSelector(graph)
persons = selector.select('Person').where('_.name =~ "A.*"')
print(list(persons))
也可以使用 order_by () 进行排序:
from py2neo import Graph, NodeSelector
graph = Graph(password='123456')
selector = NodeSelector(graph)
persons = selector.select('Person').order_by('_.age')
print(list(persons))
前面返回的都是列表,如果要查询单个节点的话,可以使用 first () 方法,实例如下:
from py2neo import Graph, NodeSelector
graph = Graph(password='123456')
selector = NodeSelector(graph)
person = selector.select('Person').where('_.name =~ "A.*"').first()
print(person)
OGM 类似于 ORM,意为 Object Graph Mapping,这样可以实现一个对象和 Node 的关联,例如:
from py2neo.ogm import GraphObject, Property, RelatedTo, RelatedFrom
class Movie(GraphObject):
__primarykey__ = 'title'
title = Property()
released = Property()
actors = RelatedFrom('Person', 'ACTED_IN')
directors = RelatedFrom('Person', 'DIRECTED')
producers = RelatedFrom('Person', 'PRODUCED')
class Person(GraphObject):
__primarykey__ = 'name'
name = Property()
born = Property()
acted_in = RelatedTo('Movie')
directed = RelatedTo('Movie')
produced = RelatedTo('Movie')
可以用它来结合 Graph 查询,例如:
from py2neo import Graph
from py2neo.ogm import GraphObject, Property
graph = Graph(password='123456')
class Person(GraphObject):
__primarykey__ = 'name'
name = Property()
age = Property()
location = Property()
person = Person.select(graph).where(age=21).first()
print(person)
print(person.name)
print(person.age)
可以用它动态改变 Node 的属性,例如修改某个 Node 的 age 属性,实例如下:
person = Person.select(graph).where(age=21).first()
print(person.__ogm__.node)
person.age = 22
print(person.__ogm__.node)
graph.push(person)
也可以通过映射关系进行 Relationship 的调整,例如通过 Relationship 添加一个关联 Node,实例如下:
from py2neo import Graph
from py2neo.ogm import GraphObject, Property, RelatedTo
graph = Graph(password='123456')
class Person(GraphObject):
__primarykey__ = 'name'
name = Property()
age = Property()
location = Property()
knows = RelatedTo('Person', 'KNOWS')
person = Person.select(graph).where(age=21).first()
print(list(person.knows))
new_person = Person()
new_person.name = 'Durant'
new_person.age = 28
person.knows.add(new_person)
graph.push(person)
print(list(person.knows))
通过 remove () 方法移除某个关联 Node,实例如下:
person = Person.select(graph).where(name='Alice').first()
target = Person.select(graph).where(name='Durant').first()
person.knows.remove(target)
graph.push(person)
graph.delete(target)
官方文档:https://py2neo.org/2021.1/#quick-example
博文一:https://cuiqingcai.com/4778.html
博文二:https://cloud.tencent.com/developer/article/1434904
GitHub:https://github.com/technige/py2neo