getting started with graph databases

32
©2013 DataStax Confidential. Do not distribute without consent. @rustyrazorblade Jon Haddad Technical Evangelist, DataStax Graph Databases 101 1

Upload: datastax-academy

Post on 11-Apr-2017

498 views

Category:

Technology


2 download

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

A Fun, Real World Example

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

How is this Solved with a Graph?

Vertex• A vertex represents a "thing" • For example: Movie, Person

JCVD Time cop

JCVD Time cop

Edge• Edges are labeled relationships • Edges have direction

ActedIn

Edges are always many to many

JCVD Time copActedIn

Bloodsport

ActedIn

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

Tinkerpop 3• Cross database graph query

language • API for graph • Gremlin Server

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

Traversing the Graph

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

Bloodsport (1988)

Kickboxer (1989)

Timecop (1994)jcvd.out()

Traversals

Bloodsport (1988)

Kickboxer (1989)

Timecop (1994)jcvd.outE()

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))

Use Cases

Recommendation Engines•What do I like? •What do others like? •What do others listen to? • Find correlations

Fraud & Anomaly Detection

Complex Taxonomies

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

©2013 DataStax Confidential. Do not distribute without consent. 32