La position actuelle:Accueil du site>[carte des connaissances] cql et py2neo notes d'apprentissage

[carte des connaissances] cql et py2neo notes d'apprentissage

2022-07-23 04:02:42Xiao Li aime voyager.

cqlAvecpy2neoNotes d'étude

CQL

Ouvertneo4jServices

neo4j.bat console

Le lien par défaut est http://llocalhost:7474

Supprimer les graphiques de la base de données

match (n) detach delete n

Créer un noeud

create (p:Person{name:"Zhang San",age:18})

Création de noeuds par lots

create (p1:Person{name:"Li - si.",age:19}),(p2:Person{name:"Wang Wu",age:20}),(p3:Person{name:"Zhao Liu",age:20}),(p4:Person{name:"Tian Qi",age:22})

Voir le graphique

match (n) return n

Insérer la description de l'image ici

Créer une relation

match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Li - si."}) merge (p1)-[:Mon frère.]->(p2);
match (p2:Person{name:"Li - si."}), (p1:Person{name:"Zhang San"}) merge (p2)-[:Mon frère]->(p1);

Créer une relation tout en ajoutant des attributs

match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Wang Wu"}) merge (p1)-[:Étudiants{
   time:2}]->(p2)

Insérer la description de l'image ici

Établir des relations entre les différentes étiquettes

match (p:Person{name:"Zhang San"}), (n:Location{city:"Beijing"}) merge (p)-[:Lieu de naissance]->(n);

Insérer la description de l'image ici

Consultez toutes les personnes nées à Pékin

match (p:Person)-[:Lieu de naissance]->(n:Location{city:"Beijing"}) return p, n

Insérer la description de l'image ici

Interroger tous les noeuds externes

match (a)-->() return a

Insérer la description de l'image ici

Interroger tous les noeuds liés

match (a)--() return a

Insérer la description de l'image ici

Interrogez tous les noeuds qui ont des relations avec les étudiants

match (a)-[:Camarade de classe]-() return a

Insérer la description de l'image ici

Ajouter/Modifier les propriétés

match (p:Person{name:"Zhang San"}) set p.sex = "Hommes"

Supprimer la propriété

match (p:Person{name:"Zhang San"}) remove p.sex

Supprimer la relation( Des relations spécifiques peuvent être spécifiées , Les noeuds peuvent être supprimés en même temps )

match (p1:Person{name:"Zhang San"})-[r]-(p2:Person{name:"Tian Qi"}) delete r

Supprimer le noeud

match (p:Person{name:"Tian Qi"}) delete p

Recherche conditionnelle

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

Insérer la description de l'image ici

Résumé

match (n) detach delete n;
create (p:Person{name:"Zhang San",age:18});
create (p1:Person{name:"Li - si.",age:19}),(p2:Person{name:"Wang Wu",age:20}),(p3:Person{name:"Zhao Liu",age:20}),(p4:Person{name:"Tian Qi",age:22});
create (n:Location{city:"Beijing"});
create (n1:Location{city:"Shanghai"}), (n2:Location{city:"Guangzhou"});
match (n) return n;
match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Li - si."}) merge (p1)-[:Mon frère.]->(p2);
match (p2:Person{name:"Li - si."}), (p1:Person{name:"Zhang San"}) merge (p2)-[:Mon frère]->(p1);
match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Wang Wu"}) merge (p1)-[:Étudiants{
   time:2}]->(p2);
match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Wang Wu"}) merge (p2)-[:Professeur.{
   time:2}]->(p1);
match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Zhao Liu"}) merge (p1)-[:Amis]->(p2);
match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Tian Qi"}) merge (p1)-[:Camarade de classe]->(p2);
match (p:Person{name:"Zhang San"}), (n:Location{city:"Beijing"}) merge (p)-[:Lieu de naissance]->(n);
match (p:Person{name:"Li - si."}), (n:Location{city:"Shanghai"}) merge (p)-[:Lieu de naissance]->(n);
match (p:Person{name:"Wang Wu"}), (n:Location{city:"Guangzhou"}) merge (p)-[:Lieu de naissance]->(n);
match (p:Person{name:"Zhao Liu"}), (n:Location{city:"Beijing"}) merge (p)-[:Lieu de naissance]->(n);
match (p:Person)-[:Lieu de naissance]->(n:Location{city:"Beijing"}) return p, n;
match (a)-->() return a;
match (a)--() return a;
match (a)-[:Camarade de classe]-() return a;
match (p:Person{name:"Zhang San"}) set p.sex = "Hommes";
match (p:Person{name:"Zhang San"}) remove p.sex;
match (p1:Person{name:"Zhang San"})-[r]-(p2:Person{name:"Tian Qi"}) delete r;
match (p:Person{name:"Tian Qi"}) delete p;
match (p:Person) where(p.age>19)  return p

py2neo

Installationpy2neo

pip install --user --upgrade py2neo

Importer des paquets connexes

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

ConnexionNeo4jServices

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

Supprimer tous les noeuds

graph.delete_all()

Créer un noeud

p1 = Node("Person", name="Zhang San", age=18)
graph.create(p1)

Établir des relations

r1 = Relationship(p1, 'Mon frère.', p2)
graph.create(r1)

Ajouter un attribut

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

Supprimer la propriété

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

Trouver des noeuds

nodes = NodeMatcher(graph)
result1 = nodes.match("Person", name="Zhang San").first()
print(result1)

Trouver les noeuds admissibles

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

Requête spécifiant l'étiquette tous les noeuds

persons = nodes.match("Person").all()
print("Demande à tout le monde")
for p in persons:
    print(p)

Interroger les relations entre les noeuds

#  Rechercher les relations que tout le monde contient 
matcher = RelationshipMatcher(graph)
result4 = matcher.match([p1], r_type=None).all()
for r in result4:
    print(r)

