Mastering Neo4j A Graph Database Data Masters
Special Thanks To… Planet Linux Caffe
We now have a sponsor!! John Jadvani
Short bio about me… Andrew Simkovsky 15 years working with database technology Oracle, MySQL/MariaDB, SQL Server, Postgres Redis, MongoDB, CouchDB, Cassandra, Neo4j Worked across many industries Consulting, Retail, Telecommunications, Energy Data, Marketing, Gaming, Health care
DBTekPro
Graph Databases
What is a graph database? Based on graph theory Data is stored as “nodes”, and relationships as “edges” Nodes have attributes. Relationships have attributes
Relational vs Graph Relational Relationships are used to join entities together to get results Data integrity is enforced through constraints Graph Relationships are considered data Can easily “walk” from node to node using relationships Can “walk” the graph in multiple directions in parallel
Relational Model actormovieactor_movie 1, Keanu Reeves 2, Carrie-Ann Moss 3, Lawrence Fishburne actor_id name movie_id title release_date actor_id movie_id role 1, The Matrix 2, The Matrix Reloaded 3, The Matrix Revolutions 1, 1, Neo 1, 2, Neo 1, 3, Neo 2, 1, Trinity 2, 2, Trinity 2, 3, Trinity 3, 1, Morpheus 3, 2, Morpheus 3, 3, Morpheus
Graph Model The Matrix The Matrix 2 The Matrix 3 Keanu Reeves Carrie- Ann Moss Lawrence Fishburne
Graph Model - Detailed type: Actor name: “Keanu Reeves” type: Movie title: “Matrix” :ACTS_IN type: Role, role: “Neo” Node Relationship PropertyValue Property Path
Example Graphs
Graph Databases – Examples Uses Social relationships Actor / movie relationships Medicine interactions Just some of the many possibilities!
Cypher A Graph Query Language
Queries START [MATCH] [WITH] [WHERE] RETURN [ORDER BY] [SKIP] [LIMIT] Starting node(s) Matching pattern (except properties) Filter on properties Properties to return Further conditions / calculations
Query Example START user=node:nodeIndexName(name={“Bob”}) MATCH (user)-[:FRIEND]->(friend) WITH user, count(friend) as friends WHERE friends > 10 RETURN friend.name ORDER BY friend.name LIMIT 100
Query Parts START user=node:nodeIndexName(name={“Bob”}) made-up identifier object type node property identifier index namepropertyvalue
Query Parts MATCH (user)-[:FRIEND]->(friend) node relationship definition (including direction) Another made-up identifier
Query Parts WITH user, count(friend) as friends grouping node calculation alias
Query Parts WHERE friends > 10 property filter
Query Parts RETURN friend.name ORDER BY friend.name LIMIT 100
Query Example – All Together START user=node:nodeIndexName(name={“Bob”}) MATCH (user)-[:FRIEND]->(friend) WITH user, count(friend) as friends WHERE friends > 10 RETURN friend.name ORDER BY friend.name LIMIT 100 This will return all nodes where they are friends with Bob, and that Bob has more than 10 friends, but limit to the first 100 found.
Query Example – Matching START user=node:nodeIndexName(name={“Bob”}) MATCH (user)-[:FRIEND {since: “ ”} ]->(friend) RETURN friend.name Since relationships have properties, we can search on them too!!
Creating Data CREATE [UNIQUE]* [SET | DELETE | FOREACH]* [RETURN [ORDER BY][SKIP][LIMIT]]
Creating Data - Examples CREATE (n {name: “Bob”}) made-up identifier again propertyvalue
Creating Data - Examples CREATE (b {name: “Bob”}), (m {name: “Mary”}), (b)-[:KNOWS]->(m) Creates two nodes with a relationship between them. This is called creating a path.
Creating Data - Examples You can combine queries with create options! This allows you to create parts of the graph starting from a particular node (or nodes)! START b=node:nodeIndexName(name={“Bob”}), m=node:nodeIndexName(name={“Mary”}) MATCH (b)-[:KNOWS]->(m) CREATE (b)-[:LOVES]->(m)
Live Demo!
Where Do I Get It?
Thank You For Coming! Please rate this Meet Up: (or go there to join!) Check out my blog and forums: After Party!! Miller’s Ale House Miracle Mile