This document discusses graphs and Neo4j. It begins with definitions of vertices, edges, and graphs. It then shows examples of creating a graph representing the Smith family relationships in Neo4j using Cypher queries. These queries create nodes for family members and relationships between them like parent/child and marriage. It asks questions like who are someone's parents, siblings, or cousins that can be answered by running Cypher queries on the graph.
6. SOME FEATURES
#Big Data tools
#Data Center and
Cloud Software
#Technology of
the Year
•
•
•
•
•
•
•
•
•
Totalmente Transacional (ACID)
Tem controle de concorrência
Armazenamento próprio baseado em discos
Escalável para múltiplas máquinas
Possui uma linguagem intuitiva (Cypher)
Possui interface RESTFul
Possui API nativa em Java
Pode ser distribuído como um servidor Standalone
Pode ser embutido na aplicação
13. CYPHER – Creating the basic relationship between the
Smiths 1/2
MATCH (dad:People {name:"Joseph Smith" })
MATCH (mom:People {name:"Joan Smith" })
MATCH (me:People {name:"John Smith" })
MATCH (sister:People {name:"Josephine Smith" })
MATCH (bro:People {name:"Jonathan Smith" })
CREATE (me)-[:IS_SON_OF]->(dad)
CREATE (me)-[:IS_SON_OF]->(mom)
CREATE (sister)-[:IS_SON_OF]->(dad)
CREATE (sister)-[:IS_SON_OF]->(mom)
CREATE (bro)-[:IS_SON_OF]->(dad)
14.
15. CYPHER – Creating the basic relationship between the
Smiths 2/2
MATCH (sister:People {name:"Josephine Smith" })
MATCH (cousin:People {name:"Cousine Smith Oliver" })
MATCH (me:People {name:"John Smith" })
MATCH (wife:People {name:"Jennifer K. L. Smith" })
MATCH (children1:People {name:"Jen-El Smith" })
MATCH (children2:People {name:"Jil-El Smith" })
MATCH (children3:People {name:"Juju-El Smith" })
MATCH (children4:People {name:"Jor-El Smith" })
CREATE (cousin)-[:IS_SON_OF]->(sister)
CREATE (me)-[:IS_MARRIED_WITH]->(wife)
CREATE (children1)-[:IS_SON_OF]->(me)
CREATE (children2)-[:IS_SON_OF]->(me)
CREATE (children2)-[:IS_SON_OF]->(wife)
CREATE (children3)-[:IS_SON_OF]->(me)
CREATE (children3)-[:IS_SON_OF]->(wife)
CREATE (children4)-[:IS_SON_OF]->(me)
CREATE (children4)-[:IS_SON_OF]->(wife)
16.
17. SOME QUESTIONS!!!
1) Quem são os pais de John Smith?
MATCH (me:People {name:"John Smith" })
MATCH (me)-[:IS_SON_OF]->(parents)
RETURN parents.name
2) Quem são os filhos de John Smith?
MATCH (me:People {name:"John Smith" })
MATCH (me)<-[:IS_SON_OF]-(children)
RETURN children.name
3) Quem são os irmãos de John Smith?
MATCH (me:People {name:"John Smith" })
MATCH (me)-[:IS_SON_OF]->(parents)<-[:IS_SON_OF]-(brothers)
RETURN DISTINCT brothers.name
4) Quem são os irmãos de John Smith do mesmo pai e da mesma mãe?
MATCH (me:People {name:"John Smith" })
MATCH (me)-[:IS_SON_OF]->(parents)<-[:IS_SON_OF]-(brothers)
WITH me, brothers, count(parents) as parents_count
WHERE parents_count = length(me-[:IS_SON_OF]->())
RETURN brothers.name
18. 5) Quem são os primos de Cousine?
MATCH (cousin:People {name:"Cousine Smith Oliver" })
MATCH (cousin)-[:IS_SON_OF]->(mysister)
MATCH (mysister)-[:IS_SON_OF]->(ourparents)<-[:IS_SON_OF]-(brothers_of_my_sister)
MATCH (brothers_of_my_sister)<-[:IS_SON_OF]-(sons_of_brothers_of_my_sister)
RETURN DISTINCT sons_of_brothers_of_my_sister.name
Para finalizar, vamos deixar um pouco mais
complexo!
19. 6) Quem são os primos de Cousine que torcem para o Chelsea?
6.1 – Creating Nodes
MATCH (children1:People {name:"Jen-El Smith" })
MATCH (children4:People {name:"Jor-El Smith” })
CREATE (chelsea:SoccerTeam { name: “Chelsea”})
CREATE (children1)-[:IS_FAN_OF]->(chelsea)
CREATE (children4)-[:IS_FAN_OF]->(chelsea)
6.2 MATCH
MATCH
MATCH
MATCH
MATCH
WHERE
Query
(cousin:People {name:"Cousine Smith Oliver" })
(cousin)-[:IS_SON_OF]->(mysister)
(mysister)-[:IS_SON_OF]->(ourparents)<-[:IS_SON_OF]-(brothers_of_my_sister)
(brothers_of_my_sister)<-[:IS_SON_OF]-(sons_of_brothers_of_my_sister)
(sons_of_brothers_of_my_sister)-[:IS_FAN_OF]->(soccer_team)
soccer_team.name = "Chelsea"