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.

Neo4jrb

6,378 views

Published on

Introduction to Neo4j.rb - the Ruby wrapper of Neo4j. Also a quick introduction to Ruby.

Published in: Technology

Neo4jrb

  1. 2. Neo4j.rb The Benefits of Graph Database Andreas Ronge
  2. 3. Who am I ?
  3. 4. NoSQL The problem with SQL: not designed for <ul><ul><ul><li>Accelerating growth of data
  4. 5. Huge clustered environments
  5. 6. Complex and evolving data models </li></ul></ul></ul>Choose the right tools - N ot O nly SQL
  6. 7. The CAP Theorem Can't achieve all three, pick two <ul><ul><ul><li>C onsistency – all readers will see the same write
  7. 8. A vailability – tolerant of node failures
  8. 9. P artition tolerant – if lost interconnect between nodes </li></ul></ul></ul>Many NoSQL databases choose <ul><ul><ul><li>Sacrifice consistency over availability
  9. 10. Eventual consistency instead of ACID (but not Neo4j) </li></ul></ul></ul>
  10. 12. What is Neo4j.rb ? Neo4j <ul><ul><li>A Graph Database written in Java
  11. 13. Accessible from a Java API, but also </li><ul><li>HTTP/REST interface
  12. 14. Language binding - Python, Ruby, Scala, Groovy
  13. 15. Gremlin ./outE[@label='created']/inV/inE[@label='created']/outV[g:except($_)] </li></ul></ul></ul>Neo4j.rb <ul><ul><li>A language binding for (J) Ruby </li></ul></ul>
  14. 16. A Ruby Crash Course
  15. 18. What is a Graph Database ?
  16. 19. Graph DB vs. SQL id name 1 andreas 2 peter id p1_id p2_id since 1234 1 2 2002 People Friends friend since: 2002 name: andreas name: peter
  17. 20. Graph DB vs SQL
  18. 21. Benefit 1 Domain Modeling
  19. 22. Benefit 2 No O/R mismatch
  20. 23. Benefit 3 Semi-structured information
  21. 25. Benefit 4 No Schema
  22. 26. DEMO TIME Does Thomas Andersson know someone [who knows]* called Agent Smith ? Benefit 5 Deep traversals
  23. 27. Neo4j(.rb) Features Neo4j <ul><ul><ul><li>Embedded
  24. 28. ACID Transactions
  25. 29. Lucene Integration
  26. 30. Graph Algorithms </li></ul></ul></ul>Neo4j.rb <ul><ul><ul><li>Object Oriented Mapping
  27. 31. “Drop in” replacement for Rails Active Model
  28. 32. Improved/extended API (lucene, rules, migrations,...) </li></ul></ul></ul>
  29. 33. Embedded Easier to install, deploy & test Is running in same thread as your application No network connection to DB needed No Database Tier
  30. 34. Another Ruby Crash Course Ruby Java
  31. 35. Transactions ACID <ul><ul><ul><li>a tomicity, c onsistency, i solation, d urability
  32. 36. only write locks, no read locks </li></ul></ul></ul>
  33. 37. Object Oriented Mapping Neo4j and a Dynamic Language – A Perfect Match ! Ruby Class A Neo4j Node _class: Person name: andreas
  34. 38. How do I find things ? <ul><li>Start from Reference Node
  35. 39. Graph as an Index
  36. 40. Use Lucene </li></ul>
  37. 41. Reference Node Find Thomas Andersson Neo4j.ref_node.outgoing(:root).first REF_NODE Thomas Andersson
  38. 42. Use the Graph as an Index A A B Neo4j.ref_node.outgoing('A').each {...} RefNode name andreas name anders name bertil
  39. 43. Lucene Full-featured text search engine Features <ul><ul><ul><li>Phrase queries, wildcard queries, proximity queries, range queries and more
  40. 44. Ranked searching
  41. 45. Sorting
  42. 46. Date-range
  43. 47. Sorting by any field </li></ul></ul></ul>
  44. 48. Lucene in Neo4j.rb
  45. 49. NodeMixin Lucene Integration Declared properties DSL for relationship traversal Migrations Lists Cascade Delete Works with inheritance
  46. 50. Ruby Class Mapping: Relationships friend friend name andreas name peter name david
  47. 51. Incoming Relationship Same relationship different direction Same _class = Actor name = 'keanu' _class = Movie name = 'matrix'
  48. 52. Ruby on Rails/Active Record “drop in” replacement
  49. 55. A Common Problem I have a <ul><ul><ul><li>System already in production
  50. 56. Huge database </li></ul></ul></ul>I need to <ul><ul><ul><li>Change the structure of the database </li></ul></ul></ul>Solution: <ul><ul><ul><li>Migrations </li></ul></ul></ul>
  51. 57. Migrations: Direct
  52. 58. Migrations: Lazy (Neo4j::LazyMigrationMixin)
  53. 59. Inheritance
  54. 60. Recommendation Engine Likes Likes Likes Recommend This 1 2 4 3 5 Bertil Caesar Adam
  55. 61. Example, Recommendation likes other_people 4 2 5 3 1
  56. 62. Included Graph Algorithms Shortest paths, Simple paths, Graph measures ... node1 node2
  57. 63. High Availability Online Backup - hot spare Read-slave replication Write master election
  58. 64. Neo4j – An Object DB ? Neo4j has very fast traversals <ul><ul><ul><li>Avoids loading properties </li></ul></ul></ul>No need to declare two way relationships <ul><ul><ul><li>A relationship has a start and end node </li></ul></ul></ul>Does have two ways of finding objects <ul><ul><ul><li>Traversals
  59. 65. Lucene </li></ul></ul></ul>Optimized for Graph Algorithms
  60. 66. Conclusions: Benefits Express your domain as a Graph <ul><ul><ul><li>Domain Modeling
  61. 67. No O/R mismatch
  62. 68. Efficient storage of Semi Structured Information
  63. 69. Schema Less </li></ul></ul></ul>Express Queries as Traversals <ul><ul><ul><li>Fast deep traversal instead of slow SQL queries that span many table joins </li></ul></ul></ul>
  64. 70. When NOT use Graph DB Don't have a graph related problem ? Not too much changing requirements ? Easy to organized data into: <ul><ul><ul><li>Tables, Documents or Key-Value models ? </li></ul></ul></ul>Few & well defined relationships in the domain ? Don't have SQL queries that span many table joins ? Many YES => maybe Graph DB not a good choice
  65. 71. When should I use a Graph DB ? Need to solve a graph related problem ? <ul><ul><ul><li>Recommendations, Shortest path, Social Networks </li></ul></ul></ul>Have a complex and evolving data model ? Few mandatory and many optional attributes ? Big part of domain is expressed as relationships ? Have SQL queries that span many table joins ? Many YES => maybe a Graph DB is a good choice

×