Python使用py2neo操作图数据库neo4j的方法详解
什么是neo4j和py2neo
neo4j
neo4j是一款开源的、高性能的、基于Java的图数据库管理系统(Graph Database Management Systems)。它的特点在于支持存储跟踪重关系数据、快速的读写速度和脚本可操作性能。
py2neo
py2neo是python对neo4j的封装的一个库,提供了方便的访问和管理neo4j数据库的接口,方便了python编程用户的使用。
安装
安装neo4j
neo4j的安装请参考官网的指南https://neo4j.com/docs/operations-manual/current/installation/。
安装py2neo
pip install py2neo
连接Database
连接Database是py2neo操作neo4j的重要的第一步。为了连接到neo4j server需要neo4j的URI以及用户名和密码。
from py2neo import Graph, Node, Relationship
graph = Graph("bolt://localhost:7687", username="neo4j", password="password")
创建Node
在neo4j中创建一个节点,我们需要创建一个Node对象然后将它添加到图形数据库中。
创建一个节点
#创建一个节点
node = Node('Movie', title='The Dark Knight', released=2008, tagline='Why so serious?')
graph.create(node)
创建带标签和属性的节点
#创建带标签和属性的节点
node = Node('Person', name='Tom Cruise')
graph.create(node)
创建关系
在neo4j中,两个节点可以通过边(relationships)被连接在一起,并且可以带有边上的属性。
创建一个关系
#创建一个关系
type_of_movie = Relationship.type('TYPE_OF_MOVIE')
rel = Relationship(node1, type_of_movie, node3)
graph.create(rel)
创建带有属性的关系
#创建带有属性的关系
acted_in = Relationship.type('ACTED_IN')
rel = acted_in((node1, {'role':'Batman'}), (node3, {'role':'The Joker'}))
graph.create(rel)
查询
py2neo提供了query函数来发送cypher查询语句至数据库中并返回结果集。
查询所有节点
#查询所有节点
result = graph.run("MATCH (n) RETURN n")
for row in result:
print(row[0])
查询特定类型的节点
#查询特定类型的节点
result = graph.run("MATCH (n:Movie) RETURN n")
for row in result:
print(row[0])
查询特定的属性
#查询特定的属性
result = graph.run("MATCH (n:Person) WHERE n.name = 'Tom Cruise' RETURN n")
for row in result:
print(row[0])
综合练习
我们使用py2neo操作neo4j创建一个电影数据模型。
from py2neo import Graph, Node, Relationship, NodeMatcher
graph = Graph("bolt://localhost:7687", username="neo4j", password="password")
#创建电影
batman_movie = Node('Movie', title='Batman', released=1989, tagline='Have you ever danced with the devil in the pale moonlight?')
dark_knight_movie = Node('Movie', title='The Dark Knight', released=2008, tagline='Why so serious?')
graph.create(batman_movie)
graph.create(dark_knight_movie)
#创建导演
tim_burton = Node('Person', name='Tim Burton')
christopher_nolan = Node('Person', name='Christopher Nolan')
graph.create(tim_burton)
graph.create(christopher_nolan)
#创建演员
michael_keaton = Node('Person', name='Michael Keaton')
jack_nicholson = Node('Person', name='Jack Nicholson')
heath_ledger = Node('Person', name='Heath Ledger')
graph.create(michael_keaton)
graph.create(jack_nicholson)
graph.create(heath_ledger)
#创建关联关系
type_of_movie = Relationship.type('TYPE_OF_MOVIE')
directed_by = Relationship.type('DIRECTED_BY')
acted_in = Relationship.type('ACTED_IN')
tim_burton_movies = type_of_movie(batman_movie, dark_knight_movie)
burton_directed_batman = directed_by(tim_burton, batman_movie)
nolan_directed_dark_knight = directed_by(christopher_nolan, dark_knight_movie)
keaton_in_batman = acted_in((michael_keaton, {'role':'Batman'}), (batman_movie, {'role':'Batman'}))
nicholson_in_batman = acted_in((jack_nicholson, {'role':'The Joker'}), (batman_movie, {'role':'The Joker'}))
ledger_in_dark_knight = acted_in((heath_ledger, {'role':'The Joker'}), (dark_knight_movie, {'role':'The Joker'}))
graph.create(tim_burton_movies)
graph.create(burton_directed_batman)
graph.create(nolan_directed_dark_knight)
graph.create(keaton_in_batman)
graph.create(nicholson_in_batman)
graph.create(ledger_in_dark_knight)
#查询电影
result = graph.run("MATCH (n:Movie) RETURN n")
for row in result:
print(row[0])
#查询电影的演员
movie_title = 'Batman'
match_movie = NodeMatcher(graph).match("Movie", title=movie_title).first()
acted_in = match_movie.match_outgoing(rel_type="ACTED_IN")
for rel in acted_in:
print('{} acted as {} in {}'.format(rel.start_node['name'], rel['role'], rel.end_node['title']))
总结
通过py2neo依据neo4j的API,我们可以方便对neo4j进行创建节点、创建关系、查询、删除节点、删除关系等操作。当然,将相对底层的cypher查询语句转化为高层次的能力也给予我们便利程度的提升。