Python编程:py2neo操作neo4j图数据库

py2neo文档: https://py2neo.org/v4/index.html

安装:

pip install py2neo

本文用的版本是:
py2neo 4.1.3

代码示例

# -*- coding: utf-8 -*-

from py2neo import Graph, Node, Relationship, NodeMatcher
from py2neo.matching import RelationshipMatcher

# 连接数据库
graph = Graph("http://localhost:7474", username="neo4j", password='123456')

# 创建节点
p1 = Node("Person", name="张三")
p2 = Node("Person", name="李四")
graph.create(p1)
graph.create(p2)

# 创建关系
r = Relationship(p1, "认识", p2)
graph.create(r)

# 节点查询
mather = NodeMatcher(graph)
result = mather.match("Person", name="张三").first()
print(result)
# (_0:Person {name: '\u5f20\u4e09'})


# 查询关系
relation_matcher = RelationshipMatcher(graph)
ret = relation_matcher.match((result,), r_type="认识").first()
print(ret)
# (张三)-[:认识 {}]->(李四)

# 直接运行cypher语句
ret = graph.run('match(p:Person{name:"李四"}) return p').data()
print(ret)
# [{'p': (_3:Person {name: '\u674e\u56db'})}]

Python编程:py2neo操作neo4j图数据库_第1张图片

你可能感兴趣的:(python,neo4j)