您当前的位置: 首页 >  彭世瑜 Python

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

彭世瑜 发布时间:2019-01-22 14:50:03 ,浏览量:3

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'})}]

在这里插入图片描述

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 3浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0540s