surrounded by graphs

48
Surrounded by Graphs a short introduction to Graph Databases and Neo4j Created by , Neo Technology Inc. Julian Simpson @builddoctor

Upload: julian-simpson

Post on 21-Jan-2018

270 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Surrounded by Graphs

Surrounded by Graphsa short introduction to Graph Databases

and Neo4jCreated by , Neo Technology Inc. Julian Simpson

@builddoctor

Page 2: Surrounded by Graphs

Introduction: Who are you?Yes, you!

0

Page 3: Surrounded by Graphs

Introduction: Who am I?Dotbomb Solaris AdminReformed Java Build MonkeyDevOps Agitator(Who actually went to DevOpsDays conference #1)Full Stack Developer at Neo Technology, makers of Neo4j

Page 4: Surrounded by Graphs

Agenda: What's this talk about?NoSQL LandscapeWhat are graphs, really?Graph ExamplesNeo4j Example and Story TimeQuestions

Page 5: Surrounded by Graphs

Define NoSQL(from: http://martinfowler.com/bliki/NosqlDe�nition.html)

Not using the relational model (nor the SQL language)Open source (mostly)Designed to run on large clusters (mostly)Based on the needs of 21st century web propertiesNo schema, allowing �elds to be added to any record without controls

Page 6: Surrounded by Graphs

4 kinds of NoSQL DatabaseKey-Value (e.g. BerkeleyDB, Redis)Document (e.g. MongoDB, CouchDB)Column Family (e.g. Cassandra, Vertica)Graph (e.g. Neo4j, In�niteGraph)

Most content stolen from NoSQL Distilled: A Brief Guide to the EmergingWorld of Polyglot Persistence Paperback – August 18, 2012 by Pramod J.

Sadalage, Martin Fowler

Page 7: Surrounded by Graphs

Key ValueDefinition

A simple hash table, primarily used when all access tothe database is via primary key

Example < SET hobbit:name "baggins" OK < GET hobbit:name "baggins"

Page 8: Surrounded by Graphs

Key ValueUse Cases

Recommended Not RecommendedStoring Session Information Relationships among dataUser Pro�les and Preferences Multi Operation TransactionsShopping Cart Data Operations by Sets

Query by Data

Page 9: Surrounded by Graphs

Document DatabaseDefinition

... documents are self-describing, hierarchical treedata structures which can consist of maps, collections

and scalar values.

Example { "_id" : 1, "name" : { "first" : "Bilbo", "last" : "Baggins" }, "comment": "Aren't we glad this hobbit business is done?" }

Page 10: Surrounded by Graphs

Document DatabaseUse Cases

Recommended Not RecommendedEvent Logging Complex Transactions spanning di�erent

documentsBlogging, CMSplatforms

Queries against varying aggregate structure

Web or Real-TimeanalyticsE-Commerce

Page 11: Surrounded by Graphs

Column Family StoresDefinition

... store data with keys mapped to values and thevalues grouped into multiple column families, each

column family being a map of data

Example { "bilbo": { age: "eleventy six", surname: "baggins" }, "frodo": { nickname: "Mr. Frodo", surname: "Baggins" }

Page 12: Surrounded by Graphs

Column Family StoresUse Cases

Recommended Not RecommendedEvent Logging ACID TransactionsContent Management Systems, Bloggingplatforms

Early Prototypes andSpikes

CountersExpiring Usage

Page 13: Surrounded by Graphs

Graph DatabasesDefinition

... store entities and relationships between thoseentities.

Example create (bilbo:Hobbit {name:'Bilbo Baggins'}); create (frodo:Hobbit {name: 'Frodo Baggins'}); create(bilbo)-[:UNCLE_OF]->(frodo);

Page 14: Surrounded by Graphs

Graph DatabasesUse Cases

Recommended Not RecommendedConnected Data Cases where you update all (or a

subset) of entitiesRouting, Dispatch, Locationbased services

(because Global Graph Operations)

Recommendation EnginesFraud Detection

Page 15: Surrounded by Graphs

Chart or Graph?

image: giphy.com

Page 16: Surrounded by Graphs

Chart or Graph?image: giphy.com

Page 17: Surrounded by Graphs

Chart or Graph?From Ancient Greek su�x -γραφω (-graphō), fromγράφω (gráphō, “to scratch, to scrape, to graze”),

from whence also -graphy.

Wiktionary

Page 18: Surrounded by Graphs

Chart or Graph?Every invariant and covariant thus becomes

expressible by a graph precisely identical with aKekuléan diagram or chemicograph.

JJ Sylvester, 1878

image: Wikipedia

Page 19: Surrounded by Graphs

Graph all the bridgesimage: Wikipedia

Page 20: Surrounded by Graphs

Graph all the bridgesimage: Wikipedia

Page 21: Surrounded by Graphs

Graph all the things: Make all: foo bar baz bar: touch bar foo: bar touch foo baz: bar touch baz

Page 22: Surrounded by Graphs

Graph all the things: Make digraph G { n3[label="all", color="red"]; n5[label="bar", color="red"]; n6[label="baz", color="red"]; n2[label="examples/Makefile", color="green"]; n4[label="foo", color="red"]; n5 -> n3 ; n6 -> n3 ; n4 -> n3 ; n5 -> n6 ; n5 -> n4 ; }

Page 23: Surrounded by Graphs

Graph all the things: Make

Page 24: Surrounded by Graphs

Graph all the things: Maven

Page 25: Surrounded by Graphs

Graph all the things: Git

Page 26: Surrounded by Graphs

http://www.cse.scu.edu/

Graph all the things: Linux

image: http://www.cse.scu.edu/

Page 27: Surrounded by Graphs

Story Time(a convoluted Neo4j Demo)

Page 28: Surrounded by Graphs

Cool story

Page 29: Surrounded by Graphs

Cool story digraph Book { page1 [label="You're On Concorde"]; page1 -> page6; page6 [label="You meet aliens"]; page6 -> page3 [label="Demand to go home"]; page6 -> page4 [label="Bond with U-TY"]; page3 [label="You're feeling sleepy"]; page3 -> page5 [label="Have a nap"]; page3 -> page16 [label="Stay Awake"]; page3 -> page8 [label="You meet Incu"];

Page 30: Surrounded by Graphs

Cool graph

Page 31: Surrounded by Graphs

Cypher Query LanguageDeclarative, like SQLAllows query or update of the graphBuilt on ASCII artNamed after a Matrix character

Page 32: Surrounded by Graphs

Cypher Query Language CREATE (page1:Beginning:Page {number:1, synopsis: 'You are on Concorde, and something comes towards you' (page6:Page {number:6, synopsis: 'You in in a circular room'}), (page3:Page {number:3, synopsis: 'You are in another room with other people. You are feeling sleepy.' (page4:Page {number:4, synopsis: 'You ask the U-TY masters about themselves.'}), page1-[:TURNS_TO]->page6, page6-[:TURNS_TO {decision: 'Demand to be taken to Earth'}]->page3, page6-[:TURNS_TO {decision: 'Ask the U-TY about themselves'}]->page4, (page5:Page {number:4, synopsis: 'There is a band on your head'}), (page8:Page {number:8, synopsis: 'You meet Incu, captive from Alara'}), (page16:Page {number:16, synopsis: 'You meet Ingmar, the Swedish kid from 1682'}),

Page 33: Surrounded by Graphs

How many pages are there? MATCH (p:Page) RETURN count(p) as Pages;

+-------+ | Pages | +-------+ | 80 | +-------+ 1 row 238 ms

Page 34: Surrounded by Graphs

How many endings are there? match (e:Ending) return count(e) as Endings;

+---------+ | Endings | +---------+ | 27 | +---------+ 1 row 25 ms

Page 35: Surrounded by Graphs

How many Decisions? match (p1)-[r:TURNS_TO]->(p2) where has(r.decision) return count(r.decision) as Decisions;

+-----------+ | Decisions | +-----------+ | 59 | +-----------+ 1 row 198 ms

Page 36: Surrounded by Graphs

How many paths through thebook?

match (b:Beginning)-[r:TURNS_TO*]-(e:Ending) return count(b) as Paths;

+-------+ | Paths | +-------+ | 125 | +-------+ 1 row 250 ms

Page 37: Surrounded by Graphs

What's the shortest path? match (b:Beginning)-[r:TURNS_TO*]-(e:Ending) return length(r) as Length, e.synopsis as Ending order by length(r) limit 1;

+------------------------------------+ | Length | Ending | +------------------------------------+ | 4 | "You fall asleep forever" | +------------------------------------+ 1 row 88 ms

Page 38: Surrounded by Graphs

What's the longest path? match (b:Beginning)-[r:TURNS_TO*]-(e:Ending) return length(r) as Length, e.synopsis as Ending order by length(r) desc limit 1;

+--------------------------------------------------------------------------------+ | Length | Ending | +--------------------------------------------------------------------------------+ | 19 | "You send the U-TY on their way, feeling guilt about abandoning Incu" | +--------------------------------------------------------------------------------+ 1 row 138 ms

Page 39: Surrounded by Graphs

What's in the shortest path? match (b:Beginning), (e:Ending), path = shortestPath((b)-[*]-(e)) return nodes(path);

|+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| [Node[20]{number:1,synopsis:"You are on Concorde, and something comes towards you"},Node[21]{number:6,synopsis:"You in in a circular room"},Node[22]{number:3,synopsis:"You are in another room with other people. You are feeling sleepy."},Node[24]{number:4,synopsis:"There is a band on your head"},Node[31]{number:14,synopsis:"You fall asleep forever"}] || [Node[20]{number:1,synopsis:"You are on Concorde, and something comes towards you"},Node[21]{number:6,synopsis:"You in in a circular room"},Node[23]{number:4,synopsis:"You ask the U-TY masters about themselves."},Node[28]{number:25,synopsis:"You irritate the U-TY"},Node[29]{number:114,synopsis:"You are at home, with a rock in your pocket"},Node[32]{number:41,synopsis:"You miss the basket, realize you were on ship, nobody beleives you"}] || [Node[20]{number:1,synopsis:"You are on Concorde, and something comes towards you"},Node[21]{number:6,synopsis:"You in in a circular room"},Node[22]{number:3,synopsis:"You are in another room with other people. You are feeling sleepy."},Node[24]{number:4,synopsis:"There is a band on your head"},Node[30]{number:11,synopsis:"You take the headband off, escape and meet Mopo"},Node[36]{number:111,synopsis:"The UFO has landed on the ocean"},Node[35]{number:21,synopsis:"Mopo runs away, you are rescued by fishermen"}]

Page 40: Surrounded by Graphs

Wat? Oh. match (b:Beginning), (e:Ending), path = shortestPath((b)-[*]-(e)) return nodes(path) as ShortestPath limit 1;

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| ShortestPath | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| [Node[20]{number:1,synopsis:"You are on Concorde, and something comes towards you"},Node[21]{number:6,synopsis:"You in in a circular room"},Node[22]{number:3,synopsis:"You are in another room with other people. You are feeling sleepy."},Node[24]{number:4,synopsis:"There is a band on your head"},Node[31]{number:14,synopsis:"You fall asleep forever"}] |+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Page 41: Surrounded by Graphs

Where is Ultima? match (p:Page) where p.synopsis =~ '.*Ultima.*' return p;

+-------------------------------------------------------------------+ | p | +-------------------------------------------------------------------+ | Node[98]{number:101,synopsis:"You Find Ultima! It is paradise."} | +-------------------------------------------------------------------+ 1 row 5 ms

Page 42: Surrounded by Graphs

What links to Ultima? match page-[:TURNS_TO]->(p:Page {number: 101}) return page;

+------+ | page | +------+ +------+ 0 row 4 ms

Page 43: Surrounded by Graphs

Does anything link to it? match n-[*]-(p:Page {number: 101}) return n, p;

+---------------------------------------------------------------------------------------------------------------------------- ---------------------------------+ | n | p | +---------------------------------------------------------------------------------------------------------------------------- ---------------------------------+ | Node[2684]{number:104,synopsis:"Your new friends let you hang out any time you like"} | Node[2683]{number:101,synopsis:"You Find Ultima! It is paradise."} | +---------------------------------------------------------------------------------------------------------------------------- ---------------------------------+ 1 row 10 ms

Page 44: Surrounded by Graphs

Ultima Visualized

Page 45: Surrounded by Graphs

About Neo4jDrivers for Java, Ruby, Python, Scala, Perl, Clojure (and more)GPL Community editionAGPL EnterpriseWritten in Java and ScalaOpen sourced in 2007https://github.com/neo4j/neo4jhttp://neo4j.com

Page 46: Surrounded by Graphs

Thank youQuestions?

@neo4j

@builddoctorimage: memegenerator

Page 47: Surrounded by Graphs

Circular Dependenciescreate (alice:Person {name:"Alice"})-[:LOVES]->

(bob:Person {name:"Bob"}),(bob)-[:LOVES]->(carol:Person {name:"Carol"}),

(carol)-[:LOVES]->(alice)

Page 48: Surrounded by Graphs

Circular Dependenciesmatch (n)-[:LOVES]->(m) return n.name,m.name

+-------------------+ | n.name | m.name | +-------------------+ | "Alice" | "Bob" | | "Bob" | "Carol" | | "Carol" | "Alice" | +-------------------+ 6 rows 39 ms