赞
踩
conda install py2neo
neo4j console
neo4j stop
或 Ctrl+C
from py2neo import Graph
URI = "neo4j://localhost"
AUTH = (USER, PASSWORD)
# 连接到Neo4j数据库
graph = Graph(URI, auth=AUTH)
# 初始化数据库
graph.delete_all()
order = """
CREATE (f:Person {name: 'Father', age:30}),
(s:Person {name: 'Son', age:10}),
(:Person {name: 'Nobody', age:20}),
(f) - [:FAMILY_WITH {type: 'Father'}] -> (s)
"""
graph.run(order)
order = "MATCH (n:Person{name:'Nobody'}) DELETE n"
graph.run(order)
quey_update = """
MATCH (f:Person {name: 'Father'})-[r]->(s:Person {name: 'Son'})
DELETE r
CREATE (f)-[:FAMILY_WITH {type: 'Son'}]->(s)
"""
graph.update(quey_update)
query = "MATCH (n) RETURN n"
graph.run(query).to_data_frame()
from py2neo import Subgraph
# 连接到Neo4j数据库
propertys = "Person"
# 增加节点及关系
father = Node(propertys, name="Father", age=30)
son = Node(propertys, name="Son", age=10)
nobody = Node(propertys, name="Nobody", age=20)
relation = Relationship(father, "FAMILY_WITH", son)
# 省略Node, Relationship, Path的新增方法
subgraph = Subgraph(nodes=[father, son, nobody], relationships=[relation])
graph.create(subgraph)
from py2neo import NodeMatcher
nodes = NodeMatcher(graph).match(name="Nobody")
if nodes.exists():
# 找到节点,删除
for node in nodes:
graph.delete(node)
from py2neo import RelationshipMatcher
# 查询关系
relation = RelationshipMatcher(graph)
rel_match = relation.match(r_type="FAMILY_WITH")
if rel_match.exists():
for rel in rel_match:
rel['type'] = 'Son'
graph.push(rel)
relation.match().all()
# 返回所有节点:方法一 from py2neo import NodeMatcher node_matcher = NodeMatcher(graph) nodes = node_matcher.match(propertys).all() print(nodes) # 返回所有节点:方法二 from py2neo import NodeMatch nodes = NodeMatch(graph, labels=frozenset({propertys})).all() print(nodes) # 查询关系:方法一 from py2neo import RelationshipMatcher rel_matcher = RelationshipMatcher(graph) rels = rel_matcher.match(nodes=[father]).all() for rel in rels: print(rel) # 查询关系:方法二 from py2neo import RelationshipMatch rels = RelationshipMatch(graph, nodes=[father]).all() for rel in rels: print(rel)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。