當前位置:網站首頁>[知識圖譜]cql與py2neo學習筆記

[知識圖譜]cql與py2neo學習筆記

2022-07-23 04:02:37熱愛旅行的小李同學

cql與py2neo學習筆記

CQL

開啟neo4j服務

neo4j.bat console

默認鏈接是 http://llocalhost:7474

删除數據庫中的圖

match (n) detach delete n

創建節點

create (p:Person{name:"張三",age:18})

批量創建節點

create (p1:Person{name:"李四",age:19}),(p2:Person{name:"王五",age:20}),(p3:Person{name:"趙六",age:20}),(p4:Person{name:"田七",age:22})

查看圖

match (n) return n

在這裏插入圖片描述

創建關系

match (p1:Person{name:"張三"}), (p2:Person{name:"李四"}) merge (p1)-[:弟弟]->(p2);
match (p2:Person{name:"李四"}), (p1:Person{name:"張三"}) merge (p2)-[:哥哥]->(p1);

創建關系同時增加屬性

match (p1:Person{name:"張三"}), (p2:Person{name:"王五"}) merge (p1)-[:學生{
   time:2}]->(p2)

在這裏插入圖片描述

不同標簽之間建立關系

match (p:Person{name:"張三"}), (n:Location{city:"北京"}) merge (p)-[:出生地]->(n);

在這裏插入圖片描述

查詢所有在北京出生的人

match (p:Person)-[:出生地]->(n:Location{city:"北京"}) return p, n

在這裏插入圖片描述

查詢所有對外有關系的節點

match (a)-->() return a

在這裏插入圖片描述

查詢所有有關系的節點

match (a)--() return a

在這裏插入圖片描述

查詢所有有同學關系的節點

match (a)-[:同學]-() return a

在這裏插入圖片描述

增加/修改屬性

match (p:Person{name:"張三"}) set p.sex = "男"

删除屬性

match (p:Person{name:"張三"}) remove p.sex

删除關系(可指定具體關系,可同時删除節點)

match (p1:Person{name:"張三"})-[r]-(p2:Person{name:"田七"}) delete r

删除節點

match (p:Person{name:"田七"}) delete p

條件查找

match (p:Person) where(p.age>19)  return p

在這裏插入圖片描述

匯總

match (n) detach delete n;
create (p:Person{name:"張三",age:18});
create (p1:Person{name:"李四",age:19}),(p2:Person{name:"王五",age:20}),(p3:Person{name:"趙六",age:20}),(p4:Person{name:"田七",age:22});
create (n:Location{city:"北京"});
create (n1:Location{city:"上海"}), (n2:Location{city:"廣州"});
match (n) return n;
match (p1:Person{name:"張三"}), (p2:Person{name:"李四"}) merge (p1)-[:弟弟]->(p2);
match (p2:Person{name:"李四"}), (p1:Person{name:"張三"}) merge (p2)-[:哥哥]->(p1);
match (p1:Person{name:"張三"}), (p2:Person{name:"王五"}) merge (p1)-[:學生{
   time:2}]->(p2);
match (p1:Person{name:"張三"}), (p2:Person{name:"王五"}) merge (p2)-[:老師{
   time:2}]->(p1);
match (p1:Person{name:"張三"}), (p2:Person{name:"趙六"}) merge (p1)-[:朋友]->(p2);
match (p1:Person{name:"張三"}), (p2:Person{name:"田七"}) merge (p1)-[:同學]->(p2);
match (p:Person{name:"張三"}), (n:Location{city:"北京"}) merge (p)-[:出生地]->(n);
match (p:Person{name:"李四"}), (n:Location{city:"上海"}) merge (p)-[:出生地]->(n);
match (p:Person{name:"王五"}), (n:Location{city:"廣州"}) merge (p)-[:出生地]->(n);
match (p:Person{name:"趙六"}), (n:Location{city:"北京"}) merge (p)-[:出生地]->(n);
match (p:Person)-[:出生地]->(n:Location{city:"北京"}) return p, n;
match (a)-->() return a;
match (a)--() return a;
match (a)-[:同學]-() return a;
match (p:Person{name:"張三"}) set p.sex = "男";
match (p:Person{name:"張三"}) remove p.sex;
match (p1:Person{name:"張三"})-[r]-(p2:Person{name:"田七"}) delete r;
match (p:Person{name:"田七"}) delete p;
match (p:Person) where(p.age>19)  return p

py2neo

安裝py2neo

pip install --user --upgrade py2neo

導入相關包

from py2neo import Graph, Node, Relationship
from py2neo.matching import *

連接Neo4j服務

graph = Graph('http://localhost:7474', auth=("neo4j", "19980912"))

删除所有節點

graph.delete_all()

建立節點

p1 = Node("Person", name="張三", age=18)
graph.create(p1)

建立關系

r1 = Relationship(p1, '弟弟', p2)
graph.create(r1)

增加屬性

p1['sex'] = '男'
graph.push(p1)

删除屬性

del p1['sex']
graph.push(p1)

查找節點

nodes = NodeMatcher(graph)
result1 = nodes.match("Person", name="張三").first()
print(result1)

查找符合條件的節點

result3 = nodes.match("Person").where("_.age > 19").all()
for p in result3:
    print(p)

查詢指定標簽所有節點

persons = nodes.match("Person").all()
print("查詢所有人")
for p in persons:
    print(p)

查詢節點之間的關系

# 查詢所有人包含的關系
matcher = RelationshipMatcher(graph)
result4 = matcher.match([p1], r_type=None).all()
for r in result4:
    print(r)

查詢指定關系
查詢到的結果需要用all()或first()轉化為可輸出的類型

result5 = matcher.match([p1, p2], r_type=None).all()
result6 = matcher.match([p2, p1], r_type=None).all()
for r in [result5, result6]:
    print(r)

