SlideShare a Scribd company logo
DSE Graph Fundamentals
Jon Haddad, DataStax
Technical Evangelist
@rustyrazorblade
© DataStax, All Rights Reserved.
A Quick Look at Relational
2
© DataStax, All Rights Reserved.
Tables
• Very structured data
• Relational calculus
• Various normal forms
3
id name title country
1 jon evangelist usa
2 patrick chief	evangelist usa
3 rebecca jr	evangelist canada
4 billy ceo usa
© DataStax, All Rights Reserved.
Selecting and Filtering
4
id name title country
1 jon evangelist usa
2 patrick chief	evangelist usa
3 rebecca jr	evangelist canada
4 billy ceo usa
© DataStax, All Rights Reserved.
Selecting and Filtering
4
id name title country
1 jon evangelist usa
2 patrick chief	evangelist usa
3 rebecca jr	evangelist canada
4 billy ceo usa
select * from people where country = 'canada';
© DataStax, All Rights Reserved.
Selecting and Filtering
4
id name title country
1 jon evangelist usa
2 patrick chief	evangelist usa
3 rebecca jr	evangelist canada
4 billy ceo usa
select * from people where country = 'canada';
© DataStax, All Rights Reserved.
Foreign Keys
5
id name title country
1 jon evangelist 1
2 patrick chief	evangelist 1
3 rebecca jr	evangelist 2
4 billy ceo 1
id name
1 usa
2 canada
3 germany
4 japan
© DataStax, All Rights Reserved.
Foreign Keys
5
id name title country
1 jon evangelist 1
2 patrick chief	evangelist 1
3 rebecca jr	evangelist 2
4 billy ceo 1
id name
1 usa
2 canada
3 germany
4 japan
© DataStax, All Rights Reserved.
Foreign Keys
5
id name title country
1 jon evangelist 1
2 patrick chief	evangelist 1
3 rebecca jr	evangelist 2
4 billy ceo 1
id name
1 usa
2 canada
3 germany
4 japan
© DataStax, All Rights Reserved.
Joins
6
select * from people join country on
people.country = country.id;
id name title country name
1 jon evangelist 1 usa
2 patrick chief	evangelist 1 usa
3 rebecca jr	evangelist 2 canada
4 billy ceo 1 usa
© DataStax, All Rights Reserved.
Many to Many Relationships
7
id name title
1 jon evangelist
2 patrick chief	evangelist
3 rebecca jr	evangelist
4 billy ceo
id name
1 usa
2 canada
3 germany
4 japan
user country
1 1
1 2
2 1
3 2
4 1
© DataStax, All Rights Reserved.
Many to Many Relationships
7
id name title
1 jon evangelist
2 patrick chief	evangelist
3 rebecca jr	evangelist
4 billy ceo
id name
1 usa
2 canada
3 germany
4 japan
user country
1 1
1 2
2 1
3 2
4 1
© DataStax, All Rights Reserved.
Many to Many Relationships
7
id name title
1 jon evangelist
2 patrick chief	evangelist
3 rebecca jr	evangelist
4 billy ceo
id name
1 usa
2 canada
3 germany
4 japan
user country
1 1
1 2
2 1
3 2
4 1
© DataStax, All Rights Reserved.
Many to Many Relationships
7
id name title
1 jon evangelist
2 patrick chief	evangelist
3 rebecca jr	evangelist
4 billy ceo
id name
1 usa
2 canada
3 germany
4 japan
user country
1 1
1 2
2 1
3 2
4 1
© DataStax, All Rights Reserved.
Many to Many Relationships
7
id name title
1 jon evangelist
2 patrick chief	evangelist
3 rebecca jr	evangelist
4 billy ceo
id name
1 usa
2 canada
3 germany
4 japan
user country
1 1
1 2
2 1
3 2
4 1
© DataStax, All Rights Reserved.
Joining
8
select * from people
join people_country on people.id = people_country.user
join country on people_country.country = country.id;
id name title country name
1 jon evangelist 1 usa
1 jon evangelist 2 canada
2 patrick chief	evangelist 1 usa
3 rebecca jr	evangelist 2 canada
4 billy ceo 1 usa
© DataStax, All Rights Reserved.
Joining
8
select * from people
join people_country on people.id = people_country.user
join country on people_country.country = country.id;
id name title country name
1 jon evangelist 1 usa
1 jon evangelist 2 canada
2 patrick chief	evangelist 1 usa
3 rebecca jr	evangelist 2 canada
4 billy ceo 1 usa
© DataStax, All Rights Reserved.
A Fun, Real World Example
9
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
person
id
name
dob
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
person
id
name
dob
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
actor_movie
person
movie
role
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
actor_movie
person
movie
role
actor_tv
person
tv_show
dob
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
actor_movie
person
movie
role
actor_tv
person
tv_show
dob
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
actor_movie
person
movie
role
actor_tv
person
tv_show
dob
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
actor_movie
person
movie
role
actor_tv
person
tv_show
dob
episode
tv_show
season
episode
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
actor_movie
person
movie
role
actor_tv
person
tv_show
dob
episode
tv_show
season
episode
actor_episode
episode
actor
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
actor_movie
person
movie
role
actor_tv
person
tv_show
dob
episode
tv_show
season
episode
actor_episode
episode
actor
© DataStax, All Rights Reserved.
Media Database
1. People - actors, directories, assistants
2. Movies, TV, episodes
10
movie
id
name
year
tv_show
id
name
person
id
name
dob
actor_movie
person
movie
role
actor_tv
person
tv_show
dob
episode
tv_show
season
episode
actor_episode
episode
actor
© DataStax, All Rights Reserved.
Problems
• Every entity gets a table
• Lots of many to many tables
• Very rigid structure
• Going from one to many
requires a migration and new
data model
11
© DataStax, All Rights Reserved.
Problems
• Every entity gets a table
• Lots of many to many tables
• Very rigid structure
• Going from one to many
requires a migration and new
data model
11
© DataStax, All Rights Reserved.
How is this Solved with a Graph?
12
© DataStax, All Rights Reserved.
Vertex
• A vertex represents a "thing"
• For example: Movie, Person
13
© DataStax, All Rights Reserved.
Vertex
• A vertex represents a "thing"
• For example: Movie, Person
13
JCVD
© DataStax, All Rights Reserved.
Vertex
• A vertex represents a "thing"
• For example: Movie, Person
13
JCVD
Time
cop
© DataStax, All Rights Reserved.
JCVD
Time
cop
Edge
• Edges are labeled relationships
• Edges have direction
14
© DataStax, All Rights Reserved.
JCVD
Time
cop
Edge
• Edges are labeled relationships
• Edges have direction
14
ActedIn
© DataStax, All Rights Reserved.
Edges are always many to many
15
JCVD
Time
cop
Blood
sport
© DataStax, All Rights Reserved.
Edges are always many to many
15
JCVD
Time
copActedIn
Blood
sport
© DataStax, All Rights Reserved.
Edges are always many to many
15
JCVD
Time
copActedIn
Blood
sport
ActedIn
© DataStax, All Rights Reserved.
Edges are always many to many
15
JCVD
Time
copActedIn
Blood
sport
ActedIn
Directed
© DataStax, All Rights Reserved.
The power of graph?
16
© DataStax, All Rights Reserved.
Relationships
17
© DataStax, All Rights Reserved.
Let's build on that
18
© DataStax, All Rights Reserved.
Properties
• Similar to fields in a table
• Much more flexible
• Meta properties - properties on properties
• Can be on an edge or a vertex
• Special property - a label
19
JCVD
status amazing
charm infinite
odds	of	rendezvous 99.6%
enemies	decapitated 108747
© DataStax, All Rights Reserved.
Summary
1. We don't define tables
2. We create vertices to represent real
world objects
3. Relationships don't need to be
explicitly modeled
4. Expressing complex relationships is
not hard
5. Adding new edge types is easy
20
© DataStax, All Rights Reserved.
Tinkerpop 3 & Gremlin
• Cross database graph query framework
• API for graph
21
© DataStax, All Rights Reserved.
TinkerPop Basics
22
© DataStax, All Rights Reserved.
Create a Vertex
23
graph.createVertex(label,	"person",		
								"name",	"Jean	Claude	Van	Damme")
