The records in a graph database are called Nodes .
Nodes are connected through typed, directed Relationships.
Each single Node and Relationship can have named attributes referred to as Properties.
A Label is a name that organizes nodes into groups.
The flexibility of the graph model has allowed us to add new nodes and new relationships.
Relationships in a graph naturally form paths. Querying—or traversing—the graph involves following paths.
1. Introduction to Graph
Databases in term of Neo4j
By: Abdullah Hamidi
Seminar Teacher: Ms.Agnieszka Zielinska
2. Table of contents
• Graph Introduction
• Graph Database Management Systems
• Graph Database
• The Power of Graphs
• Weaknesses of Graphs
• Lack Relationships in Relational Databases
• Neo4j and Cypher Query Language
• Simple Cypher Query Examples
• Conclusion
• More on this Subject
3. Graph Introduction
• A set of nodes and the relationships that connect them.
• Graphs represent entities as nodes and the ways in which those entities
relate to the world as relationships.
4. Example of Graph
• Twiter
Figure 1 : Graph Databases (O’ Really) , page: 3
5. Graph Database Management System
• Is an online database management system with Create, Read, Update, and
Delete (CRUD) methods that expose a graph data model.
• They are normally optimized for transactional performance.
• Relationships are first-class citizens of the graph data model, unlike other
database management systems (Foreign Keys)
6. Graph Database
• The records in a graph database are called Nodes .
• Nodes are connected through typed, directed Relationships.
• Each single Node and Relationship can have named attributes referred to
as Properties.
• A Label is a name that organizes nodes into groups.
8. Graph Database
• Nodes:
• Nodes are used to represent entities like actors, directors and movies (in
a movie and actor graph database).
• Some nodes
(a) actors
(m) movies
() some anonymous nodes
9. Graph Database
• Relationships:
• Relationships are used to connect nodes.
(a)-[r]->(m) actors having a relationship referred to as "r" to movies
(a)-[:ACTED_IN]->(m) actors that ACTED_IN some movie
• Properties
• Nodes with properties
(m {title:"The Matrix"}) Movie with a title property
10. Graph Database
• Labels
• Are used to distinguish between nodes
(a:Person) a Person
11. The power of Graphs
• Performance
• Connected data versus relational databases and NOSQL stores.
• Performance remains relatively constant, even as the dataset grows.
• The execution time for each query is proportional only to the size of the part of the graph
traversed, rather than the size of the overall graph.
• Flexibility
• Graphs are naturally additive, meaning we can add new kinds of relationships, new nodes,
and new sub graphs to an existing structure without disturbing existing queries and
application functionality
12. Weaknesses of Graph Databases
• Maturity or levels of Support
• Less support
• Less market
• Ease of Programming
• Is a little bit harder than Mysql or other Relational Databases (Resolved by Neo4j)
• Does not have built in security and assumed as a trusted environment
13. Lack Relationships in Relational Databases
• Relationships do exist in relational databases as joining tables
• The relational model becomes burdened with large join tables
• The rise in connectedness translates in the relational world into increased
joins in response to changing business needs.
• As data size increases, the query runtime increases too.
14. Understand the cost of performing connected
queries
Figure 4 : Graph Databases (O’ Really) , page: 13
15. Understand the cost of performing connected
queries
• Who are Bob’s friends?
Figure 5 : Graph Databases (O’ Really) , page: 13
• Alice and Zach
16. Understand the cost of performing connected
queries
• Who is friends with Bob?
• Alice
Figure 6 : Graph Databases (O’ Really) , page: 13
17. Undrestand the cost of performing connected
queries
• Alice’s friends-of-friends
• Who are my friends-of-friends-of-friends? Figure 7 : Graph Databases (O’ Really) , page: 13
18. But a Graph Approach
Figure 8 : Graph Databases (O’ Really) , page: 19
19. A Graph Approach
• The flexibility of the graph model has allowed us to add new nodes and new
relationships.
• We can see who LOVES whom , who is a COLLEAGUE_OF of whom, and
who is BOSS_OF them all.
• Relationships in a graph naturally form paths. Querying—or traversing—the
graph involves following paths.
20. An Experiment
• Partner and Vukotic’s experiment seeks to find friends-of-friends in a social
network, to a maximum depth of five.
• For a social network containing 1,000,000 people
• Each with approximately 50 friends
22. Neo4J and Cypher
• Neo4j is a Database - use it to reliably store information and find it later
• Neo4j's data model is a Graph
• Opensource
• Written in Java
• Brief History
• Official v1.0 2010
• Current Version 2.4
Neo4j
Graph
Database
23. James
Marry
BMW
Name:Marry
Age: 22
Brand: BMW
Model: 2004a
Name: James
Age: 26
Purchased_at: 12-01-2001
24. Traversal in Graph
Figure 9: What is Graph Database, slideshare.com, page: 82
25. Traversal in Graph
Figure 10: What is Graph Database, slideshare.com, page: 83
26. Neo4J and Cypher
• Querying Language
• Cypher is Neo4j's graph query language (SQL for graphs!)
• Cypher is a declarative query language
• Cypher is meant to be very readable and expressive
• Similar to SQL
• Based on Graph traversal
27. Cypher Query Language
• Cypher is a declarative, SQL inspired language for describing patterns in
graphs.
Figure 11: http://neo4j.com/graphacademy/online-course/
28. Some Simple Queries on Movie and Actor
Database
MATCH(n)(m)
RETURN n, m; Returns all records which an actor has relationship with movies
29. Some Simple Queries on Movie and Actor
Database
MATCH(n)()
RETURN n; Returns all records or nodes that are actors
30. Some Simple Queries on Movie and Actor
Database
MATCH(actor)-[:ACTED_IN]->(movie)
RETURN actor.name, movie.title;
Returns all actor names and movie titles that each actor acted in
31. Some Simple Queries on Movie and Actor
Database
MATCH(actor:Person)-[:ACTED_IN]->(m:Movie)
WHERE actor.name = “Tom Hanks”
RETURN actor, m;
Returns Tom Hanks with its properties and all the movies he acted in.
32. Some Simple Queries on Movie and Actor
Database
MATCH(actor:Person)-[role:ACTED_IN]->(m:Movie)
WHERE m.title= “The Matrix”
RETURN role.roles, actor.name, m;
Returns all the actors with their roles they played in Matrix movie.
33. Other Queries Using Cypher
• Querying the graph
• MATCH
• WHERE
• RETURN
• ORDER BY
• SKIP/LIMIT
34. Other Queries Using Cypher
• Updating the graph
• CREATE: Creates nodes and relationships.
• MERGE: Creates nodes uniquely.
• CREATE UNIQUE: Creates relationships uniquely.
• DELETE: Removes nodes, relationships.
• SET: Updates properties and labels.
• REMOVE: Removes properties and labels.
• FOREACH: Performs updating actions once per element in a list.
• WITH: Divides a query into multiple, distinct parts and passes results from one to the
next.
35. Conclusion
• The world is a Graph.
• Natural way to model almost everything
• “Whiteboard Friendly”
• Even the internet is a Graph
• Relational Databases are not so great for storing graph structures
• Expensive joins
• Expensive lookups during graph traversals
• Graph Databases fix these
• Efficient Storage
• Direct pointers and no joins
•
36. More on this subject:
• http://neo4j.com/graphacademy/online-course/
• Free Online Courses
• Graph Databases by Ian Robinson, Jim Webber and Emil Eifrem (O’Reilly)
• A Comparison of a Graph Database and a Relational Database