SlideShare a Scribd company logo
INTRODUCTION TO GRAPH
DATABASES WITH NEO4J
Brant Boehmann
@tbrantb
2DEVSPACE 2017 | @tbrantb
About Me
- ~20 Years Dev Experience
- Java/JVM Specialist
- Lead Software Engineer @ScrippsNet
3DEVSPACE 2017 | @tbrantb
Outline
- DB Types
- What is a GraphDB
- RDBMS vs GraphDB
- Neo4J
- Cypher
- Neo4J Console
4DEVSPACE 2017 | @tbrantb
Multiple DB Types
- Relational
- Oracle, MS-SQL, PostgreSQL, MySQL, ...
- Columnar
- GreenPlum, RedShift, ...
- Key-Value
- Dynamo, Riak, ...
- Document
- MongoDB, CouchDB, ...
- Graph
- Neo4J, JanusDB, OrientDB, TigerGraph, ...
5DEVSPACE 2017 | @tbrantb
Which DB type is right?
- Data Size
- Data structure
- Access Patterns
- Write Patterns
- Performance Requirements
- Consistency Requirements
- Degree of Data Connectedness
6DEVSPACE 2017 | @tbrantb
Remember Graph Basics
- Node (Vertex)
- Relationship (Edge)
- Direction
- Path
A
D
B
C E
7DEVSPACE 2017 | @tbrantb
Turn a Graph into a DB
- Node Labels
- Relationship Type
- Properties
8DEVSPACE 2017 | @tbrantb
Shoehorn into RDBMS Attempt #1
- Convert a Graph to Table
- Adjacency Matrix
9DEVSPACE 2017 | @tbrantb
Shoehorn into RDBMS Attempt #1
- Adjacency Matrix Doesn’t fit in RDBMS table :(
- Why?
- Schema / Static Columns
- Column growth
10
Shoehorn into RDBMS Attempt #2
- Join Table
- Many-To-Many
11DEVSPACE 2017 | @tbrantb
Who are Bob’s Friends
Pretty Simple
SELECT p2.id, p2.name
FROM person p1
JOIN person_relationship rel
ON p1.id = rel.start_person_id
AND p1.id = 1 --Bob
AND rel.relationship_type=1 --Friend
JOIN person p2
ON p2.id = rel.end_person_id
12DEVSPACE 2017 | @tbrantb
Who are the Friends of Bob’s Friends?
SELECT p3.id, p3.name
FROM person p1
JOIN person_relationship rel1
ON p1.id = rel1.start_person_id
AND p1.id = 1 --Bob
AND rel1.relationship_type = 1 --Friend
JOIN person p2
ON p2.id = rel1.end_person_id
JOIN person_relationship rel2
ON p2.id = rel2.start_person_id
AND rel2.relationship_type = 1 --Friend
JOIN person p3
ON p3.id = rel2.end_person_id
13
Bob’s Friends’ Friends’ Friends to the N
Do I have to?
14DEVSPACE 2017 | @tbrantb
What is Bob’s Bacon # ?
?
15DEVSPACE 2017 | @tbrantb
Not all problems
are well suited
for RDBMS
16DEVSPACE 2017 | @tbrantb
How do Graph DBs do this better?
- RDBMS uses joins & table/index scans
- Native vs Non-Native GraphDB
- “Index Free Adjacency”
17DEVSPACE 2017 | @tbrantb
Popular GraphDB Use Cases
- Social Networks
- Fraud Detection
- Recommendation Engines
- IAM
- Master Data Management (MDM)
- Network Ops
- Content/Asset Management
- Journalism (see Panama Papers)
18DEVSPACE 2017 | @tbrantb
Algorithms/Concepts
- Shortest Path
- A*
- Triadic Closures
19DEVSPACE 2017 | @tbrantb
Neo4J
- Labeled Property Graph
- Native Graph DB w/Index Free Adjacency
- JVM Based
- Fully ACID
- All relationships are directed *
- Schema-less, Schema-optional
- Embedded, Single Instance Server, HA Cluster
20DEVSPACE 2017 | @tbrantb
Neo4J APIs
- Embedded Java API
- HTTP API
- Drivers/Bolt Protocol
21DEVSPACE 2017 | @tbrantb
Cypher
- Declarative Graph Query Language
- Originated at Neo Technology
- Standardizing at OpenCypher.org
- Ascii Art-ish
22DEVSPACE 2017 | @tbrantb
Ascii Art-ish ?
- () look like circles or nodes
- --, -->, <-- look like relationships or edges
23
MATCH (a)-->(b)
WHERE a.name = 'Bob'
RETURN b
DEVSPACE 2017 | @tbrantb
MATCH Clause
24
MATCH (a)-->(b)
MATCH (a)-[:FRIEND]->(b)
MATCH (a)<-[:FRIEND]-(b)
MATCH (a)-[:FRIEND]-(b)
DEVSPACE 2017 | @tbrantb
MATCH Clause with Labels
25
MATCH (p:Person)-[:DIRECTED]->(m:Movie)
MATCH (a:Person)-[:ACTED_IN]->(m:Movie)<-[:DIRECTED]-(d:Person)
DEVSPACE 2017 | @tbrantb
WHERE
26
MATCH (p:Person)-[:DIRECTED]->(m:Movie)
WHERE p.name = 'Ron Howard'
AND p.born < 1955
MATCH (p:Person {name: 'Ron Howard'})-[:DIRECTED]->(m:Movie)
DEVSPACE 2017 | @tbrantb
RETURN, ORDER BY, LIMIT
27
MATCH (p:Person {name: 'Ron Howard'})-[:DIRECTED]->(m:Movie)
RETURN m.title, m.released, m.tagline
ORDER BY m.title
LIMIT 3
DEVSPACE 2017 | @tbrantb
Overall Read Query Structure
28
[MATCH WHERE]
[OPTIONAL MATCH WHERE]
[WITH [ORDER BY] [SKIP] [LIMIT]]
RETURN [ORDER BY] [SKIP] [LIMIT]
DEVSPACE 2017 | @tbrantb
CREATE
29
CREATE (matrix:Movie {title:'The Matrix', released:1999})
CREATE (keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (keanu)-[:ACTED_IN]->(matrix)
MATCH (matrix:Movie {title:'The Matrix'}),
(keanu:Person {name:'Keanu Reeves'})
CREATE (keanu)-[:ACTED_IN]->(matrix)
DEVSPACE 2017 | @tbrantb
UPDATE
30
MATCH (matrix:Movie {title:'The Matrix'}),
(keanu:Person {name:'Keanu Reeves'})
SET matrix.tagline = 'Follow the White Rabbit',
keanu.born = 1900
RETURN matrix, keanu //optional
DEVSPACE 2017 | @tbrantb
DELETE
31
MATCH (p:Person{name: 'Keanu Reeves'})
DELETE p
MATCH (p:Person{name: 'Keanu Reeves'})-[r]-()
DELETE r, p
MATCH (p:Person{name: 'Keanu Reeves'})
DETACH DELETE n
DEVSPACE 2017 | @tbrantb
Remember our friends?
32DEVSPACE 2017 | @tbrantb
Who are Bob’s Friends
SELECT p2.id, p2.name
FROM person p1
JOIN person_relationship rel
ON p1.id = rel.start_person_id
AND p1.id = 1 --Bob
AND rel.relationship_type=1 --Friend
JOIN person p2
ON p2.id = rel.end_person_id
33
MATCH (b:Person {name:'Bob'})-[:FRIEND]->(f:Person)
RETURN f.name
DEVSPACE 2017 | @tbrantb
Who are the Friends of Bob’s Friends?
SELECT p3.id, p3.name
FROM person p1
JOIN person_relationship rel1
ON p1.id = rel1.start_person_id
AND p1.id = 1 --Bob
AND rel1.relationship_type = 1 --Friend
JOIN person p2
ON p2.id = rel1.end_person_id
JOIN person_relationship rel2
ON p2.id = rel2.start_person_id
AND rel2.relationship_type = 1 --Friend
JOIN person p3
ON p3.id = rel2.end_person_id
34
MATCH (b:Person {name:'Bob'})-[:FRIEND]->(f:Person)-[:FRIEND]->(fof:Person)
RETURN DISTINCT fof.name
MATCH (b:Person {name:'Bob'})-[:FRIEND *2]->(fof:Person)
RETURN DISTINCT fof.name
DEVSPACE 2017 | @tbrantb
Bob’s Friends’ Friends’ Friends to the N
35
MATCH (b:Person {name:'Bob'})-[:FRIEND *2]->(fof:Person)
RETURN DISTINCT fof.name
MATCH (b:Person {name:'Bob'})-[:FRIEND *]->(fof:Person)
RETURN DISTINCT fof.name
MATCH (b:Person {name:'Bob'})-[:FRIEND *..5]->(fof:Person)
RETURN DISTINCT fof.name
MATCH (b:Person {name:'Bob'})-[:FRIEND *2..5]->(fof:Person)
RETURN DISTINCT fof.name
DEVSPACE 2017 | @tbrantb
What is Bob’s Bacon # ?
36
MATCH path=shortestPath(
(kb:Person {name:'Kevin Bacon'})-[*]-(b:Person {name:'Bob'})
)
RETURN length(path)
DEVSPACE 2017 | @tbrantb
Resources
- Developer Website
- https://neo4j.com/developer/
- Neo4J Docs
- https://neo4j.com/docs/
- Cypher Refcard
- https://neo4j.com/docs/cypher-refcard/
- Neo4J Book (free)
- Me @tbrantb on twitter
37

More Related Content

PDF
The 2nd graph database in sv meetup
PDF
Pg Conf - Implementing Graph Database based-on PostgreSQL
PDF
Graph database in sv meetup
PDF
GDB in SV_1st_meetup_09082016
PDF
GDBinSV_Meetup_DBMS_Trends_10062016
PDF
Nebula Graph nMeetup in Shanghai - Meet with Graph Technology Enthusiasts
PDF
Introduction to Nebula Graph, an Open-Source Distributed Graph Database
PPTX
CRS and SVG
The 2nd graph database in sv meetup
Pg Conf - Implementing Graph Database based-on PostgreSQL
Graph database in sv meetup
GDB in SV_1st_meetup_09082016
GDBinSV_Meetup_DBMS_Trends_10062016
Nebula Graph nMeetup in Shanghai - Meet with Graph Technology Enthusiasts
Introduction to Nebula Graph, an Open-Source Distributed Graph Database
CRS and SVG

Similar to Introduction to Graph Databases wth neo4J (20)

PDF
Introduction to Graph Databases with Neo4J
PDF
Pg no sql_beatemjoinem_v10
PDF
Neo4j Morpheus: Interweaving Documents, Tables and and Graph Data in Spark wi...
PPT
Processing Large Graphs
PDF
Cascading Map-Side Joins over HBase for Scalable Join Processing
PDF
Brisk hadoop june2011_sfjava
PDF
PGQL: A Language for Graphs
PDF
Eifrem neo4j
KEY
Taming NoSQL with Spring Data
PPTX
Chengqi zhang graph processing and mining in the era of big data
PDF
Brisk hadoop june2011
PPTX
Json data modeling june 2017 - pittsburgh tech fest
ODP
How do You Graph
PPTX
Graph Databases
PDF
What Makes Graph Queries Difficult?
PPTX
Azure machine learning
PDF
AgensGraph Presentation at PGConf.us 2017
PPTX
Introduction to SQL Server Graph DB
PDF
Bringing the Semantic Web closer to reality: PostgreSQL as RDF Graph Database
PDF
Bicod2017
Introduction to Graph Databases with Neo4J
Pg no sql_beatemjoinem_v10
Neo4j Morpheus: Interweaving Documents, Tables and and Graph Data in Spark wi...
Processing Large Graphs
Cascading Map-Side Joins over HBase for Scalable Join Processing
Brisk hadoop june2011_sfjava
PGQL: A Language for Graphs
Eifrem neo4j
Taming NoSQL with Spring Data
Chengqi zhang graph processing and mining in the era of big data
Brisk hadoop june2011
Json data modeling june 2017 - pittsburgh tech fest
How do You Graph
Graph Databases
What Makes Graph Queries Difficult?
Azure machine learning
AgensGraph Presentation at PGConf.us 2017
Introduction to SQL Server Graph DB
Bringing the Semantic Web closer to reality: PostgreSQL as RDF Graph Database
Bicod2017
Ad

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Programs and apps: productivity, graphics, security and other tools
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Review of recent advances in non-invasive hemoglobin estimation
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine learning based COVID-19 study performance prediction
Machine Learning_overview_presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
A comparative analysis of optical character recognition models for extracting...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Ad

Introduction to Graph Databases wth neo4J

  • 1. INTRODUCTION TO GRAPH DATABASES WITH NEO4J Brant Boehmann @tbrantb
  • 2. 2DEVSPACE 2017 | @tbrantb
  • 3. About Me - ~20 Years Dev Experience - Java/JVM Specialist - Lead Software Engineer @ScrippsNet 3DEVSPACE 2017 | @tbrantb
  • 4. Outline - DB Types - What is a GraphDB - RDBMS vs GraphDB - Neo4J - Cypher - Neo4J Console 4DEVSPACE 2017 | @tbrantb
  • 5. Multiple DB Types - Relational - Oracle, MS-SQL, PostgreSQL, MySQL, ... - Columnar - GreenPlum, RedShift, ... - Key-Value - Dynamo, Riak, ... - Document - MongoDB, CouchDB, ... - Graph - Neo4J, JanusDB, OrientDB, TigerGraph, ... 5DEVSPACE 2017 | @tbrantb
  • 6. Which DB type is right? - Data Size - Data structure - Access Patterns - Write Patterns - Performance Requirements - Consistency Requirements - Degree of Data Connectedness 6DEVSPACE 2017 | @tbrantb
  • 7. Remember Graph Basics - Node (Vertex) - Relationship (Edge) - Direction - Path A D B C E 7DEVSPACE 2017 | @tbrantb
  • 8. Turn a Graph into a DB - Node Labels - Relationship Type - Properties 8DEVSPACE 2017 | @tbrantb
  • 9. Shoehorn into RDBMS Attempt #1 - Convert a Graph to Table - Adjacency Matrix 9DEVSPACE 2017 | @tbrantb
  • 10. Shoehorn into RDBMS Attempt #1 - Adjacency Matrix Doesn’t fit in RDBMS table :( - Why? - Schema / Static Columns - Column growth 10
  • 11. Shoehorn into RDBMS Attempt #2 - Join Table - Many-To-Many 11DEVSPACE 2017 | @tbrantb
  • 12. Who are Bob’s Friends Pretty Simple SELECT p2.id, p2.name FROM person p1 JOIN person_relationship rel ON p1.id = rel.start_person_id AND p1.id = 1 --Bob AND rel.relationship_type=1 --Friend JOIN person p2 ON p2.id = rel.end_person_id 12DEVSPACE 2017 | @tbrantb
  • 13. Who are the Friends of Bob’s Friends? SELECT p3.id, p3.name FROM person p1 JOIN person_relationship rel1 ON p1.id = rel1.start_person_id AND p1.id = 1 --Bob AND rel1.relationship_type = 1 --Friend JOIN person p2 ON p2.id = rel1.end_person_id JOIN person_relationship rel2 ON p2.id = rel2.start_person_id AND rel2.relationship_type = 1 --Friend JOIN person p3 ON p3.id = rel2.end_person_id 13
  • 14. Bob’s Friends’ Friends’ Friends to the N Do I have to? 14DEVSPACE 2017 | @tbrantb
  • 15. What is Bob’s Bacon # ? ? 15DEVSPACE 2017 | @tbrantb
  • 16. Not all problems are well suited for RDBMS 16DEVSPACE 2017 | @tbrantb
  • 17. How do Graph DBs do this better? - RDBMS uses joins & table/index scans - Native vs Non-Native GraphDB - “Index Free Adjacency” 17DEVSPACE 2017 | @tbrantb
  • 18. Popular GraphDB Use Cases - Social Networks - Fraud Detection - Recommendation Engines - IAM - Master Data Management (MDM) - Network Ops - Content/Asset Management - Journalism (see Panama Papers) 18DEVSPACE 2017 | @tbrantb
  • 19. Algorithms/Concepts - Shortest Path - A* - Triadic Closures 19DEVSPACE 2017 | @tbrantb
  • 20. Neo4J - Labeled Property Graph - Native Graph DB w/Index Free Adjacency - JVM Based - Fully ACID - All relationships are directed * - Schema-less, Schema-optional - Embedded, Single Instance Server, HA Cluster 20DEVSPACE 2017 | @tbrantb
  • 21. Neo4J APIs - Embedded Java API - HTTP API - Drivers/Bolt Protocol 21DEVSPACE 2017 | @tbrantb
  • 22. Cypher - Declarative Graph Query Language - Originated at Neo Technology - Standardizing at OpenCypher.org - Ascii Art-ish 22DEVSPACE 2017 | @tbrantb
  • 23. Ascii Art-ish ? - () look like circles or nodes - --, -->, <-- look like relationships or edges 23 MATCH (a)-->(b) WHERE a.name = 'Bob' RETURN b DEVSPACE 2017 | @tbrantb
  • 24. MATCH Clause 24 MATCH (a)-->(b) MATCH (a)-[:FRIEND]->(b) MATCH (a)<-[:FRIEND]-(b) MATCH (a)-[:FRIEND]-(b) DEVSPACE 2017 | @tbrantb
  • 25. MATCH Clause with Labels 25 MATCH (p:Person)-[:DIRECTED]->(m:Movie) MATCH (a:Person)-[:ACTED_IN]->(m:Movie)<-[:DIRECTED]-(d:Person) DEVSPACE 2017 | @tbrantb
  • 26. WHERE 26 MATCH (p:Person)-[:DIRECTED]->(m:Movie) WHERE p.name = 'Ron Howard' AND p.born < 1955 MATCH (p:Person {name: 'Ron Howard'})-[:DIRECTED]->(m:Movie) DEVSPACE 2017 | @tbrantb
  • 27. RETURN, ORDER BY, LIMIT 27 MATCH (p:Person {name: 'Ron Howard'})-[:DIRECTED]->(m:Movie) RETURN m.title, m.released, m.tagline ORDER BY m.title LIMIT 3 DEVSPACE 2017 | @tbrantb
  • 28. Overall Read Query Structure 28 [MATCH WHERE] [OPTIONAL MATCH WHERE] [WITH [ORDER BY] [SKIP] [LIMIT]] RETURN [ORDER BY] [SKIP] [LIMIT] DEVSPACE 2017 | @tbrantb
  • 29. CREATE 29 CREATE (matrix:Movie {title:'The Matrix', released:1999}) CREATE (keanu:Person {name:'Keanu Reeves', born:1964}) CREATE (keanu)-[:ACTED_IN]->(matrix) MATCH (matrix:Movie {title:'The Matrix'}), (keanu:Person {name:'Keanu Reeves'}) CREATE (keanu)-[:ACTED_IN]->(matrix) DEVSPACE 2017 | @tbrantb
  • 30. UPDATE 30 MATCH (matrix:Movie {title:'The Matrix'}), (keanu:Person {name:'Keanu Reeves'}) SET matrix.tagline = 'Follow the White Rabbit', keanu.born = 1900 RETURN matrix, keanu //optional DEVSPACE 2017 | @tbrantb
  • 31. DELETE 31 MATCH (p:Person{name: 'Keanu Reeves'}) DELETE p MATCH (p:Person{name: 'Keanu Reeves'})-[r]-() DELETE r, p MATCH (p:Person{name: 'Keanu Reeves'}) DETACH DELETE n DEVSPACE 2017 | @tbrantb
  • 33. Who are Bob’s Friends SELECT p2.id, p2.name FROM person p1 JOIN person_relationship rel ON p1.id = rel.start_person_id AND p1.id = 1 --Bob AND rel.relationship_type=1 --Friend JOIN person p2 ON p2.id = rel.end_person_id 33 MATCH (b:Person {name:'Bob'})-[:FRIEND]->(f:Person) RETURN f.name DEVSPACE 2017 | @tbrantb
  • 34. Who are the Friends of Bob’s Friends? SELECT p3.id, p3.name FROM person p1 JOIN person_relationship rel1 ON p1.id = rel1.start_person_id AND p1.id = 1 --Bob AND rel1.relationship_type = 1 --Friend JOIN person p2 ON p2.id = rel1.end_person_id JOIN person_relationship rel2 ON p2.id = rel2.start_person_id AND rel2.relationship_type = 1 --Friend JOIN person p3 ON p3.id = rel2.end_person_id 34 MATCH (b:Person {name:'Bob'})-[:FRIEND]->(f:Person)-[:FRIEND]->(fof:Person) RETURN DISTINCT fof.name MATCH (b:Person {name:'Bob'})-[:FRIEND *2]->(fof:Person) RETURN DISTINCT fof.name DEVSPACE 2017 | @tbrantb
  • 35. Bob’s Friends’ Friends’ Friends to the N 35 MATCH (b:Person {name:'Bob'})-[:FRIEND *2]->(fof:Person) RETURN DISTINCT fof.name MATCH (b:Person {name:'Bob'})-[:FRIEND *]->(fof:Person) RETURN DISTINCT fof.name MATCH (b:Person {name:'Bob'})-[:FRIEND *..5]->(fof:Person) RETURN DISTINCT fof.name MATCH (b:Person {name:'Bob'})-[:FRIEND *2..5]->(fof:Person) RETURN DISTINCT fof.name DEVSPACE 2017 | @tbrantb
  • 36. What is Bob’s Bacon # ? 36 MATCH path=shortestPath( (kb:Person {name:'Kevin Bacon'})-[*]-(b:Person {name:'Bob'}) ) RETURN length(path) DEVSPACE 2017 | @tbrantb
  • 37. Resources - Developer Website - https://neo4j.com/developer/ - Neo4J Docs - https://neo4j.com/docs/ - Cypher Refcard - https://neo4j.com/docs/cypher-refcard/ - Neo4J Book (free) - Me @tbrantb on twitter 37