graph.createVertex(label,	"movie",		
								"name",	"Bloodsport")
© DataStax, All Rights Reserved.
Traversals
24
g	=	graph.traversal()
g.V()
© DataStax, All Rights Reserved.
Finding Vertices
25
g.V().has("person",	"name",	"Jean	Claude	van	Damme")
© DataStax, All Rights Reserved.
Finding Vertices
25
g.V().has("person",	"name",	"Jean	Claude	van	Damme")
© DataStax, All Rights Reserved.
Finding Vertices
25
g.V().has("person",	"name",	"Jean	Claude	van	Damme")
© DataStax, All Rights Reserved.
Finding Vertices
25
g.V().has("person",	"name",	"Jean	Claude	van	Damme")
© DataStax, All Rights Reserved.
Finding Vertices
25
g.V().has("person",	"name",	"Jean	Claude	van	Damme")
© DataStax, All Rights Reserved.
Create an Edge
26
g.V().has('person',	'name',	"Jean	Claude	Van	Damme").as('a').	
		V().has('movie',	'name',	"Bloodsport").as('b').	
						addE("acted_in").property('year',	1988).	
							from("a").to("b")
© DataStax, All Rights Reserved.
Traversals
27
jcvd	=	g.V().has('person','name',	"Jean	Claude	Van	Damme")
© DataStax, All Rights Reserved.
Traversals to neighbors
28
Bloodsport (1988)
Kickboxer (1989)
Timecop (1994)jcvd.out()
© DataStax, All Rights Reserved.
Traversals to neighbors
28
Bloodsport (1988)
Kickboxer (1989)
Timecop (1994)jcvd.out()
© DataStax, All Rights Reserved.
Traversals to neighbors
28
Bloodsport (1988)
Kickboxer (1989)
Timecop (1994)jcvd.out()
© DataStax, All Rights Reserved.
Traversals to edges
29
Bloodsport (1988)
Kickboxer (1989)
Timecop (1994)
jcvd.outE()
© DataStax, All Rights Reserved.
Traversals to edges
29
Bloodsport (1988)
Kickboxer (1989)
Timecop (1994)
jcvd.outE()
© DataStax, All Rights Reserved.
Who cares?
30
© DataStax, All Rights Reserved. 31
SELECT	movie.name	from	person		
		JOIN	roles	on	person.id	=	roles.person	
		JOIN	movies	on	roles.movie	=	movies.id	