cypher語句

result8 = graph.run("match (n) return n").to_series()
print(result8)
# 查詢所有出生在北京的人
result9 = graph.run("match (p:Person)-[:出生地]->(n:Location{city:'北京'}) return p, n").to_data_frame()
print(result9)

删除關系

graph.run(""" match (p1:Person{name:"張三"})-[r]-(p2:Person{name:"田七"}) delete r;"""
)

删除節點

# 删除節點
graph.delete(p5)

代碼匯總

from py2neo import Graph, Node, Relationship
from py2neo.matching import *

# 連接Neo4j
graph = Graph('http://localhost:7474', auth=("neo4j", "19980912"))
print(graph)

""" match (n) detach delete n; create (p:Person{name:"張三",age:18}); create (p1:Person{name:"李四",age:19}),(p2:Person{name:"王五",age:20}),(p3:Person{name:"趙六",age:20}),(p4:Person{name:"田七",age:22}); create (n:Location{city:"北京"}); create (n1:Location{city:"上海"}), (n2:Location{city:"廣州"}); """
# 删除所有節點
graph.delete_all()

# 建立節點
p1 = Node("Person", name="張三", age=18)
p2 = Node("Person", name="李四", age=19)
p3 = Node("Person", name="王五", age=20)
p4 = Node("Person", name="趙六", age=20)
p5 = Node("Person", name="田七", age=22)
graph.create(p1)
graph.create(p2)
graph.create(p3)
graph.create(p4)
graph.create(p5)
l1 = Node("Location", city="北京")
l2 = Node("Location", city="上海")
l3 = Node("Location", city="廣州")
graph.create(l1)
graph.create(l2)
graph.create(l3)

""" match (p1:Person{name:"張三"}), (p2:Person{name:"李四"}) merge (p1)-[:弟弟]->(p2); match (p2:Person{name:"李四"}), (p1:Person{name:"張三"}) merge (p2)-[:哥哥]->(p1); match (p1:Person{name:"張三"}), (p2:Person{name:"王五"}) merge (p1)-[:學生{time:2}]->(p2); match (p1:Person{name:"張三"}), (p2:Person{name:"王五"}) merge (p2)-[:老師{time:2}]->(p1); match (p1:Person{name:"張三"}), (p2:Person{name:"趙六"}) merge (p1)-[:朋友]->(p2); match (p1:Person{name:"張三"}), (p2:Person{name:"田七"}) merge (p1)-[:同學]->(p2); match (p:Person{name:"張三"}), (n:Location{city:"北京"}) merge (p)-[:出生地]->(n); match (p:Person{name:"李四"}), (n:Location{city:"上海"}) merge (p)-[:出生地]->(n); match (p:Person{name:"王五"}), (n:Location{city:"廣州"}) merge (p)-[:出生地]->(n); """
# 節點關系建立
r1 = Relationship(p1, '弟弟', p2)
r2 = Relationship(p2, '哥哥', p1)
r3 = Relationship(p1, '學生', p3)
r4 = Relationship(p3, '老師', p1)
r5 = Relationship(p1, '朋友', p4)
r6 = Relationship(p1, '同學', p5)
r7 = Relationship(p1, '出生地', l1)
r8 = Relationship(p2, '出生地', l2)
r9 = Relationship(p3, '出生地', l3)
r10 = Relationship(p4, '出生地', l1)
graph.create(r1)
graph.create(r2)
graph.create(r3)
graph.create(r4)
graph.create(r5)
graph.create(r6)
graph.create(r7)
graph.create(r8)
graph.create(r9)
graph.create(r10)

""" match (p:Person{name:"張三"}) set p.sex = "男"; match (p:Person{name:"張三"}) remove p.sex; """
# 增加屬性
p1['sex'] = '男'
graph.push(p1)
# 删除屬性
del p1['sex']
graph.push(p1)

""" match (n) return n; match (p:Person)-[:出生地]->(n:Location{city:"北京"}) return p, n; match (a)-->() return a; match (a)--() return a; match (a)-[:同學]-() return a; """
# 查找
nodes = NodeMatcher(graph)
result1 = nodes.match("Person", name="張三").first()
print(result1)

result2 = nodes.match("Person").all()
for person in result2:
    print(person)
print(len(result2))

# 模糊匹配
result3 = nodes.match("Person").where("_.age > 19").all()
for p in result3:
    print(p)

# 查詢所有人
persons = nodes.match("Person").all()
print("查詢所有人")
for p in persons:
    print(p)
# 查詢所有城市
locations = nodes.match("Location").all()
for l in locations:
    print(l)

# RelationshipMatcher查詢關系
# 查詢所有人包含的關系
matcher = RelationshipMatcher(graph)
result4 = matcher.match([p1], r_type=None).all()
for r in result4:
    print(r)

# 查詢張三和李四的關系
result5 = matcher.match([p1, p2], r_type=None).all()
result6 = matcher.match([p2, p1], r_type=None).all()
for r in [result5, result6]:
    print(r)

# 查詢所有出生地關系
result7 = matcher.match(None, r_type='出生地').all()
for r in result7:
    print(r)

# cypher語句
result8 = graph.run("match (n) return n").to_series()
print(result8)
# 查詢所有出生在北京的人
result9 = graph.run("match (p:Person)-[:出生地]->(n:Location{city:'北京'}) return p, n").to_data_frame()
print(result9)

# 删除關系
graph.run(""" match (p1:Person{name:"張三"})-[r]-(p2:Person{name:"田七"}) delete r;"""
)
# 删除節點
graph.delete(p5)

版權聲明
本文為[熱愛旅行的小李同學]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/204/202207221748414284.html

隨機推薦