Neo4what?

  • 519 views
Uploaded on

Talk presented @JustJava 2013 …

Talk presented @JustJava 2013
Sources @ https://github.com/tiagobento/neo4j
Eder Ignatowicz and Tiago Bento Fernandes
Neo4j Java Gremlim Cypher REST Graph

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
519
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Neo4 what?A practical guide to Graph DatabasesFriday, June 7, 13
  • 2. @ederign /tiagobentoFriday, June 7, 13
  • 3. DextraGalera ponta firme que manda bemAmbiente de melhoria contínuaProjetos desafiadoresQualidade de vidaFriday, June 7, 13
  • 4. DATABIGFriday, June 7, 13
  • 5. Every 2 days we create asmuch information as wedid up to 2003”Eric Schmidt, Google“Friday, June 7, 13
  • 6. Our data is moreconnectedText (content)HyperText (addedpointers)RSS (joined thosepointers)Blogs (addedpingbacks)Friday, June 7, 13
  • 7. Data is more complexand semi-structuredFriday, June 7, 13
  • 8. If you tried to collect allthe data of every musicalbum ever made, howwould you model it?Friday, June 7, 13
  • 9. ReleaseDateComposerTrackArtistAlbumhasplayed on hascomposed byparticipated onhasFriday, June 7, 13
  • 10. ComposerAlbumparticipated onhasidNamerelease_dateartAlbum!idnamebirthComposer!album_idComposer_idAlbumComposer!Friday, June 7, 13
  • 11. You have to stretch your data(and your domain model)to fit in relational databasesLose semantics Accidental complexityObject-relational impedance mismatchTearsFriday, June 7, 13
  • 12. There is no“one size fits all”approachFriday, June 7, 13
  • 13. Key-ValueColumn FamilyDocumentGraphNot Only SQLFriday, June 7, 13
  • 14. The right toolfor the right jobFriday, June 7, 13
  • 15. Dont be a hipsterRDBMS&ComplexityBigTableClonesSizeKey-ValueStoreDocumentDatabasesGraphDatabases90% ofUse CasesRelationalDatabasesFriday, June 7, 13
  • 16. Why should I usea graph database?Friday, June 7, 13
  • 17. Graphs areeverywhereFriday, June 7, 13
  • 18. Highly connected data (social networks)Recommendations (e-commerce)Path Finding (how do i know you)A* (Least Cost path)Data First Schema (bottom-up)Schema EvolutionBest fits forFriday, June 7, 13
  • 19. The world is connectedFriday, June 7, 13
  • 20. Property graphFriday, June 7, 13
  • 21. What is a graph database?A database with an explicit graph structureEach node knows its adjacent nodesAs the number of the nodes increases,the cost of a local step (or hop) remains thesamePlus a index for lookupsFriday, June 7, 13
  • 22. Neo4jTHE graph databaseFriday, June 7, 13
  • 23. Graph Database + Lucene IndexProperty GraphEmbeddable and serverREST interfaceStableFriday, June 7, 13
  • 24. Full ACID (atomicity, consistency, isolation, durability)High Availability (with Enterprise Edition)32 Billion Nodes32 Billion Relationships64 Billion PropertiesSchema freeFriday, June 7, 13
  • 25. # persons query timeRelationaldatabase1000 2000msNeo4j 1000 2msNeo4j 1000000 2msSocial network“path exists”~1k persons~ 50 friends/personspathExists(a,b)depth 4Friday, June 7, 13
  • 26. If you’ve everJoined more than 7 tables togetherModeled a graph in a tableFells icky when need to “adapt” yourER model to fit on a DBTried to write some crazy view/storedprocedure with multiple recursive self an innerjoinsFriday, June 7, 13
  • 27. You should useFriday, June 7, 13
  • 28. Neo4jFriday, June 7, 13
  • 29. Neo4jStarting the serverFriday, June 7, 13
  • 30. EmbeddedGraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j</artifactId> <version>1.9</version> </dependency>Friday, June 7, 13
  • 31. Standaloneneo4j-community-1.9 tiagobento$ bin/neo4j startStarting Neo4j Server...WARNING: not changing userprocess [80169]... waiting for server to be ready..... OK.Go to http://localhost:7474/webadmin/for administration interface.Friday, June 7, 13
  • 32. Friday, June 7, 13
  • 33. Friday, June 7, 13
  • 34. Manipulating dataFriday, June 7, 13
  • 35. CypherNeo4j’s Query LanguageFriday, June 7, 13
  • 36. CREATE( _1 { name: "Radiohead" }),( _2 { name: "The Black Keys" }),( _3 { name: "Joy Division" }),( _4 { name: "Los hermanos" }),( _5 { name: "Oasis" }),( _6 { name: "Daft Punk" }),( _7 { name: "Felguk" }),( _8 { name: "Deadmau5" }),( eder { name: "Eder Ignatowicz", age: 22 }),( tiago { name: "Tiago Bento", age: 19 }),Friday, June 7, 13
  • 37. Tiago BentoEder IgnatowiczOasisLos hermanosJoy DivisionThe Black KeysRadioheadDaft PunkDeadmau5FelgukFriday, June 7, 13
  • 38. eder-[:LIKES]->_8,eder-[:LIKES]->_7,eder-[:LIKES]->_6,eder-[:LIKES]->_5,eder-[:LIKES]->_4,tiago-[:LIKES]->_6,tiago-[:LIKES]->_5,tiago-[:LIKES]->_4,tiago-[:LIKES]->_3,tiago-[:LIKES]->_2,tiago-[:LIKES]->_1RETURN *Friday, June 7, 13
  • 39. Tiago BentoEder IgnatowiczOasisLos hermanosJoy DivisionThe Black KeysRadioheadDaft PunkDeadmau5FelgukFriday, June 7, 13
  • 40. Tiago BentoEder IgnatowiczOasisLos hermanosJoy DivisionThe Black KeysRadioheadDaft PunkDeadmau5FelgukLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESFriday, June 7, 13
  • 41. Tiago BentoEder IgnatowiczOasisLos hermanosJoy DivisionThe Black KeysRadioheadDaft PunkDeadmau5FelgukLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESFriday, June 7, 13
  • 42. Eder LIKES?Friday, June 7, 13
  • 43. [ "Deadmau5", "Felguk", "Daft Punk", "Oasis", "Los hermanos" ]START ed=node(9)MATCH (ed)-[:LIKES]->(a)RETURN collect(a.name);Friday, June 7, 13
  • 44. Tiago BentoEder IgnatowiczOasisLos hermanosJoy DivisionThe Black KeysRadioheadDaft PunkDeadmau5FelgukLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESLIKESFriday, June 7, 13
  • 45. Eder TiagoLIKES LIKES?Friday, June 7, 13
  • 46. [ "Daft Punk", "Oasis", "Los hermanos" ]START ti=node(10), ed=node(9)MATCH (ed)-[:LIKES]->(a)<-[:LIKES]-(ti)RETURN collect(a.name);Musical compatibilityFriday, June 7, 13
  • 47. [ "Daft Punk" ]START ti=node(10), ed=node(9)MATCH (ed)-[:LIKES]->(a)<-[:LIKES]-(ti)WHERE a.name =~ "(?i)D.*"RETURN collect(a.name);Musical compatibilityFriday, June 7, 13
  • 48. GremlinChaining navigationFriday, June 7, 13
  • 49. Friday, June 7, 13
  • 50. GremlinFriday, June 7, 13
  • 51. ==> ==>          ,,,/==>          (o o)==> -----oOOo-(_)-oOOo-----==> ==> Available variables:==>   g = (neo4jgraph[EmbeddedGraphDatabase [data/graph.db]]==> , null)  out = (java.io.PrintStream@14c55ea==> , null)gremlin>Friday, June 7, 13
  • 52. g.addVertex(1, [name: "Lion", size: "Big" ]);g.addVertex(2, [name: "Aligator", size: "Big" ]);g.addVertex(3, [name: "Zebra", size: "Big" ]);g.addVertex(4, [name: "Deer", size: "Big" ]);g.addVertex(5, [name: "Giraf", size: "Huge" ]);...Friday, June 7, 13
  • 53. g.addEdge(g.v(1), g.v(3), EATS);g.addEdge(g.v(1), g.v(4), EATS);g.addEdge(g.v(1), g.v(5), EATS);g.addEdge(g.v(1), g.v(7), EATS);g.addEdge(g.v(1), g.v(6), EATS);...Friday, June 7, 13
  • 54. Aligator ZebraHumanBearFishLionGirafShrimpDeerBeeGrassPlanctonHoneyEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSFriday, June 7, 13
  • 55. Lion EATS EATS?Not relevantFriday, June 7, 13
  • 56. Start pointAligator ZebraHumanBearFishLionGirafShrimpDeerBeeGrassPlanctonHoneyEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSFriday, June 7, 13
  • 57. Aligator ZebraHumanBearFishLionGirafShrimpDeerBeeGrassPlanctonHoneyEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSFriday, June 7, 13
  • 58. Aligator ZebraHumanBearFishLionGirafShrimpDeerBeeGrassPlanctonHoneyEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSFriday, June 7, 13
  • 59. g.v(1).out(EATS).out(EATS).name.unique().sort()Lion’s IDBeeFishGrassHoneyShrimpFriday, June 7, 13
  • 60. g.v(1).out(EATS).loop(1){it.loops == 2}.name.unique().sort()BeeFishGrassHoneyShrimpLion’s IDFriday, June 7, 13
  • 61. Java APICypher Queries ExecutionTraversal FrameworkFriday, June 7, 13
  • 62. ExecutionEngine ee = new ExecutionEngine(graphDb);ExecutionResult result = ee.execute("START n=node(1) RETURN n;");Iterator<Node> columns = result.columnAs("n");for (Node node : IteratorUtil.asIterable(columns)) {System.out.println(node.getProperty("name"));}First nodeCypherFriday, June 7, 13
  • 63. Traversal FrameworkTraverse through your dataFriday, June 7, 13
  • 64. Lion EATS EATS?Not relevantFriday, June 7, 13
  • 65. Start pointAligator ZebraHumanBearFishLionGirafShrimpDeerBeeGrassPlanctonHoneyEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSFriday, June 7, 13
  • 66. Aligator ZebraHumanBearFishLionGirafShrimpDeerBeeGrassPlanctonHoneyEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSFriday, June 7, 13
  • 67. Aligator ZebraHumanBearFishLionGirafShrimpDeerBeeGrassPlanctonHoneyEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSEATSFriday, June 7, 13
  • 68. Node lion = graphDb.getNodeById(LION_ID); TraversalDescription td = Traversal.description() .depthFirst() .relationships(RelationshipTypes.EATS, Direction.OUTGOING) .evaluator(Evaluators.atDepth(2)).uniqueness(Uniqueness.NODE_LEVEL); for (Node node : td.traverse(lion).nodes()) { System.out.println(node.getProperty("name")); }BeeHoneyFishShrimpGrassFood chainFriday, June 7, 13
  • 69. Node lion = graphDb.getNodeById(LION_ID);TraversalDescription td = Traversal.description() .depthFirst() .relationships(RelationshipTypes.EATS, Direction.OUTGOING) .evaluator(Evaluators.atDepth(2)).uniqueness(Uniqueness.NODE_LEVEL); for (Path path : td.traverse(lion)) { System.out.println(path); }Food chain(1)--[EATS,4]-->(6)--[EATS,22]-->(9)(1)--[EATS,4]-->(6)--[EATS,11]-->(13)(1)--[EATS,4]-->(6)--[EATS,10]-->(7)(1)--[EATS,3]-->(7)--[EATS,12]-->(8)(1)--[EATS,2]-->(5)--[EATS,9]-->(11)Friday, June 7, 13
  • 70. REST APIFull power through HTTP RequestsFriday, June 7, 13
  • 71. DELETE PUTGETPOSTNodesRelationshipsNode LabelsIndexesTransactionsFriday, June 7, 13
  • 72. DELETE PUTGETPOSTNodesRelationshipsNode LabelsIndexesTransactionsFriday, June 7, 13
  • 73. POST http://localhost:7474/db/data/transactionTRANSACTIONPOST http://localhost:7474/db/data/transaction/7{  "statements" : [ {    "statement" : "CREATE n RETURN n"  } ]}POST http://localhost:7474/db/data/transaction/7/commitPOST http://localhost:7474/db/data/transaction/7{  "statements" : [ {    "statement" : "START n=node(1) DELETE n"  } ]}Friday, June 7, 13
  • 74. DELETE PUTGETPOSTNodesRelationshipsNode LabelsIndexesTransactionsFriday, June 7, 13
  • 75. {  "extensions" : {  },  "paged_traverse" : "http://localhost:7474/db/data/node/5/paged/traverse/{returnType}{?pageSize,leaseTime}",  "labels" : "http://localhost:7474/db/data/node/5/labels",  "outgoing_relationships" : "http://localhost:7474/db/data/node/5/relationships/out",  "traverse" : "http://localhost:7474/db/data/node/5/traverse/{returnType}",  "all_typed_relationships" : "http://localhost:7474/db/data/node/5/relationships/all/{-list|&|types}",  "property" : "http://localhost:7474/db/data/node/5/properties/{key}",  "all_relationships" : "http://localhost:7474/db/data/node/5/relationships/all",  "self" : "http://localhost:7474/db/data/node/5",  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/5/relationships/out/{-list|&|types}",  "properties" : "http://localhost:7474/db/data/node/5/properties",  "incoming_relationships" : "http://localhost:7474/db/data/node/5/relationships/in",  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/5/relationships/in/{-list|&|types}",  "create_relationship" : "http://localhost:7474/db/data/node/5/relationships", "data" : {    "foo" : "bar"  }}• POST http://localhost:7474/db/data/node• Accept: application/json• Content-Type: application/json{  "foo" : "bar"}• 201: Created• Content-Length: 1156• Content-Type: application/json• Location: http://localhost:7474/db/data/node/5Friday, June 7, 13
  • 76. • POST http://localhost:7474/db/data/cypher• Accept: application/json• Content-Type: application/json{  "columns" : [ "TYPE(r)" ],  "data" : [ [ "know" ] ]}{  "query" :"START x  = node:node_auto_index(name={startName})MATCH path = (x-[r]-friend)WHERE friend.name = {name}RETURN TYPE(r)",  "params" : {    "startName" : "Tiago Bento",    "name" : "Eder Ignatowicz"  }}• 200: OK• Content-Type: application/jsonFriday, June 7, 13
  • 77. Friday, June 7, 13
  • 78. @ederign /tiagobentoFriday, June 7, 13