WHERE	person.name	=	"Jean	Claude	Van	Damme"
© DataStax, All Rights Reserved. 32
jcvd.out("acted_in")
© DataStax, All Rights Reserved. 33
© DataStax, All Rights Reserved. 33
NO
© DataStax, All Rights Reserved. 34
jcvd.out("acted_in",	"directed")
© DataStax, All Rights Reserved. 35
SELECT	movies.name	from	person		
		JOIN	roles	on	person.id	=	roles.person	
		JOIN	movies	on	roles.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	directed	on	person.id	=	directed.person	
		JOIN	movies	on	directed.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"
© DataStax, All Rights Reserved.
SELECT	movies.name	from	person		
		JOIN	roles	on	person.id	=	roles.person	
		JOIN	movies	on	roles.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	directed	on	person.id	=	directed.person	
		JOIN	movies	on	directed.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	produced	on	person.id	=	produced.person	
		JOIN	movies	on	produced.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	stage_hand	on	person.id	=	stage_hand.person	
		JOIN	movies	on	stage_hand.movie	=	movies.id
© DataStax, All Rights Reserved.
SELECT	movies.name	from	person		
		JOIN	directed	on	person.id	=	directed.person	
		JOIN	movies	on	directed.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	produced	on	person.id	=	produced.person	
		JOIN	movies	on	produced.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	stage_hand	on	person.id	=	stage_hand.person	
		JOIN	movies	on	stage_hand.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	stage_hand	on	person.id	=	stage_hand.person	
		JOIN	movies	on	stage_hand.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"
© DataStax, All Rights Reserved.
SELECT	movies.name	from	person		
		JOIN	directed	on	person.id	=	directed.person	
		JOIN	movies	on	directed.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	produced	on	person.id	=	produced.person	
		JOIN	movies	on	produced.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	stage_hand	on	person.id	=	stage_hand.person	
		JOIN	movies	on	stage_hand.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"	
UNION	
SELECT	movies.name	from	person		
		JOIN	stage_hand	on	person.id	=	stage_hand.person	
		JOIN	movies	on	stage_hand.movie	=	movies.id	
WHERE	actor.name	=	"Jean	Claude	Van	Damme"
© DataStax, All Rights Reserved. 37
jcvd.out()
© DataStax, All Rights Reserved.
Who acted with Van Damme
and was born in Texas?
38
© DataStax, All Rights Reserved.
Traversals
39
jcvd.out("acted_in")
jcvd
acted_in
acted_in
time	cop
bloodsport
© DataStax, All Rights Reserved.
Traversals
39
jcvd.out("acted_in")
jcvd
acted_in
acted_in
.in("acted_in")
time	cop
bloodsport
© DataStax, All Rights Reserved.
Traversals
39
jcvd.out("acted_in")
jcvd
acted_in
acted_in
.in("acted_in")
ron	
silveracted_in
Bruce	
McGill
acted_in
Forest	
Whitaker
acted_in
New	York
Texas
Texas
time	cop
bloodsport
© DataStax, All Rights Reserved.
Traversals
39
jcvd.out("acted_in")
jcvd
acted_in
acted_in
.in("acted_in")
ron	
silveracted_in
Bruce	
McGill
acted_in
Forest	
Whitaker
acted_in
New	York
Texas
Texas
time	cop
bloodsport
© DataStax, All Rights Reserved.
Traversals
39
jcvd.out("acted_in")
jcvd
acted_in
acted_in
	.has("state_of_birth",	"Texas")
.in("acted_in")
ron	
silveracted_in
Bruce	
McGill
acted_in
Forest	
Whitaker
acted_in
New	York
Texas
Texas
time	cop
bloodsport
© DataStax, All Rights Reserved.
Traversals
39
jcvd.out("acted_in")
jcvd
acted_in
acted_in
	.has("state_of_birth",	"Texas")
.in("acted_in")
ron	
silveracted_in
Bruce	
McGill
acted_in
Forest	
Whitaker
acted_in
New	York
Texas
Texas
time	cop
bloodsport
© DataStax, All Rights Reserved.
Creating Indexes
40
schema.vertexLabel('person').index('by_year_born')	
	.materialized().by('year_born').add()	
schema.vertexLabel('person').index('search').	
	search().by('biography').asText().add()
