Date post: | 21-Jan-2018 |
Category: |
Technology |
Upload: | julian-simpson |
View: | 270 times |
Download: | 0 times |
Surrounded by Graphsa short introduction to Graph Databases
and Neo4jCreated by , Neo Technology Inc. Julian Simpson
@builddoctor
Introduction: Who are you?Yes, you!
0
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
Agenda: What's this talk about?NoSQL LandscapeWhat are graphs, really?Graph ExamplesNeo4j Example and Story TimeQuestions
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
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
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"
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
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?" }
Document DatabaseUse Cases
Recommended Not RecommendedEvent Logging Complex Transactions spanning di�erent
documentsBlogging, CMSplatforms
Queries against varying aggregate structure
Web or Real-TimeanalyticsE-Commerce
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" }
Column Family StoresUse Cases
Recommended Not RecommendedEvent Logging ACID TransactionsContent Management Systems, Bloggingplatforms
Early Prototypes andSpikes
CountersExpiring Usage
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);
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
Chart or Graph?
image: giphy.com
Chart or Graph?image: giphy.com
Chart or Graph?From Ancient Greek su�x -γραφω (-graphō), fromγράφω (gráphō, “to scratch, to scrape, to graze”),
from whence also -graphy.
Wiktionary
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
Graph all the bridgesimage: Wikipedia
Graph all the bridgesimage: Wikipedia
Graph all the things: Make all: foo bar baz bar: touch bar foo: bar touch foo baz: bar touch baz
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 ; }
Graph all the things: Make
Graph all the things: Maven
Graph all the things: Git
http://www.cse.scu.edu/
Graph all the things: Linux
image: http://www.cse.scu.edu/
Story Time(a convoluted Neo4j Demo)
Cool story
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"];
Cool graph
Cypher Query LanguageDeclarative, like SQLAllows query or update of the graphBuilt on ASCII artNamed after a Matrix character
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'}),
How many pages are there? MATCH (p:Page) RETURN count(p) as Pages;
+-------+ | Pages | +-------+ | 80 | +-------+ 1 row 238 ms
How many endings are there? match (e:Ending) return count(e) as Endings;
+---------+ | Endings | +---------+ | 27 | +---------+ 1 row 25 ms
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
How many paths through thebook?
match (b:Beginning)-[r:TURNS_TO*]-(e:Ending) return count(b) as Paths;
+-------+ | Paths | +-------+ | 125 | +-------+ 1 row 250 ms
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
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
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"}]
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"}] |+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
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
What links to Ultima? match page-[:TURNS_TO]->(p:Page {number: 101}) return page;
+------+ | page | +------+ +------+ 0 row 4 ms
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
Ultima Visualized
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
Thank youQuestions?
@neo4j
@builddoctorimage: memegenerator
Circular Dependenciescreate (alice:Person {name:"Alice"})-[:LOVES]->
(bob:Person {name:"Bob"}),(bob)-[:LOVES]->(carol:Person {name:"Carol"}),
(carol)-[:LOVES]->(alice)
Circular Dependenciesmatch (n)-[:LOVES]->(m) return n.name,m.name
+-------------------+ | n.name | m.name | +-------------------+ | "Alice" | "Bob" | | "Bob" | "Carol" | | "Carol" | "Alice" | +-------------------+ 6 rows 39 ms