Graph databases in PHP @ PHPCon Poland 10-22-2011

33,221 views

Published on

Presentation given at the national PHP conference in Poland, in Kielce, October 2011, dealing with the introduction of graph databases in PHP, taking a practical look at OrientDB.

Published in: Technology
1 Comment
26 Likes
Statistics
Notes
  • Worth reading
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
33,221
On SlideShare
0
From Embeds
0
Number of Embeds
23,563
Actions
Shares
0
Downloads
202
Comments
1
Likes
26
Embeds 0
No embeds

No notes for slide

Graph databases in PHP @ PHPCon Poland 10-22-2011

  1. 1. GraphDB in PHPAlessandro Nadalin David Funaro 1
  2. 2. Agenda•Theory•When to use a graph?•Why graphDB?•The graphDB community•OrientDB•OrientDB in PHP•Demo 2
  3. 3. Essential (Theory) 3
  4. 4. Essential (Theory)G=Graph 3
  5. 5. Essential (Theory)G = (V, VertexGraph 3
  6. 6. Essential (Theory)G = (V, VertexGraph A 3
  7. 7. Essential (Theory)G = (V, E) VertexGraph Edge A 3
  8. 8. Essential (Theory)G = (V, E) VertexGraph Edge A 3
  9. 9. Binary Relation Hates A BItchy Scratchy 4
  10. 10. Binary Relation Edge A BVertex Vertex 4
  11. 11. Graph B E FA D G 5
  12. 12. Undirected Graph B E A D FExample: Friendship 6
  13. 13. Directed Edge A BVertex Vertex 7
  14. 14. Directed Edge Edge A BVertex Vertex 7
  15. 15. Directed Graph A B A F DExample: Followee 8
  16. 16. Path B E FA D G 9
  17. 17. PathA B D G E F 10
  18. 18. Graph -> GraphDBGraphDB is a database that use the Graph as its primary data structure 11
  19. 19. ... when to use a graph ?
  20. 20. Web in ’99 13
  21. 21. Web in 2005 14
  22. 22. The social web 15
  23. 23. Your data is a graph 16
  24. 24. a tree is a graph 17
  25. 25. parent_id is a graph 18
  26. 26. Recommendations lives inJohn type shows Mr Fun Cinema B Bean loca tion lik Rome es shows Cinema A location type Thriller Se7en s ho ws location Milan Cinema C 19
  27. 27. Recommendations lives inJohn type shows Mr Fun Cinema B Bean loca tion lik Rome es type shows Cinema A location ✓ x x Thriller Se7en s ho ws location Milan Cinema C 20
  28. 28. Recommendations lives inJohn type shows Mr Fun Cinema B Bean ✓ loca tion lik Rome es type shows Cinema A location ✓ x x Thriller Se7en s ho ws location Milan Cinema C 21
  29. 29. Recommendations lives inJohn type shows Mr Fun Cinema B ✓ Bean ✓ loca tion lik Rome es type shows Cinema A location ✓ x x Thriller Se7en s ho ws location Milan Cinema C 22
  30. 30. Recommendations lives inJohn xFun type Mr ✓ Bean shows Cinema B ✓ loca tion lik Rome es type shows Cinema A location ✓ x x Thriller Se7en s ho ws location Milan Cinema C 23
  31. 31. Recommendations lives inJohn x x x Fun type Mr Bean shows Cinema B loca tion lik Rome es type shows ✓ Cinema A location ✓ x x Thriller Se7en s ho ws location Milan Cinema C 24
  32. 32. Recommendations lives inJohn x x x Fun type Mr Bean shows Cinema B loca tion lik Rome es type shows ✓ Cinema A location ✓ ✓ shows x x Thriller Se7en location Milan Cinema C 25
  33. 33. Recommendations lives inJohn x x x Fun type Mr Bean shows Cinema B loca tion lik Rome es type shows ✓ Cinema A location ✓ ✓ shows x x ✓ Thriller Se7en location Milan Cinema C 26
  34. 34. Recommendations lives inJohn x x x Fun type Mr Bean shows Cinema B loca tion lik ✓ Rome es type shows ✓ Cinema A location ✓ ✓ shows x x ✓ Thriller Se7en location Milan Cinema C 27
  35. 35. Solve decision problems
  36. 36. Maximum flow
  37. 37. Given a dataset, calculate how to best organize it maximum flow
  38. 38. travelling salesman problem
  39. 39. The pizza guy needs to deliver on A, B,C.
  40. 40. Decision base on distance, traffic, time and so on.
  41. 41. Shortest path
  42. 42. Identify "special" nodes of the graph
  43. 43. Given your dataset, organize some clusters Are there some nodes which cannot belong to a cluster?They probably have some properties different from the average
  44. 44. Given your dataset, organize some clusters Are there some nodes which cannot belong to a cluster?They probably have some properties different from the average ACHTUNG! TERRORISTEN!
  45. 45. but ... why graphDB? 38
  46. 46. Representing a Graph in: http://www.slideshare.net/slidarko/problemsolving-using-graph-traversals-searching- scoring-ranking-and-recommendation# 39
  47. 47. Representing a Graph in: http://www.slideshare.net/slidarko/problemsolving-using-graph-traversals-searching- scoring-ranking-and-recommendation#✓ Relational Database (mysql, oracle)✓ Document Oriented DB (mongodb, couchdb)✓ XML Database (MarkLogic, eXist-db) 39
  48. 48. where is the difference ? 40
  49. 49. GraphDB A graph database is any storage system that provides index-free adjacency.http://www.slideshare.net/slidarko/problemsolving-using-graph-traversals-searching-scoring-ranking-and-recommendation
  50. 50. Step by step exampleGiven a list of people, find their homepages 42
  51. 51. Tree-based DB WAY 1 43
  52. 52. Tree-based DB WAY David Funaro put in the Search Engine 2 1 43
  53. 53. Tree-based DB WAY David Funaro put in the Search Engine 2 find 3 1 http://davidfunaro.com 43
  54. 54. Tree-based DB WAY David Funaro The cost to find Search Engine friend HP put in the a single 2grows as the friends HP tables grows find 3 1 http://davidfunaro.com 43
  55. 55. GraphDB WAYit’s like that the GraphDB has an additional information (the ancor <a>) 44
  56. 56. GraphDB WAY 1 get the embedded information(index) www.odino.orgit’s like that the GraphDB has an additional information (the ancor <a>) 44
  57. 57. GraphDB WAY The Anchor work as a local index to reach the document = index-free adjacency <a href=”http://odino.org”> Alessandro Nadalin </a> 45
  58. 58. Local costThe local cost is O(k) = Constant 46
  59. 59. Local costThe local cost is O(k) = Constant 47
  60. 60. Local cost 48
  61. 61. Local cost Thus, as the graph grows in size,the cost of a local step remain the same 48
  62. 62. any database can implicity represent a graph BUTonly a graph database make the graph structure explicit 49
  63. 63. BenchmarkDeph RDBMS Graph 1 100ms 30ms • 1 Million Vertex • 4 Million Edge 2 1000ms 500ms • Scale Free Tolopogy 3 10000ms 3000ms • Postgres VS Neo4J 4 100000m 50000ms s • Both Hash and BTree 5 N/A 100000m s 50 http://markorodriguez.com/2011/02/18/mysql-vs-neo4j-on-a-large-scale-graph-traversal/
  64. 64. community that is building and feeding the GraphDB ecosystem GraphDB community ThinkerPop Stack Databases
  65. 65. data model and their implementation Blueprints is a collection of interfaces, implementations,ouplementations, and test suites for the property graph data model. Blueprints is analogous to the JDBC, but for graph databases. https://github.com/tinkerpop/blueprints/wiki/
  66. 66. a data flow Framework using Process Graph provide a collection of "pipes" that are connected togheter to from processing pipelines
  67. 67. a graph-based programming language.a Turing-Complete graph-base programming language that compiles Gremlin syntax down to Pipes
  68. 68. a REST-full graph shell.Allow blueprints graph to be exposed through a RESTful API (HTTP)
  69. 69. Whats hot
  70. 70. OrientDB
  71. 71. Glossary RID <10:05>Cluster Position 58
  72. 72. Glossary RID <10:05>Cluster Position CLASS 58
  73. 73. Main features
  74. 74. Inheritance
  75. 75. class Vehicle class Carclass Bike
  76. 76. class Vehicle class Car class BikeSELECT FROM Vehicle WHERE owner = 1:1
  77. 77. class Vehicle class Car class Bikecan return records of class Bike or Car
  78. 78. Traversal
  79. 79. SELECT FROM fellas WHERE any() traverse(0,-1) ( @rid = [Michelle @rid] ) 66
  80. 80. SELECT FROM fellas WHERE any() traverse(0,-1) ( @rid = [Michelle @rid] ) 67
  81. 81. SELECT FROM fellas WHERE any() traverse(0,2) ( @rid = = [Michelle @rid] )) SELECT FROM fellas WHERE any() traverse(0,2) ( @rid [Michelle @rid]
  82. 82. SELECT FROM fellas WHERE any() traverse(0,2) ( @rid = [Michelle @rid] )
  83. 83. SQL synthax
  84. 84. beyond SQL
  85. 85. SELECT FROM authors WHERE book.title = ...
  86. 86. ACID
  87. 87. speaks JSON
  88. 88. { "schema": { "name": "Address" }, "result": [{ "@type": "d", "@rid": "#13:0", "@version": 6, "@class": "Address", "type": "Residence", "street": "Piazza Navona, 1", "city": "#14:0", "nick": "Luca2" }, { ... ...
  89. 89. Double Protocol
  90. 90. HTTP
  91. 91. HTTPUniversal
  92. 92. HTTPEasy to interact with
  93. 93. binary
  94. 94. binary Blazing fast
  95. 95. on-record SELECTs
  96. 96. SELECT FROM cats
  97. 97. SELECT FROM cats
  98. 98. SELECT FROM 11:0
  99. 99. SELECT FROM 11:0
  100. 100. SELECT FROM [11:0,11:1]
  101. 101. SELECT FROM [11:0,11:1]
  102. 102. SELECT FROM [11:0,12:0]
  103. 103. SELECT FROM [11:0,12:0]
  104. 104. stress-free setup
  105. 105. 2 Mb
  106. 106. ./orient/bin/server.sh 93
  107. 107. in-memory DB
  108. 108. or disk-persisted
  109. 109. Supports standards Supports standards 96
  110. 110. •Inheritance •Traversal •Sql syntax like •ACIDOrientDB •Speak JSON •Double protocol •on-record Select •ThinkerPop Compliant
  111. 111. Oh, its Java. 98
  112. 112. PHP ?
  113. 113. somebody started writing the binary-protocol bindinghttps://github.com/AntonTerekhov/OrientDB-PHP ( beta0.4.1, 28 April 2010 )
  114. 114. $db = new OrientDB($host, $port);$record = $db->recordLoad(1:1, *:-1);// $record instance of OrientDBRecord
  115. 115. and others
  116. 116. Orient Library... are writing a complete library https://github.com/congow/Orient 104
  117. 117. Orient = PHP Library to work with OrientDB 105
  118. 118. Data MapperQuery Builder HTTP Binding
  119. 119. HTTP Binding
  120. 120. use CongowOrient;use CongowOrientFoundationBinding;$driver   = new OrientHttpClientCurl();$orient   = new Binding($driver, 127.0.0.1, 2480, admin, admin, demo);$response = $orient->query("SELECT FROM Address");$output   = json_decode($response->getBody());foreach ($output->result as $address){  var_dump($address->street);}
  121. 121. use CongowOrient;use CongowOrientFoundationBinding;$driver   = new OrientHttpClientCurl();$orient   = new Binding($driver, 127.0.0.1, 2480, admin, admin, demo);$response = $orient->query("SELECT FROM Address");$output   = json_decode($response->getBody());foreach ($output->result as $address){  var_dump($address->street);}
  122. 122. use CongowOrient;use CongowOrientFoundationBinding;$driver   = new OrientHttpClientCurl();$orient   = new Binding($driver, 127.0.0.1, 2480, admin, admin, demo);$response = $orient->query("SELECT FROM Address");$output   = json_decode($response->getBody());foreach ($output->result as $address){  var_dump($address->street);}
  123. 123. use CongowOrient;use CongowOrientFoundationBinding;$driver   = new OrientHttpClientCurl();$orient   = new Binding($driver, 127.0.0.1, 2480, admin, admin, demo);$response = $orient->query("SELECT FROM Address");$output   = json_decode($response->getBody());foreach ($output->result as $address){  var_dump($address->street);}
  124. 124. use CongowOrient;use CongowOrientFoundationBinding;$driver   = new OrientHttpClientCurl();$orient   = new Binding($driver, 127.0.0.1, 2480, admin, admin, demo);$response = $orient->query("SELECT FROM Address");$output   = json_decode($response->getBody());foreach ($output->result as $address){  var_dump($address->street); { "schema": { "name": "Address"} }, "result": [{ "@type": "d", "@rid": "#13:0", "@version": 6, "@class": "Address", "type": "Residence", "street": "Piazza Navona, 1", "city": "#14:0", "nick": "Luca2" }, { ... ...
  125. 125. apart from ->query($SQL)
  126. 126. ->get|delete|postClass($class)
  127. 127. ->post|delete|put|getDocument($rid)
  128. 128. ...and much more!(connect, disconnect, ...)
  129. 129. Query Builder
  130. 130. use CongowOrientQuery;$query = new Query();$query->from(array(users))->where(username = ?, "admin");echo $query->getRaw(); // SELECT FROM users WHERE username = "admin"
  131. 131. use CongowOrientQuery;$query = new Query();$query->from(array(users))->where(username = ?, "admin");echo $query->getRaw(); // SELECT FROM users WHERE username = "admin"
  132. 132. use CongowOrientQuery;$query = new Query();$query->from(array(users))->where(username = ?, "admin");echo $query->getRaw(); // SELECT FROM users WHERE username = "admin"
  133. 133. use CongowOrientQuery;$query = new Query();$query->from(array(users))->where(username = ?, "admin");echo $query->getRaw(); // SELECT FROM users WHERE username = "admin"
  134. 134.    $query->select(array(name, username, email), false)    ->from(array(12:0, 12:1), false)    ->where(any() traverse ( any() like "%danger%" ))    ->orWhere("1 = ?", 1)    ->andWhere("links = ?", 1)    ->limit(20)    ->orderBy(username)    ->orderBy(name, true, true)    ->range("12:0", "12:1");  SELECT name, username, email   FROM [12:0, 12:1]   WHERE any() traverse ( any() like "%danger%" )  OR 1 = "1" AND links = "1"   ORDER BY name, username   LIMIT 20   RANGE 12:0 12:1
  135. 135. Data Mapper
  136. 136. A Doctrine2 strange ODM
  137. 137. namespace PolandPHPConEntity;use CongowOrientODMMapperAnnotations as ODM;/*** @ODMDocument(class="Person")*/class Speaker{    /**     * @ODMProperty( type="string")     */    protected $name;    public function setName($name)    {        $this->name = $name;    }
  138. 138. namespace PolandPHPConEntity;use CongowOrientODMMapperAnnotations as ODM;/*** @ODMDocument(class="Person")*/class Speaker{    /**     * @ODMProperty(type="string")     */    protected $name;    public function setName($name)    {        $this->name = $name;    }
  139. 139. namespace PolandPHPConEntity;use CongowOrientODMMapperAnnotations as ODM;/*** @ODMDocument(class="Person")*/class Speaker{    /**     * @ODMProperty(type="string")     */    protected $name;    public function setName($name)    {        $this->name = $name;    }
  140. 140. namespace PolandPHPConEntity;use CongowOrientODMMapperAnnotations as ODM;/*** @ODMDocument(class="Person")*/class Speaker{    /**     * @ODMProperty(type="string")     */    protected $name;    public function setName($name)    {        $this->name = $name;    }
  141. 141. Domain Driven Design
  142. 142. { "schema": { "name": "Speaker" }, "result": [{ "@type": "d", "@rid": "#1:0", "@version": 6, "@class": "Speaker", "name": "David Coallier" }, { ... ...
  143. 143. { "schema": { "name": "Speaker" }, "result": [{ "@type": "d", "@rid": "#1:0", "@version": 6, "@class": "Speaker", "name": "David Coallier" }, { ... ... $david = $mapper->hydrate(json_decode($speaker));
  144. 144. { "schema": { "name": "Speaker" }, "result": [{ "@type": "d", "@rid": "#1:0", "@version": 6, "@class": "Speaker", "name": "David Coallier" }, { ... ... $david instanceOf PolandPHPConEntitySpeaker
  145. 145. Repository Pattern$repo = $manager->getRepository(Speaker)
  146. 146. $speakers = $repo->findAll();
  147. 147. $speaker = $repo->find($rid);
  148. 148. $criteria = array(Name => Lorna); $lornas = $repo->findBy($criteria);
  149. 149. $criteria = array( Name => Lorna, last_name => Jane);$lornaJ = $repo->findOneBy($criteria);
  150. 150. Know your boundaries 138
  151. 151. https://github.com/doctrine/common/tree/master/lib/Doctrine/Common/Persistence 139
  152. 152. Theory sucks. 140
  153. 153. Demo
  154. 154. Demo Menu items in RDBMSid type page url1 external NULL http://www.google.com2 page 1 NULL 142
  155. 155. Demo Menu items in OrientDB Link PageLinkrid title {page ExternalLink rid title url9:1 home 1 8:2 google google.com 143
  156. 156. That’s all, folks! 144
  157. 157. That’s all, folks!David Funaro@ingdavidinohttp://davidfunaro.com 144
  158. 158. That’s all, folks!David Funaro Alessandro Nadalin@ingdavidino @_odino_http://davidfunaro.com http://odino.org 144
  159. 159. That’s all, folks!David Funaro Alessandro Nadalin@ingdavidino @_odino_http://davidfunaro.com http://odino.org 144
  160. 160. Creditshttp://www.flickr.com/photos/sayamindu/5677281218/sizes/l/in/photostream/ http://farm1.static.flickr.com/182/471383865_79d04aec36_o.png http://farm1.static.flickr.com/134/318947873_12028f1b66_b.jpg http://www.flickr.com/photos/atomdocs/3275758118/sizes/o/in/photostream/ http://www.flickr.com/photos/pattipics/5229478393/sizes/o/in/photostream/ http://www.flickr.com/photos/kongharald/366597251/sizes/o/in/photostream/ http://www.everaldo.com/ http://www.flickr.com/photos/tusnelda/6140792529/sizes/l/in/photostream/ http://www.flickr.com/photos/mondi/5368644355/sizes/l/in/photostream/ http://www.flickr.com/photos/jayneandd/4191106566/sizes/l/in/photostream/ http://www.flickr.com/photos/jooon/2093253534/sizes/l/in/photostream/ http://www.flickr.com/photos/bluedharma/89186151/sizes/o/in/photostream/ http://www.flickr.com/photos/exfordy/2747089295/sizes/l/in/photostream/ http://www.flickr.com/photos/nostri-imago/3137422976/sizes/o/in/photostream/ http://www.flickr.com/photos/fionasjournal/379587818/sizes/z/in/photostream/ http://www.flickr.com/photos/nperlapro/1297392267/ http://www.flickr.com/photos/fastphive/28428808/sizes/m/in/photostream/ http://www.flickr.com/photos/rnugraha/2003147365/sizes/o/in/photostream/ http://www.flickr.com/photos/zigazou76/4412946911/sizes/l/in/photostream/ http://www.flickr.com/photos/greatnet/4667555436/sizes/l/in/photostream/ http://www.flickr.com/photos/mnsc/2768391365/sizes/l/in/photostream/http://www.flickr.com/photos/christmaswithak/4675962453/sizes/l/in/photostream/ http://www.amazon.com/Trainspotting-Irvine-Welsh/dp/0393314804http://www.flickr.com/photos/franconadalin59/5778176872/sizes/l/in/photostream/ http://farm6.static.flickr.com/5176/5474445627_875d621689_b.jpg http://farm3.static.flickr.com/2243/2189435082_a16d3c89ae_b.jpg http://farm3.static.flickr.com/2647/3816311930_ac52cff491_o.jpg http://i130.photobucket.com/albums/p266/feike1977/PES6-4-3-3defencesettings.jpg http://images.usatoday.com/life/_photos/2006/11/30/numb3rs-topper.jpg http://www.flickr.com/photos/jakecaptive/3205277810/sizes/l/in/photostream/

×