© DataStax, All Rights Reserved.
Creating Indexes
40
schema.vertexLabel('person').index('by_year_born')	
	.materialized().by('year_born').add()	
schema.vertexLabel('person').index('search').	
	search().by('biography').asText().add()
© DataStax, All Rights Reserved.
Creating Indexes
40
schema.vertexLabel('person').index('by_year_born')	
	.materialized().by('year_born').add()	
schema.vertexLabel('person').index('search').	
	search().by('biography').asText().add()
© DataStax, All Rights Reserved.
Who has ANY relationship with any actor born in 1920?
41
g.V().has("person",	"year_born",	1920).	
						out("acted_in").in().hasLabel("person")
© DataStax, All Rights Reserved.
index	selection
Who has ANY relationship with any actor born in 1920?
41
g.V().has("person",	"year_born",	1920).	
						out("acted_in").in().hasLabel("person")
© DataStax, All Rights Reserved.
Who has ANY relationship with any actor born in 1920?
41
g.V().has("person",	"year_born",	1920).	
						out("acted_in").in().hasLabel("person")
unconstrained	relationships	with	no	foreign	keys
© DataStax, All Rights Reserved.
How do I know Patrick?
42
© DataStax, All Rights Reserved.
Path Finding!
43
© DataStax, All Rights Reserved. 44
jon	=	g.V().has("nickname",	"rustyrazorblade")	
jon.until(has("nickname",	"PatrickMcFadin")).	
				repeat(both())
© DataStax, All Rights Reserved.
My cousin went to school with Patrick
45
@rustyrazorblade
@PatrickMcfadin
© DataStax, All Rights Reserved.
My cousin went to school with Patrick
45
@rustyrazorblade
@PatrickMcfadin
cousin
@oscar_the_grouch
© DataStax, All Rights Reserved.
My cousin went to school with Patrick
45
@rustyrazorblade
@PatrickMcfadin
cousin
@oscar_the_grouch
attended
Cal	Poly
© DataStax, All Rights Reserved.
My cousin went to school with Patrick
45
attended
@rustyrazorblade
@PatrickMcfadin
cousin
@oscar_the_grouch
attended
Cal	Poly
© DataStax, All Rights Reserved.
We have a mutual friend in the same company
46
© DataStax, All Rights Reserved.
We have a mutual friend in the same company
46
friend
© DataStax, All Rights Reserved.
We have a mutual friend in the same company
46
friend
works_at
© DataStax, All Rights Reserved.
We have a mutual friend in the same company
46
friend
works_at
works_at
© DataStax, All Rights Reserved.
When do you use graph?
47
© DataStax, All Rights Reserved.
Graph helps solves complex
problems by utilizing of the
power of relationships
48
© DataStax, All Rights Reserved. 49
© DataStax, All Rights Reserved. 49
Thank	you!!!

More Related Content

PDF
II-SDV 2014 Product Presentations GenomeQuest
PPTX
Probation and parole unit 3
PPTX
Introduction to Graph Databases
PDF
Getting Started with Graph Databases
PDF
Tapping the Data Deluge with R
PDF
Data Natives Amsterdam v 9.0 | "Point in Time Labeling at Scale" - Timothy Th...
PDF
Intro to Neo4j 2.0
PDF
DataStax | Graph Data Modeling in DataStax Enterprise (Artem Chebotko) | Cass...
II-SDV 2014 Product Presentations GenomeQuest
Probation and parole unit 3
Introduction to Graph Databases
Getting Started with Graph Databases
Tapping the Data Deluge with R
Data Natives Amsterdam v 9.0 | "Point in Time Labeling at Scale" - Timothy Th...
Intro to Neo4j 2.0
DataStax | Graph Data Modeling in DataStax Enterprise (Artem Chebotko) | Cass...

More from DataStax (20)

