知识图谱学习(一) py2neo


一、python 与neo4j 数据库交互

py2neo==4.3.0

1.创建图对象

from py2neo import Graph
'''
host:服务器ip地址,默认为'localhost'
http_port:http协议——服务器监听端口,默认7474
https_port:https协议——服务器监听端口,默认7473
user:登录用户名,默认'neo4j'
password:登录密码,无默认值,故若数据库其他参数都为默认值,则可直接通过密码登录
'''
graph = Graph('http://localhost:7474', auth=("neo4j", "123456"))

2.创建数据对象

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)

在这里插入图片描述

query

# 新版
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()

在这里插入图片描述

匹配所有节点
from py2neo import Graph, Node, Relationship

g = Graph('http://localhost:7474', auth=("neo4j", "123456"))

nodes = g.nodes.match()

for node in nodes:
    print(node)
print(node.items())
print(node.labels)

在这里插入图片描述

匹配符合指定条件节点
from py2neo import Graph, Node, Relationship, NodeMatcher

g = Graph('http://localhost:7474', auth=("neo4j", "123456"))

# 用来查找节点的对象
matcher = NodeMatcher(g)

# first 返回第一个符合条件的节点
# node1 = matcher.match('Person', name='徐有钱').first()
node1 = matcher.match('Person').where("_.name='徐有钱'").first()
print(node1)

# all 返回所有符合条件的节点
nodes = matcher.match('Person')

for node in nodes:
    print('姓名:%s \t 年龄:%s '% (node['name'], node['age']))

nodes = matcher.match('Person').where("_.age=66")
print('*' * 25 + '年龄66的节点' + '*' * 25)
for node in nodes:
    print('姓名:%s \t 年龄:%s ' % (node['name'], node['age']))

# 模糊匹配 要用Cypher
nodes = matcher.match("Person").where("_.name =~ '徐.*'")

print('*' * 25 + '姓徐的节点' + '*' * 25)
for node in nodes:
    print('姓名:%s \t 年龄:%s ' % (node['name'], node['age']))
    

在这里插入图片描述

Update

py2neo==2020.1.0

本来想继续使用py2neo==4.3.0版本,无奈总是报错,针对该错误的情况的代码修改方式较少,更多的推荐是更改py2neo版本。

修改单个节点
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph

g = Graph('http://localhost:7474', auth=("neo4j", "123456"))

from py2neo import Graph, NodeMatcher

tx = g.begin()
# 找到你要找的Nodes
matcher = NodeMatcher(g)

# 修改单个节点
init_node = matcher.match("Person").where('_.name="徐福贵"')
new_node = init_node.first()
new_node['name'] = "福贵"
sub = Subgraph(nodes=[new_node])
tx.push(sub)
tx.commit()

在这里插入图片描述

修改多个节点
# 修改多个节点
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph

g = Graph('http://localhost:7474', auth=("neo4j", "123456"))

from py2neo import Graph, NodeMatcher

tx = g.begin()
# 找到你要找的Nodes
matcher = NodeMatcher(g)

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', auth=("neo4j", "123456"))

matcher = NodeMatcher(g)

fugui = matcher.match('Person', name='⭐福贵').first()
youqian = matcher.match('Person', name='⭐徐有钱').first()

relation = Relationship(fugui, 'Brother', youqian)

g.create(relation)

在这里插入图片描述

删除

删除关系链 delete

节点也会被删除

from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher

g = Graph('http://localhost:7474', auth=("neo4j", "123456"))

matcher = NodeMatcher(g)
r_matcher = RelationshipMatcher(g)
a = matcher.match('Person', name='⭐Alice').first()
b = matcher.match('Person', name='⭐Bob').first()

relation = r_matcher.match(nodes=[a, b]).first()	#也可以是none,表示任意节点。注意前后顺序
# nodes=[None,b] 表示所有以b为终点的关系
# nodes=[a,None] 表示所有以a为起点的关系
# nodes=[None,None] 表示所有节点的关系
print(relation)
g.delete(relation)

在这里插入图片描述

只删除关系 separate
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher

g = Graph('http://localhost:7474', auth=("neo4j", "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)

在这里插入图片描述

批处理

创建多个节点
from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph, RelationshipMatcher

g = Graph('http://localhost:7474', auth=("neo4j", "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', auth=("neo4j", "123456"))
matcher = RelationshipMatcher(g)
tx = g.begin()
relationship_list = matcher.match().all()

node_list = Subgraph(relationships=relationship_list)

tx.separate(node_list)
tx.commit()

在这里插入图片描述

二、版本问题

Neo4j版本4.2.1,Python版本3.6 的条件下 py2neo4.3.0版本无法使用

NodeMatcher(graph).match().all()

py2neo4.0.0版本可使用NodeMatcher(graph).match().all(),但无法与当前版本的Neo4j建立连接

py2neo4.3.0版本可以使用

NodeMatcher(graph).match().first()

py2neo4.3.0版本无法使用

node1 = matcher.match('Person', name='徐有钱').first()

py2neo4.3.0版本可以使用

node1 = matcher.match('Person').where("_.name='徐有钱'").first()

py2neo最新版本无法直接访问Neo4j,情况同py2neo4.0.0版本近似 py2neo3.1.2版本支持Python3.5以下版本

因此推荐使用py2neo 2020.1.0版本

三、参考链接

python 与neo4j 数据库交互
neo4j基本使用及其Python语言操作

  • 6
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值