getting started with graph databases
TRANSCRIPT
©2013 DataStax Confidential. Do not distribute without consent.
@rustyrazorblade
Jon HaddadTechnical Evangelist, DataStax
Graph Databases 101
1
A Quick Look at Relational
View the Full-Length Video & Tutorialhttps://academy.datastax.com/demos/getting-started-graph-databases
Tables• Very structured data • Relational calculus • Various normal forms
id name title country
1 jon evangelist usa
2 patrick chief evangelist usa
3 rebecca jr evangelist canada
4 billy ceo usa
Selecting and Filtering
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';
Foreign Keys
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
Joins
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
Many to Many Relationships
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
Joiningselect * 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
movieidnameyear
tv_show
id
name
personidnamedob
actor_moviepersonmovierole
actor_tvpersontv_showdob
episodetv_showseasonepisode
actor_episodeepisodeactor
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
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
JCVD
status amazing
charm infinite
odds of rendezvous 99.6%
enemies decapitated 108747
Adding Data
1 graph = TitanFactory.build().set('storage.backend', 'inmemory').open() 2 3 g = graph.traversal() 4 5 jcvd = graph.addVertex(label, "actor", 6 "name", "jean claude van damme") 7 8 kick = graph.addVertex(label, "movie", "name", "Kickboxer", 9 "year", 1989) 10 11 blood = graph.addVertex(label, "movie", "name", "Bloodsport", 12 "year", 1988) 13 14 jcvd.addEdge("acted_in", kick) 15 jcvd.addEdge("acted_in", blood)
Summary
•We don't define tables •We create vertices to represent real
world objects • Relationships don't need to be
explicitly modeled • Expressing complex relationships is
not hard • Adding new edge types is easy
Finding Vertices
single vertex g.V(4160)
matching a property
g.V().has("name", "jean claude van damme")
range filteringg.V().has("year", between(1980, 1990))
Traversals
to other vertices g.V().has("name", "jean claude van damme").out()
to edges g.V().has("name", "jean claude van damme").outE()
filtering with traversals
g.V().has("name", "jean claude van damme").out(). has("year", between(1980, 1990))
Recommendation Engines•What do I like? •What do others like? •What do others listen to? • Find correlations
Titan• Open Source •Works with Cassandra to scale to massive
graphs • OLTP + OLAP graph queries via Spark •Works with Solr or Elastic Search
DSE Graph•Next generation of distributed graph • Benefits of Titan but tightly integrated
with DataStax Enterprise