PPTX
Is Your Enterprise Ready to Shine This Holiday Season?
PPTX
Designing Fault-Tolerant Applications with DataStax Enterprise and Apache Cas...
PPTX
Running DataStax Enterprise in VMware Cloud and Hybrid Environments
PPTX
Best Practices for Getting to Production with DataStax Enterprise Graph
PPTX
Webinar | Data Management for Hybrid and Multi-Cloud: A Four-Step Journey
PPTX
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
PDF
Webinar | Better Together: Apache Cassandra and Apache Kafka
PDF
Top 10 Best Practices for Apache Cassandra and DataStax Enterprise
PDF
Introduction to Apache Cassandra™ + What’s New in 4.0
PPTX
Webinar: How Active Everywhere Database Architecture Accelerates Hybrid Cloud...
PPTX
Webinar | Aligning GDPR Requirements with Today's Hybrid Cloud Realities
PDF
Designing a Distributed Cloud Database for Dummies
PDF
How to Power Innovation with Geo-Distributed Data Management in Hybrid Cloud
PDF
How to Evaluate Cloud Databases for eCommerce
PPTX
Webinar: DataStax Enterprise 6: 10 Ways to Multiply the Power of Apache Cassa...
PPTX
Webinar: DataStax and Microsoft Azure: Empowering the Right-Now Enterprise wi...
PPTX
Webinar - Real-Time Customer Experience for the Right-Now Enterprise featurin...
PPTX
Datastax - The Architect's guide to customer experience (CX)
PPTX
An Operational Data Layer is Critical for Transformative Banking Applications
PPTX
Becoming a Customer-Centric Enterprise Via Real-Time Data and Design Thinking
Is Your Enterprise Ready to Shine This Holiday Season?
Designing Fault-Tolerant Applications with DataStax Enterprise and Apache Cas...
Running DataStax Enterprise in VMware Cloud and Hybrid Environments
Best Practices for Getting to Production with DataStax Enterprise Graph
Webinar | Data Management for Hybrid and Multi-Cloud: A Four-Step Journey
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
Webinar | Better Together: Apache Cassandra and Apache Kafka
Top 10 Best Practices for Apache Cassandra and DataStax Enterprise
Introduction to Apache Cassandra™ + What’s New in 4.0
Webinar: How Active Everywhere Database Architecture Accelerates Hybrid Cloud...
Webinar | Aligning GDPR Requirements with Today's Hybrid Cloud Realities
Designing a Distributed Cloud Database for Dummies
How to Power Innovation with Geo-Distributed Data Management in Hybrid Cloud
How to Evaluate Cloud Databases for eCommerce
Webinar: DataStax Enterprise 6: 10 Ways to Multiply the Power of Apache Cassa...
Webinar: DataStax and Microsoft Azure: Empowering the Right-Now Enterprise wi...
Webinar - Real-Time Customer Experience for the Right-Now Enterprise featurin...
Datastax - The Architect's guide to customer experience (CX)
An Operational Data Layer is Critical for Transformative Banking Applications
Becoming a Customer-Centric Enterprise Via Real-Time Data and Design Thinking
Ad

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
cuic standard and advanced reporting.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Big Data Technologies - Introduction.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
Teaching material agriculture food technology
PPTX
Spectroscopy.pptx food analysis technology
20250228 LYD VKU AI Blended-Learning.pptx
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
cuic standard and advanced reporting.pdf
The AUB Centre for AI in Media Proposal.docx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Per capita expenditure prediction using model stacking based on satellite ima...
madgavkar20181017ppt McKinsey Presentation.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Big Data Technologies - Introduction.pptx
Modernizing your data center with Dell and AMD
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
Spectroscopy.pptx food analysis technology
Ad

