py2neo==4.3.0
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"))
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)
# 新版
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']))
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)
节点也会被删除
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)
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语言操作