Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Surrounded by Graphs

295 views

Published on

My Linux.conf.au presentation from 2015

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Surrounded by Graphs

  1. 1. Surrounded by Graphs a short introduction to Graph Databases and Neo4j Created by , Neo Technology Inc.Julian Simpson @builddoctor
  2. 2. Introduction: Who are you? Yes, you! 0
  3. 3. Introduction: Who am I? Dotbomb Solaris Admin Reformed Java Build Monkey DevOps Agitator (Who actually went to DevOpsDays conference #1) Full Stack Developer at Neo Technology, makers of Neo4j
  4. 4. Agenda: What's this talk about? NoSQL Landscape What are graphs, really? Graph Examples Neo4j Example and Story Time Questions
  5. 5. 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 properties No schema, allowing elds to be added to any record without controls
  6. 6. 4 kinds of NoSQL Database Key-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 Emerging World of Polyglot Persistence Paperback – August 18, 2012 by Pramod J. Sadalage, Martin Fowler
  7. 7. Key Value Definition A simple hash table, primarily used when all access to the database is via primary key Example <SEThobbit:name"baggins" OK <GEThobbit:name "baggins"
  8. 8. Key Value Use Cases Recommended Not Recommended Storing Session Information Relationships among data User Pro les and Preferences Multi Operation Transactions Shopping Cart Data Operations by Sets Query by Data
  9. 9. Document Database Definition ... documents are self-describing, hierarchical tree data structures which can consist of maps, collections and scalar values. Example { "_id":1, "name":{ "first":"Bilbo", "last":"Baggins" }, "comment":"Aren'twegladthishobbitbusinessisdone?" }
  10. 10. Document Database Use Cases Recommended Not Recommended Event Logging Complex Transactions spanning di erent documents Blogging, CMS platforms Queries against varying aggregate structure Web or Real-Time analytics E-Commerce
  11. 11. Column Family Stores Definition ... store data with keys mapped to values and the values grouped into multiple column families, each column family being a map of data Example { "bilbo":{ age:"eleventysix", surname:"baggins" }, "frodo":{ nickname:"Mr.Frodo", surname:"Baggins" }
  12. 12. Column Family Stores Use Cases Recommended Not Recommended Event Logging ACID Transactions Content Management Systems, Blogging platforms Early Prototypes and Spikes Counters Expiring Usage
  13. 13. Graph Databases Definition ... store entities and relationships between those entities. Example create(bilbo:Hobbit{name:'BilboBaggins'}); create(frodo:Hobbit{name:'FrodoBaggins'}); create(bilbo)-[:UNCLE_OF]->(frodo);
  14. 14. Graph Databases Use Cases Recommended Not Recommended Connected Data Cases where you update all (or a subset) of entities Routing, Dispatch, Location based services (because Global Graph Operations) Recommendation Engines Fraud Detection
  15. 15. Chart or Graph?
  16. 16. Chart or Graph? image: giphy.com
  17. 17. Chart or Graph? From Ancient Greek su x -γραφω (-graphō), from γράφω (gráphō, “to scratch, to scrape, to graze”), from whence also -graphy. Wiktionary
  18. 18. Chart or Graph? Every invariant and covariant thus becomes expressible by a graph precisely identical with a Kekuléan diagram or chemicograph. JJ Sylvester, 1878 image: Wikipedia
  19. 19. Graph all the bridges image: Wikipedia
  20. 20. Graph all the bridges image: Wikipedia
  21. 21. Graph all the things: Make all:foobarbaz bar: touchbar foo:bar touchfoo baz:bar touchbaz
  22. 22. Graph all the things: Make digraphG{ 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; }
  23. 23. Graph all the things: Make
  24. 24. Graph all the things: Maven
  25. 25. Graph all the things: Git
  26. 26. http://www.cse.scu.edu/ Graph all the things: Linux image: http://www.cse.scu.edu/
  27. 27. Story Time (a convoluted Neo4j Demo)
  28. 28. Cool story
  29. 29. Cool story digraphBook{ page1[label="You'reOnConcorde"]; page1->page6; page6[label="Youmeetaliens"]; page6->page3 [label="Demandtogohome"]; page6->page4 [label="BondwithU-TY"]; page3[label="You'refeelingsleepy"]; page3->page5 [label="Haveanap"]; page3->page16[label="StayAwake"]; page3->page8 [label="YoumeetIncu"];
  30. 30. Cool graph
  31. 31. Cypher Query Language Declarative, like SQL Allows query or update of the graph Built on ASCII art Named after a Matrix character
  32. 32. Cypher Query Language CREATE (page1:Beginning:Page{number:1,synopsis:'YouareonConcorde,andsomethingcomestowards (page6:Page{number:6,synopsis:'Youininacircularroom'}), (page3:Page{number:3,synopsis:'Youareinanotherroomwithotherpeople. Youarefeelingsleep (page4:Page{number:4,synopsis:'YouasktheU-TYmastersaboutthemselves.'}), page1-[:TURNS_TO]->page6, page6-[:TURNS_TO{decision:'DemandtobetakentoEarth'}]->page3, page6-[:TURNS_TO{decision:'AsktheU-TYaboutthemselves'}]->page4, (page5:Page {number:4,synopsis:'Thereisabandonyourhead'}), (page8:Page {number:8,synopsis:'YoumeetIncu,captivefromAlara'}), (page16:Page{number:16,synopsis:'YoumeetIngmar,theSwedishkidfrom1682'}),
  33. 33. How many pages are there? MATCH(p:Page) RETURNcount(p)asPages; +-------+ | Pages | +-------+ | 80 | +-------+ 1 row 238 ms
  34. 34. How many endings are there? match(e:Ending) returncount(e)asEndings; +---------+ | Endings | +---------+ | 27 | +---------+ 1 row 25 ms
  35. 35. How many Decisions? match(p1)-[r:TURNS_TO]->(p2) wherehas(r.decision) returncount(r.decision)asDecisions; +-----------+ | Decisions | +-----------+ | 59 | +-----------+ 1 row 198 ms
  36. 36. How many paths through the book? match(b:Beginning)-[r:TURNS_TO*]-(e:Ending) returncount(b)asPaths; +-------+ | Paths | +-------+ | 125 | +-------+ 1 row 250 ms
  37. 37. What's the shortest path? match(b:Beginning)-[r:TURNS_TO*]-(e:Ending) returnlength(r)asLength,e.synopsisasEnding orderbylength(r) limit1; +------------------------------------+ | Length | Ending | +------------------------------------+ | 4 | "You fall asleep forever" | +------------------------------------+ 1 row 88 ms
  38. 38. What's the longest path? match(b:Beginning)-[r:TURNS_TO*]-(e:Ending) returnlength(r)asLength,e.synopsisasEnding orderbylength(r)desc limit1; +--------------------------------------------------------------------------------+ |Length|Ending | +--------------------------------------------------------------------------------+ |19 |"YousendtheU-TYontheirway,feelingguiltaboutabandoningIncu"| +--------------------------------------------------------------------------------+ 1row 138ms
  39. 39. What's in the shortest path? match(b:Beginning),(e:Ending), path=shortestPath((b)-[*]-(e)) returnnodes(path); +--------------------------------------------------------- | [Node[20]{number:1,synopsis:"You are on Concorde, and so | [Node[20]{number:1,synopsis:"You are on Concorde, and so | [Node[20]{number:1,synopsis:"You are on Concorde, and so
  40. 40. Wat? Oh. match(b:Beginning),(e:Ending), path=shortestPath((b)-[*]-(e)) returnnodes(path)asShortestPath limit1; +---------------------------------------------------------------------------------------------------------------------- |ShortestPath| +---------------------------------------------------------------------------------------------------------------------- |[Node[20]{number:1,synopsis:"YouareonConcorde,andsomethingcomestowardsyou"},Node[21]{number:6,synopsis:"Youi +----------------------------------------------------------------------------------------------------------------------
  41. 41. Where is Ultima? match(p:Page) wherep.synopsis=~'.*Ultima.*' returnp; +-------------------------------------------------------------------+ |p | +-------------------------------------------------------------------+ |Node[98]{number:101,synopsis:"YouFindUltima! Itisparadise."}| +-------------------------------------------------------------------+ 1row 5ms
  42. 42. What links to Ultima? matchpage-[:TURNS_TO]->(p:Page{number:101}) returnpage; +------+ | page | +------+ +------+ 0 row 4 ms
  43. 43. Does anything link to it? matchn-[*]-(p:Page{number:101}) returnn,p; +---------------------------------------------------------------------------------------------------------------------------- ---------------------------------+ |n |p | +---------------------------------------------------------------------------------------------------------------------------- ---------------------------------+ |Node[2684]{number:104,synopsis:"Yournewfriendsletyouhangoutanytimeyoulike"}|Node[2683]{number:101,synopsis:"You FindUltima! Itisparadise."}| +---------------------------------------------------------------------------------------------------------------------------- ---------------------------------+ 1row 10ms
  44. 44. Ultima Visualized
  45. 45. About Neo4j Drivers for Java, Ruby, Python, Scala, Perl, Clojure (and more) GPL Community edition AGPL Enterprise Written in Java and Scala Open sourced in 2007 https://github.com/neo4j/neo4j http://neo4j.com
  46. 46. Thank you Questions? @neo4j @builddoctor image: memegenerator
  47. 47. Circular Dependencies create (alice:Person {name:"Alice"})-[:LOVES]-> (bob:Person {name:"Bob"}), (bob)-[:LOVES]->(carol:Person {name:"Carol"}), (carol)-[:LOVES]->(alice)
  48. 48. Circular Dependencies match (n)-[:LOVES]->(m) return n.name,m.name +-------------------+ | n.name | m.name | +-------------------+ | "Alice" | "Bob" | | "Bob" | "Carol" | | "Carol" | "Alice" | +-------------------+ 6 rows 39 ms

×