DataStax Enterprise Graph Fundamentals with Real World Example

  • 1. DSE Graph Fundamentals Jon Haddad, DataStax Technical Evangelist @rustyrazorblade
  • 2. © DataStax, All Rights Reserved. A Quick Look at Relational 2
  • 3. © DataStax, All Rights Reserved. Tables • Very structured data • Relational calculus • Various normal forms 3 id name title country 1 jon evangelist usa 2 patrick chief evangelist usa 3 rebecca jr evangelist canada 4 billy ceo usa
  • 4. © DataStax, All Rights Reserved. Selecting and Filtering 4 id name title country 1 jon evangelist usa 2 patrick chief evangelist usa 3 rebecca jr evangelist canada 4 billy ceo usa
  • 5. © DataStax, All Rights Reserved. Selecting and Filtering 4 id name title country 1 jon evangelist usa 2 patrick chief evangelist usa 3 rebecca jr evangelist canada 4 billy ceo usa select * from people where country = 'canada';
  • 6. © DataStax, All Rights Reserved. Selecting and Filtering 4 id name title country 1 jon evangelist usa 2 patrick chief evangelist usa 3 rebecca jr evangelist canada 4 billy ceo usa select * from people where country = 'canada';
  • 7. © DataStax, All Rights Reserved. Foreign Keys 5 id name title country 1 jon evangelist 1 2 patrick chief evangelist 1 3 rebecca jr evangelist 2 4 billy ceo 1 id name 1 usa 2 canada 3 germany 4 japan
  • 8. © DataStax, All Rights Reserved. Foreign Keys 5 id name title country 1 jon evangelist 1 2 patrick chief evangelist 1 3 rebecca jr evangelist 2 4 billy ceo 1 id name 1 usa 2 canada 3 germany 4 japan
  • 9. © DataStax, All Rights Reserved. Foreign Keys 5 id name title country 1 jon evangelist 1 2 patrick chief evangelist 1 3 rebecca jr evangelist 2 4 billy ceo 1 id name 1 usa 2 canada 3 germany 4 japan
  • 10. © DataStax, All Rights Reserved. Joins 6 select * from people join country on people.country = country.id; id name title country name 1 jon evangelist 1 usa 2 patrick chief evangelist 1 usa 3 rebecca jr evangelist 2 canada 4 billy ceo 1 usa
  • 11. © DataStax, All Rights Reserved. Many to Many Relationships 7 id name title 1 jon evangelist 2 patrick chief evangelist 3 rebecca jr evangelist 4 billy ceo id name 1 usa 2 canada 3 germany 4 japan user country 1 1 1 2 2 1 3 2 4 1
  • 12. © DataStax, All Rights Reserved. Many to Many Relationships 7 id name title 1 jon evangelist 2 patrick chief evangelist 3 rebecca jr evangelist 4 billy ceo id name 1 usa 2 canada 3 germany 4 japan user country 1 1 1 2 2 1 3 2 4 1
  • 13. © DataStax, All Rights Reserved. Many to Many Relationships 7 id name title 1 jon evangelist 2 patrick chief evangelist 3 rebecca jr evangelist 4 billy ceo id name 1 usa 2 canada 3 germany 4 japan user country 1 1 1 2 2 1 3 2 4 1
  • 14. © DataStax, All Rights Reserved. Many to Many Relationships 7 id name title 1 jon evangelist 2 patrick chief evangelist 3 rebecca jr evangelist 4 billy ceo id name 1 usa 2 canada 3 germany 4 japan user country 1 1 1 2 2 1 3 2 4 1
  • 15. © DataStax, All Rights Reserved. Many to Many Relationships 7 id name title 1 jon evangelist 2 patrick chief evangelist 3 rebecca jr evangelist 4 billy ceo id name 1 usa 2 canada 3 germany 4 japan user country 1 1 1 2 2 1 3 2 4 1
  • 16. © DataStax, All Rights Reserved. Joining 8 select * from people join people_country on people.id = people_country.user join country on people_country.country = country.id; id name title country name 1 jon evangelist 1 usa 1 jon evangelist 2 canada 2 patrick chief evangelist 1 usa 3 rebecca jr evangelist 2 canada 4 billy ceo 1 usa
  • 17. © DataStax, All Rights Reserved. Joining 8 select * from people join people_country on people.id = people_country.user join country on people_country.country = country.id; id name title country name 1 jon evangelist 1 usa 1 jon evangelist 2 canada 2 patrick chief evangelist 1 usa 3 rebecca jr evangelist 2 canada 4 billy ceo 1 usa
  • 18. © DataStax, All Rights Reserved. A Fun, Real World Example 9
  • 19. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10
  • 20. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 person id name dob
  • 21. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year person id name dob
  • 22. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob
  • 23. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob actor_movie person movie role
  • 24. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob actor_movie person movie role actor_tv person tv_show dob
  • 25. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob actor_movie person movie role actor_tv person tv_show dob
  • 26. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob actor_movie person movie role actor_tv person tv_show dob
  • 27. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob actor_movie person movie role actor_tv person tv_show dob episode tv_show season episode
  • 28. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob actor_movie person movie role actor_tv person tv_show dob episode tv_show season episode actor_episode episode actor
  • 29. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob actor_movie person movie role actor_tv person tv_show dob episode tv_show season episode actor_episode episode actor
  • 30. © DataStax, All Rights Reserved. Media Database 1. People - actors, directories, assistants 2. Movies, TV, episodes 10 movie id name year tv_show id name person id name dob actor_movie person movie role actor_tv person tv_show dob episode tv_show season episode actor_episode episode actor
  • 31. © DataStax, All Rights Reserved. Problems • Every entity gets a table • Lots of many to many tables • Very rigid structure • Going from one to many requires a migration and new data model 11
  • 32. © DataStax, All Rights Reserved. Problems • Every entity gets a table • Lots of many to many tables • Very rigid structure • Going from one to many requires a migration and new data model 11
  • 33. © DataStax, All Rights Reserved. How is this Solved with a Graph? 12
  • 34. © DataStax, All Rights Reserved. Vertex • A vertex represents a "thing" • For example: Movie, Person 13
  • 35. © DataStax, All Rights Reserved. Vertex • A vertex represents a "thing" • For example: Movie, Person 13 JCVD
  • 36. © DataStax, All Rights Reserved. Vertex • A vertex represents a "thing" • For example: Movie, Person 13 JCVD Time cop
  • 37. © DataStax, All Rights Reserved. JCVD Time cop Edge • Edges are labeled relationships • Edges have direction 14
  • 38. © DataStax, All Rights Reserved. JCVD Time cop Edge • Edges are labeled relationships • Edges have direction 14 ActedIn
  • 39. © DataStax, All Rights Reserved. Edges are always many to many 15 JCVD Time cop Blood sport
  • 40. © DataStax, All Rights Reserved. Edges are always many to many 15 JCVD Time copActedIn Blood sport
  • 41. © DataStax, All Rights Reserved. Edges are always many to many 15 JCVD Time copActedIn Blood sport ActedIn
  • 42. © DataStax, All Rights Reserved. Edges are always many to many 15 JCVD Time copActedIn Blood sport ActedIn Directed
  • 43. © DataStax, All Rights Reserved. The power of graph? 16
  • 44. © DataStax, All Rights Reserved. Relationships 17
  • 45. © DataStax, All Rights Reserved. Let's build on that 18
  • 46. © DataStax, All Rights Reserved. Properties • Similar to fields in a table • Much more flexible • Meta properties - properties on properties • Can be on an edge or a vertex • Special property - a label 19 JCVD status amazing charm infinite odds of rendezvous 99.6% enemies decapitated 108747
  • 47. © DataStax, All Rights Reserved. Summary 1. We don't define tables 2. We create vertices to represent real world objects 3. Relationships don't need to be explicitly modeled 4. Expressing complex relationships is not hard 5. Adding new edge types is easy 20
  • 48. © DataStax, All Rights Reserved. Tinkerpop 3 & Gremlin • Cross database graph query framework • API for graph 21
  • 49. © DataStax, All Rights Reserved. TinkerPop Basics 22
  • 50. © DataStax, All Rights Reserved. Create a Vertex 23 graph.createVertex(label, "person", "name", "Jean Claude Van Damme") graph.createVertex(label, "movie", "name", "Bloodsport")
  • 51. © DataStax, All Rights Reserved. Traversals 24 g = graph.traversal() g.V()
  • 52. © DataStax, All Rights Reserved. Finding Vertices 25 g.V().has("person", "name", "Jean Claude van Damme")
  • 53. © DataStax, All Rights Reserved. Finding Vertices 25 g.V().has("person", "name", "Jean Claude van Damme")
  • 54. © DataStax, All Rights Reserved. Finding Vertices 25 g.V().has("person", "name", "Jean Claude van Damme")
  • 55. © DataStax, All Rights Reserved. Finding Vertices 25 g.V().has("person", "name", "Jean Claude van Damme")
  • 56. © DataStax, All Rights Reserved. Finding Vertices 25 g.V().has("person", "name", "Jean Claude van Damme")
  • 57. © DataStax, All Rights Reserved. Create an Edge 26 g.V().has('person', 'name', "Jean Claude Van Damme").as('a'). V().has('movie', 'name', "Bloodsport").as('b'). addE("acted_in").property('year', 1988). from("a").to("b")
  • 58. © DataStax, All Rights Reserved. Traversals 27 jcvd = g.V().has('person','name', "Jean Claude Van Damme")
  • 59. © DataStax, All Rights Reserved. Traversals to neighbors 28 Bloodsport (1988) Kickboxer (1989) Timecop (1994)jcvd.out()
  • 60. © DataStax, All Rights Reserved. Traversals to neighbors 28 Bloodsport (1988) Kickboxer (1989) Timecop (1994)jcvd.out()
  • 61. © DataStax, All Rights Reserved. Traversals to neighbors 28 Bloodsport (1988) Kickboxer (1989) Timecop (1994)jcvd.out()
  • 62. © DataStax, All Rights Reserved. Traversals to edges 29 Bloodsport (1988) Kickboxer (1989) Timecop (1994) jcvd.outE()
  • 63. © DataStax, All Rights Reserved. Traversals to edges 29 Bloodsport (1988) Kickboxer (1989) Timecop (1994) jcvd.outE()
  • 64. © DataStax, All Rights Reserved. Who cares? 30
  • 65. © DataStax, All Rights Reserved. 31 SELECT movie.name from person JOIN roles on person.id = roles.person JOIN movies on roles.movie = movies.id WHERE person.name = "Jean Claude Van Damme"
  • 66. © DataStax, All Rights Reserved. 32 jcvd.out("acted_in")
  • 67. © DataStax, All Rights Reserved. 33
  • 68. © DataStax, All Rights Reserved. 33 NO
  • 69. © DataStax, All Rights Reserved. 34 jcvd.out("acted_in", "directed")
  • 70. © DataStax, All Rights Reserved. 35 SELECT movies.name from person JOIN roles on person.id = roles.person JOIN movies on roles.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN directed on person.id = directed.person JOIN movies on directed.movie = movies.id WHERE actor.name = "Jean Claude Van Damme"
  • 71. © DataStax, All Rights Reserved. SELECT movies.name from person JOIN roles on person.id = roles.person JOIN movies on roles.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN directed on person.id = directed.person JOIN movies on directed.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN produced on person.id = produced.person JOIN movies on produced.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN stage_hand on person.id = stage_hand.person JOIN movies on stage_hand.movie = movies.id
  • 72. © DataStax, All Rights Reserved. SELECT movies.name from person JOIN directed on person.id = directed.person JOIN movies on directed.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN produced on person.id = produced.person JOIN movies on produced.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN stage_hand on person.id = stage_hand.person JOIN movies on stage_hand.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN stage_hand on person.id = stage_hand.person JOIN movies on stage_hand.movie = movies.id WHERE actor.name = "Jean Claude Van Damme"
  • 73. © DataStax, All Rights Reserved. SELECT movies.name from person JOIN directed on person.id = directed.person JOIN movies on directed.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN produced on person.id = produced.person JOIN movies on produced.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN stage_hand on person.id = stage_hand.person JOIN movies on stage_hand.movie = movies.id WHERE actor.name = "Jean Claude Van Damme" UNION SELECT movies.name from person JOIN stage_hand on person.id = stage_hand.person JOIN movies on stage_hand.movie = movies.id WHERE actor.name = "Jean Claude Van Damme"
  • 74. © DataStax, All Rights Reserved. 37 jcvd.out()
  • 75. © DataStax, All Rights Reserved. Who acted with Van Damme and was born in Texas? 38
  • 76. © DataStax, All Rights Reserved. Traversals 39 jcvd.out("acted_in") jcvd acted_in acted_in time cop bloodsport
  • 77. © DataStax, All Rights Reserved. Traversals 39 jcvd.out("acted_in") jcvd acted_in acted_in .in("acted_in") time cop bloodsport
  • 78. © DataStax, All Rights Reserved. Traversals 39 jcvd.out("acted_in") jcvd acted_in acted_in .in("acted_in") ron silveracted_in Bruce McGill acted_in Forest Whitaker acted_in New York Texas Texas time cop bloodsport
  • 79. © DataStax, All Rights Reserved. Traversals 39 jcvd.out("acted_in") jcvd acted_in acted_in .in("acted_in") ron silveracted_in Bruce McGill acted_in Forest Whitaker acted_in New York Texas Texas time cop bloodsport
  • 80. © DataStax, All Rights Reserved. Traversals 39 jcvd.out("acted_in") jcvd acted_in acted_in .has("state_of_birth", "Texas") .in("acted_in") ron silveracted_in Bruce McGill acted_in Forest Whitaker acted_in New York Texas Texas time cop bloodsport
  • 81. © DataStax, All Rights Reserved. Traversals 39 jcvd.out("acted_in") jcvd acted_in acted_in .has("state_of_birth", "Texas") .in("acted_in") ron silveracted_in Bruce McGill acted_in Forest Whitaker acted_in New York Texas Texas time cop bloodsport
  • 82. © DataStax, All Rights Reserved. Creating Indexes 40 schema.vertexLabel('person').index('by_year_born') .materialized().by('year_born').add() schema.vertexLabel('person').index('search'). search().by('biography').asText().add()
  • 83. © DataStax, All Rights Reserved. Creating Indexes 40 schema.vertexLabel('person').index('by_year_born') .materialized().by('year_born').add() schema.vertexLabel('person').index('search'). search().by('biography').asText().add()
  • 84. © DataStax, All Rights Reserved. Creating Indexes 40 schema.vertexLabel('person').index('by_year_born') .materialized().by('year_born').add() schema.vertexLabel('person').index('search'). search().by('biography').asText().add()
  • 85. © DataStax, All Rights Reserved. Who has ANY relationship with any actor born in 1920? 41 g.V().has("person", "year_born", 1920). out("acted_in").in().hasLabel("person")
  • 86. © DataStax, All Rights Reserved. index selection Who has ANY relationship with any actor born in 1920? 41 g.V().has("person", "year_born", 1920). out("acted_in").in().hasLabel("person")
  • 87. © DataStax, All Rights Reserved. Who has ANY relationship with any actor born in 1920? 41 g.V().has("person", "year_born", 1920). out("acted_in").in().hasLabel("person") unconstrained relationships with no foreign keys
  • 88. © DataStax, All Rights Reserved. How do I know Patrick? 42
  • 89. © DataStax, All Rights Reserved. Path Finding! 43
  • 90. © DataStax, All Rights Reserved. 44 jon = g.V().has("nickname", "rustyrazorblade") jon.until(has("nickname", "PatrickMcFadin")). repeat(both())
  • 91. © DataStax, All Rights Reserved. My cousin went to school with Patrick 45 @rustyrazorblade @PatrickMcfadin
  • 92. © DataStax, All Rights Reserved. My cousin went to school with Patrick 45 @rustyrazorblade @PatrickMcfadin cousin @oscar_the_grouch
  • 93. © DataStax, All Rights Reserved. My cousin went to school with Patrick 45 @rustyrazorblade @PatrickMcfadin cousin @oscar_the_grouch attended Cal Poly
  • 94. © DataStax, All Rights Reserved. My cousin went to school with Patrick 45 attended @rustyrazorblade @PatrickMcfadin cousin @oscar_the_grouch attended Cal Poly
  • 95. © DataStax, All Rights Reserved. We have a mutual friend in the same company 46
  • 96. © DataStax, All Rights Reserved. We have a mutual friend in the same company 46 friend
  • 97. © DataStax, All Rights Reserved. We have a mutual friend in the same company 46 friend works_at
  • 98. © DataStax, All Rights Reserved. We have a mutual friend in the same company 46 friend works_at works_at
  • 99. © DataStax, All Rights Reserved. When do you use graph? 47
  • 100. © DataStax, All Rights Reserved. Graph helps solves complex problems by utilizing of the power of relationships 48
  • 101. © DataStax, All Rights Reserved. 49
  • 102. © DataStax, All Rights Reserved. 49 Thank you!!!