当前位置:   article > 正文

【技巧分享】Neo4j Python入门操作_python neo4j

python neo4j

环境配置

  • 安装Neo4j及配置,略
  • 安装Java及配置,略
  • 安装py2neo:conda install py2neo
  • 启动环境:neo4j console
  • 关闭环境:neo4j stopCtrl+C

常用语句

from py2neo import Graph

URI = "neo4j://localhost"
AUTH = (USER, PASSWORD)

# 连接到Neo4j数据库
graph = Graph(URI, auth=AUTH)

# 初始化数据库
graph.delete_all()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Cypher语句

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

order = "MATCH (n:Person{name:'Nobody'}) DELETE n"
graph.run(order)
  • 1
  • 2

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

query = "MATCH (n) RETURN n"
graph.run(query).to_data_frame()
  • 1
  • 2

面向对象

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

from py2neo import NodeMatcher

nodes = NodeMatcher(graph).match(name="Nobody")

if nodes.exists():
    # 找到节点,删除
    for node in nodes:
        graph.delete(node)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12


# 返回所有节点:方法一
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/790282
推荐阅读
相关标签
  

闽ICP备14008679号