19/08/2015 1 Graph Databases and Neo4j Kevin Swingler Relationships • We all know how a relational database models relationships • But there are limitations to the approach – Relationships can’t have a type, or any properties – Permissible relationships (PK and FK) are strictly defined and cannot be added in an ad-hoc way – Must be implemented within the relational model
14
Embed
Graph Databases and Neo4j - University of Stirling · Graph Databases and Neo4j Kevin Swingler Re˜ t˙ nsh˙ s • We all know how a relational database models relationships •
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
19/08/2015
1
Graph Databases and Neo4j
Kevin Swingler
Relationships
• We all know how a relational database models
relationships
• But there are limitations to the approach
– Relationships can’t have a type, or any properties
– Permissible relationships (PK and FK) are strictly
defined and cannot be added in an ad-hoc way
– Must be implemented within the relational model
19/08/2015
2
Flexible Relationships
• Imagine a set of objects that can have
arbitrary properties and arbitrary relationships
between the objects
Animal:Fish
Lives in:Water
Moves:Swims
Animal:Cat
Lives in:Land
Blood:Warm
Eats
Likes:a lot
Main Graph DB Features
• Each entity (object) can have different properties, just like a document database
• Any entity can have a relationship with any other entity
• Relationships have a type, and any pair of entities can have a relationship of any type
• Relationships have properties, so can be thought of as entities that join other entities
• Entity pairs can have more than one relationship
19/08/2015
3
Anatomy of a Graph DB
• Nodes represent entities, for example people in a social network or an organisation
• Edges represent relationships, e.g ‘Works for’
• Edges are directional: A works for B doesn’t mean B works for A
• So relationships are INCOMING or OUTGOING in respect to a node
• Edges have properties: A works for B: since 2003, as secretary
Labels, Types and Properties
• Nodes
– Label: E.g. Person, Movie
– Properties: E.g. Name, Age
• Edges
– Type: E.g. Works for, Loves
– Properties: E.g. Since when, how much
19/08/2015
4
Example
Relationship Depth
• Relationship depth measures the steps
between one entity and another
• For example Friend is depth 1, friend of a
friend is depth 2, etc.
19/08/2015
5
Graph Traversal
• Traversing a graph draws out relationship
• Traversing means moving from one node to
another along the relationship edges
• As a node can have more than one
relationship, traversal is not trivial
• There are algorithms that try to optimise the
traversal of a graph
Traversal Type
• A graph traversal starts with a chosen node,
either a specified root, or any given node
• It can follow INCOMING or OUTGOING nodes,
so go in either direction
• Useful for asking “Who works for A?” or “Who
does B report to?”
• Can traverse DEPTH_FIRST or BREADTH_FIRST
19/08/2015
6
DEPTH or BREADTH First
• From a node with multiple edges, leading to
long paths:
– Depth first follows the first path to its end, then
returns and follows the second ...
– Breadth first follows all the first steps first, then
lists the depth 2 paths, and so on
Example (Right to Left)
From Morpheus, starting with the edge to Reagan, following KNOWS
Breadth first order is (Reagan), (Trinity), (Reagan – Agent Smith)
Depth first is (Reagan – Agent Smith), (Trinity)
19/08/2015
7
Indexing
• Nodes and edges are indexed to speed
searches for single entities and relationships
• Means graph doesn’t need to be traversed
Index
Neo
Loves
Trinity
Loves
Why Use a Graph?
• Many data structures are examples of graphs:
– Linked lists
– Trees
– Maps
• So a graph is a generic data structure
• One way to address the impedence mismatch we discussed in lecture 1 – that objects in your java don’t match the structure of a DB