Intro to Graphs
All the meetups
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)
Neo4j Fundamentals
• Nodes
• Relationships
• Properties
• Labels
CAR
Property Graph Model Components
Nodes
• Represent the objects in the graph
• Can be labeled
PERSON PERSON
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
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
Graphs are all around us
Can you identify nodes and relationships related to this meetup?
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
Cypher: Graph Query Language
MATCH (:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )
LOVES
Dan Ann
LABEL PROPERTY
NODE NODE
LABEL PROPERTY
Let's get started
With ASCII ART ¯_(ツ)_/¯
Nodes
() or (n)
• Surrounded with parentheses
• Use an alias n to refer to our node later in the query
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)
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
--> or -[r:TYPE]->
• Wrapped with hyphens & square brackets
• A relationship type starts with a colon :
Relationships
--> 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
• Used to query data
(n:Label {prop:'value'})-[:TYPE]->(m:Label)
Patterns
• Can you translate this pattern?
(p1:Person {name:'Alice'})-[:KNOWS]->(p2:Person {name:'Bob')
Patterns
• 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
Demo
Navigate to neo4j.com/download
Installing Neo4j
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
Type :play movies into the query bar:
The movies graph
Type :play movies into the query bar:
The movies graph
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
Building an application
Language Drivers
Java
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
</dependency>
Python
pip install neo4j-driver
.NET
PM> Install-Package Neo4j.Driver
JavaScript
npm install neo4j-driver
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
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()
Who’s using Neo4j?
How Customers Use Neo4j
Network &
Data Center
Master Data
Management
Social Recommendations
Identity &
Access
Search &
Discovery
GEO
What next?
If you like meetups...
meetup.com/graphdb-london
If you like reading...
graphdatabases.com
If you like videos...
youtube.com/neo4j
If you like getting your hands dirty...
neo4j.com/online_training/graphdatabases
If you get stuck....
http://stackoverflow.com/tags/neo4j
http://neo4j.com/slack
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

Introduction to Graphs with Neo4j

  • 1.
  • 2.
  • 6.
    What’s a graphdatabase? Simply put, a graph consists of nodes connected by relationships. Graph databases store data as graph structures (nodes and relationships)
  • 7.
    Neo4j Fundamentals • Nodes •Relationships • Properties • Labels
  • 8.
    CAR Property Graph ModelComponents Nodes • Represent the objects in the graph • Can be labeled PERSON PERSON
  • 9.
    CAR DRIVES Property Graph ModelComponents 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: May29, 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 allaround us Can you identify nodes and relationships related to this meetup?
  • 12.
    Summary of thegraph 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 QueryLanguage MATCH (:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} ) LOVES Dan Ann LABEL PROPERTY NODE NODE LABEL PROPERTY
  • 14.
    Let's get started WithASCII ART ¯_(ツ)_/¯
  • 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 toquery data (n:Label {prop:'value'})-[:TYPE]->(m:Label) Patterns
  • 21.
    • Can youtranslate this pattern? (p1:Person {name:'Alice'})-[:KNOWS]->(p2:Person {name:'Bob') Patterns
  • 22.
    • Find Alicewho 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
  • 23.
  • 24.
  • 25.
    Once you download, Forsetup 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
  • 26.
    Type :play moviesinto the query bar: The movies graph
  • 27.
    Type :play moviesinto the query bar: The movies graph
  • 28.
    Sample Queries totry 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
  • 29.
  • 30.
  • 31.
    Architecture and DataFlow 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 -> Neo4jexample 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()
  • 33.
  • 34.
    How Customers UseNeo4j Network & Data Center Master Data Management Social Recommendations Identity & Access Search & Discovery GEO
  • 35.
  • 36.
    If you likemeetups... meetup.com/graphdb-london
  • 37.
    If you likereading... graphdatabases.com
  • 38.
    If you likevideos... youtube.com/neo4j
  • 39.
    If you likegetting your hands dirty... neo4j.com/online_training/graphdatabases
  • 40.
    If you getstuck.... 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