Requête pour spécifier la relation
Le résultat de la requête doit être all()Oufirst() Convertir en type exportable

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)

cypherDéclarations

result8 = graph.run("match (n) return n").to_series()
print(result8)
#  Demandez à tous ceux qui sont nés à Pékin 
result9 = graph.run("match (p:Person)-[:Lieu de naissance]->(n:Location{city:'Beijing'}) return p, n").to_data_frame()
print(result9)

Supprimer la relation

graph.run(""" match (p1:Person{name:"Zhang San"})-[r]-(p2:Person{name:"Tian Qi"}) delete r;"""
)

Supprimer le noeud

# Supprimer le noeud
graph.delete(p5)

Résumé des codes

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

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

""" match (n) detach delete n; create (p:Person{name:"Zhang San",age:18}); create (p1:Person{name:"Li - si.",age:19}),(p2:Person{name:"Wang Wu",age:20}),(p3:Person{name:"Zhao Liu",age:20}),(p4:Person{name:"Tian Qi",age:22}); create (n:Location{city:"Beijing"}); create (n1:Location{city:"Shanghai"}), (n2:Location{city:"Guangzhou"}); """
# Supprimer tous les noeuds
graph.delete_all()

# Créer un noeud
p1 = Node("Person", name="Zhang San", age=18)
p2 = Node("Person", name="Li - si.", age=19)
p3 = Node("Person", name="Wang Wu", age=20)
p4 = Node("Person", name="Zhao Liu", age=20)
p5 = Node("Person", name="Tian Qi", age=22)
graph.create(p1)
graph.create(p2)
graph.create(p3)
graph.create(p4)
graph.create(p5)
l1 = Node("Location", city="Beijing")
l2 = Node("Location", city="Shanghai")
l3 = Node("Location", city="Guangzhou")
graph.create(l1)
graph.create(l2)
graph.create(l3)

""" match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Li - si."}) merge (p1)-[:Mon frère.]->(p2); match (p2:Person{name:"Li - si."}), (p1:Person{name:"Zhang San"}) merge (p2)-[:Mon frère]->(p1); match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Wang Wu"}) merge (p1)-[:Étudiants{time:2}]->(p2); match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Wang Wu"}) merge (p2)-[:Professeur.{time:2}]->(p1); match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Zhao Liu"}) merge (p1)-[:Amis]->(p2); match (p1:Person{name:"Zhang San"}), (p2:Person{name:"Tian Qi"}) merge (p1)-[:Camarade de classe]->(p2); match (p:Person{name:"Zhang San"}), (n:Location{city:"Beijing"}) merge (p)-[:Lieu de naissance]->(n); match (p:Person{name:"Li - si."}), (n:Location{city:"Shanghai"}) merge (p)-[:Lieu de naissance]->(n); match (p:Person{name:"Wang Wu"}), (n:Location{city:"Guangzhou"}) merge (p)-[:Lieu de naissance]->(n); """
#  Établissement de relations nodales 
r1 = Relationship(p1, 'Mon frère.', p2)
r2 = Relationship(p2, 'Mon frère', p1)
r3 = Relationship(p1, 'Étudiants', p3)
r4 = Relationship(p3, 'Professeur.', p1)
r5 = Relationship(p1, 'Amis', p4)
r6 = Relationship(p1, 'Camarade de classe', p5)
r7 = Relationship(p1, 'Lieu de naissance', l1)
r8 = Relationship(p2, 'Lieu de naissance', l2)
r9 = Relationship(p3, 'Lieu de naissance', l3)
r10 = Relationship(p4, 'Lieu de naissance', 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:"Zhang San"}) set p.sex = "Hommes"; match (p:Person{name:"Zhang San"}) remove p.sex; """
# Ajouter un attribut
p1['sex'] = 'Hommes'
graph.push(p1)
# Supprimer la propriété
del p1['sex']
graph.push(p1)

""" match (n) return n; match (p:Person)-[:Lieu de naissance]->(n:Location{city:"Beijing"}) return p, n; match (a)-->() return a; match (a)--() return a; match (a)-[:Camarade de classe]-() return a; """
# Trouver
nodes = NodeMatcher(graph)
result1 = nodes.match("Person", name="Zhang San").first()
print(result1)

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

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

# Demande à tout le monde
persons = nodes.match("Person").all()
print("Demande à tout le monde")
for p in persons:
    print(p)
#  Consultez toutes les villes 
locations = nodes.match("Location").all()
for l in locations:
    print(l)

# RelationshipMatcherRecherche de relations
#  Rechercher les relations que tout le monde contient 
matcher = RelationshipMatcher(graph)
result4 = matcher.match([p1], r_type=None).all()
for r in result4:
    print(r)

#  Renseignez - vous sur la relation entre Zhang San et Li Si 
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)

#  Rechercher toutes les relations de lieu de naissance 
result7 = matcher.match(None, r_type='Lieu de naissance').all()
for r in result7:
    print(r)

# cypherDéclarations
result8 = graph.run("match (n) return n").to_series()
print(result8)
#  Demandez à tous ceux qui sont nés à Pékin 
result9 = graph.run("match (p:Person)-[:Lieu de naissance]->(n:Location{city:'Beijing'}) return p, n").to_data_frame()
print(result9)

# Supprimer la relation
graph.run(""" match (p1:Person{name:"Zhang San"})-[r]-(p2:Person{name:"Tian Qi"}) delete r;"""
)
# Supprimer le noeud
graph.delete(p5)

Mentions de copyright
Auteur de cet article [Xiao Li aime voyager.],Réimpression s’il vous plaît apporter le lien vers l’original, merci
https://fra.chowdera.com/2022/204/202207221748414284.html

Recommandé au hasard