6. What’s a graph database?
Simply put, a graph consists of nodes connected by relationships.
Graph databases store data as graph structures (nodes and relationships)
8. CAR
Property Graph Model Components
Nodes
• Represent the objects in the graph
• Can be labeled
PERSON PERSON
9. CAR
DRIVES
Property Graph Model Components
Nodes
• Represent the objects in the graph
• Can be labeled
Relationships
• Relate nodes by type and direction
LOVES
LOVES
LIVES WITH
OW
NS
PERSON PERSON
10. CAR
DRIVES
name: “Dan”
born: May 29, 1970
twitter: “@dan”
name: “Ann”
born: Dec 5, 1975
since:
Jan 10, 2011
brand: “Volvo”
model: “V70”
Property Graph Model Components
Nodes
• Represent the objects in the graph
• Can be labeled
Relationships
• Relate nodes by type and direction
Properties
• Name-value pairs that can go on
nodes and relationships.
LOVES
LOVES
LIVES WITH
OW
NS
PERSON PERSON
11. Graphs are all around us
Can you identify nodes and relationships related to this meetup?
12. Summary of the graph building blocks
• Nodes - Entities and complex value types
• Relationships - Connect entities and structure domain
• Properties - Entity attributes, relationship qualities, metadata
• Labels - Group nodes by role
13. Cypher: Graph Query Language
MATCH (:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )
LOVES
Dan Ann
LABEL PROPERTY
NODE NODE
LABEL PROPERTY
15. Nodes
() or (n)
• Surrounded with parentheses
• Use an alias n to refer to our node later in the query
16. Nodes
() or (n)
• Surrounded with parentheses
• Use an alias n to refer to our node later in the query
(n:Label)
• Specify a Label, starting with a colon :
• Used to group nodes by roles or types (similar to tags)
17. Nodes
() or (n)
• Surrounded with parentheses
• Use an alias n to refer to our node later in the query
(n:Label)
• Specify a Label, starting with a colon :
• Used to group nodes by roles or types (similar to tags)
(n:Label {prop: 'value'})
• Nodes can have properties
18. --> or -[r:TYPE]->
• Wrapped with hyphens & square brackets
• A relationship type starts with a colon :
Relationships
19. --> or -[r:TYPE]->
• Wrapped with hyphens & square brackets
• A relationship type starts with a colon :
< > Specify the direction of the relationship
-[:KNOWS {since: 2010}]->
• Relationships can have properties too!
Relationships
20. • Used to query data
(n:Label {prop:'value'})-[:TYPE]->(m:Label)
Patterns
21. • Can you translate this pattern?
(p1:Person {name:'Alice'})-[:KNOWS]->(p2:Person {name:'Bob')
Patterns
22. • Find Alice who knows Bob
• In other words:
• find Person named 'Alice'
• who KNOWS
• a Person named 'Bob'
(p1:Person {name:'Alice'})-[:KNOWS]->(p2:Person {name:'Bob'})
Patterns
25. Once you download,
For setup you will be asked to give a default location.
Click start! You are good to go.
Goto Browser type http://localhost:7474 Enter default password: neo4j
Instructions
28. Sample Queries to try out
// Find the Matrix
MATCH (movie:Movie {title:"The Matrix"})
RETURN movie
// Find the people who acted in any movie and return their name and
// the role they played in the movie
MATCH (actor:Person)-[rel:ACTED_IN]->(movie:Movie)
RETURN rel.roles, actor.name
31. Architecture and Data Flow
Application
Cypher Bolt Driver
Cypher Bolt Server
MATCH (a:Person)
WHERE a.name = 'Alice'
RETURN a.surname, a.age
{surname: 'Smith',
age: 33}
Parameterised
Cypher
Result
Stream
metadata
32. Driver -> Neo4j example
from neo4j.v1 import GraphDatabase, basic_auth
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
session.run("CREATE (a:Person {name:'Arthur', title:'King'})")
result = session.run("""MATCH (a:Person)
WHERE a.name = 'Arthur'
RETURN a.name AS name, a.title AS title""")
for record in result:
print("%s %s" % (record["title"], record["name"]))
session.close()
39. If you like getting your hands dirty...
neo4j.com/online_training/graphdatabases
40. If you get stuck....
http://stackoverflow.com/tags/neo4j
http://neo4j.com/slack
41. In summary
• Flexible, iterative way of modeling data
• 4 building blocks - nodes, relationships, properties, labels
• Cypher Query Language - executable ASCII art
• Language drivers - Java, Javascript, .NET, Python