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.

Use Neo4j In Your Next Java Project

213 views

Published on

Slides from my talk on Neo4j at the I Code Java Conference. Shows why Neo4j is a good choice and how to use it in Java.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Use Neo4j In Your Next Java Project

  1. 1. Use Neo4j In Your Next Java Project Love relationships again Tobias Coetzee @tobiascode
  2. 2. All Roads Lead To SQL Do we even have other choices? Every project we work on will have some form of database, 99% of those in the enterprise will be a relational database
  3. 3. Impedance Mismatch The database model does not match the domain model Not Scaling More data means less speed Complex Queries Lots of joins and some self joining nightmares as a bonus Relationship Problems
  4. 4. Facebook For Superheroes MEMBER_OF TOOK_PART_IN WAS_IN Characters Events Teams Stories CONSISTS_OF FRIEND_OF
  5. 5. Queries with lots of joins to get to the final answer Similar Data Models Configuration Security Navigation
  6. 6. Graph Databases Just Mathematics Based on graph theory, created by Leonhard Euler All About Relationships In graph databases relationships are first class citizens Neo4j Most well known and widely used graph database
  7. 7. Graph Databases Nodes Represent the entities or records in the database Relationships Link nodes together Properties Nodes and relationships can also have properties
  8. 8. MEMBER_OF MEMBER_OF FRIEND_OF WAS_IN TOOK_PART_IN • Eyes: Blue • Hair: None • Citizenship: Canada• Since: 14 Feb 2001 Graph Databases
  9. 9. The database model does not match the domain model Impedance Mismatch
  10. 10. Impedance Mismatch Character_Team Character_Event Team_Event Character_Story Team_Story Event_Story Friends MarvelCharacter Team Event Story
  11. 11. Traditional Solutions ORM Frameworks, e.g. Hibernate or Spring Data Boilerplate code generation tools Impedance Mismatch
  12. 12. Impedance Mismatch MEMBER_OF TOOK_PART_IN WAS_IN Characters Events Teams Stories CONSISTS_OF FRIEND_OF
  13. 13. MATCH (c:Character)-[:MEMBER_OF]->(t:Team) WHERE t.name = 'Avengers' RETURN c.name Cypher Intro
  14. 14. Demo Time
  15. 15. Lots of joins and some self joining nightmares as a bonus Complex Queries
  16. 16. Denormalise the database Hide complexity behind views and stored procedures Traditional Solutions Complex Queries
  17. 17. Friends of Friends Name Number Friends in Common Vision 51 Wolfsbane 38 Punisher 33 Rage 38 Shard 25 Wind Dancer 25 Possible friend recommendations for Deadpool
  18. 18. SELECT FriendOfFriend.Name, COUNT(*) FROM MarvelCharacter deadpool INNER JOIN Friends DeadpoolFriends ON deadpool.Id = DeadpoolFriends.CharacterId1 INNER JOIN Friends FriendsFriends ON DeadpoolFriends.CharacterId2 = FriendsFriends.CharacterId1 INNER JOIN MarvelCharacter FriendOfFriend ON FriendsFriends.CharacterId2 = FriendOfFriend.Id WHERE deadpool.Name = 'Deadpool' AND FriendsFriends.CharacterId2 NOT IN( SELECT CharacterId2 FROM MarvelCharacter INNER JOIN Friends ON MarvelCharacter.Id = CharacterId1 WHERE Name = 'Deadpool') GROUP BY FriendOfFriend.Name ORDER BY COUNT(*) DESC Sooo Many Joins
  19. 19. MATCH (deadpool:Character)-[:FRIEND_OF*2]->(FriendOfFriend) WHERE deadpool.name = 'Deadpool' AND NOT (deadpool)-[:FRIEND_OF]->(FriendOfFriend) AND NOT deadpool = FriendOfFriend RETURN FriendOfFriend.name, COUNT(*) ORDER BY COUNT(*) DESC, FriendOfFriend.name Sooo Many Joins
  20. 20. MATCH (deadpool:Character)-[:FRIEND_OF*2]->(FriendOfFriend) Sooo Many Joins FROM MarvelCharacter deadpool INNER JOIN Friends DeadpoolFriends ON deadpool.Id = DeadpoolFriends.CharacterId1 INNER JOIN Friends FriendsFriends ON DeadpoolFriends.CharacterId2 = FriendsFriends.CharacterId1 INNER JOIN MarvelCharacter FriendOfFriend ON FriendsFriends.CharacterId2 = FriendOfFriend.Id
  21. 21. Quick Demo
  22. 22. How Do You Know Him? MEMBER_OF MEMBER_OF FRIEND_OF How can Deadpool connect to Ironman?
  23. 23. MATCH (deadpool:Character { name:"Deadpool" }), (other:Character { name:"Ultron" }), path = shortestPath((deadpool)-[*]-(other)) RETURN path Shortest Path
  24. 24. MATCH (deadpool:Character { name:"Deadpool" }), (other:Character { name:"Ultron" }), path = shortestPath((deadpool)-[*]-(other)) RETURN path Shortest Path
  25. 25. MATCH (deadpool:Character { name:"Deadpool" }), (other:Character { name:"Ultron" }), path = shortestPath((deadpool)-[*]-(other)) RETURN path Shortest Path
  26. 26. MATCH (deadpool:Character { name:"Deadpool" }), (other:Character { name:"Ultron" }), path = shortestPath((deadpool)-[*]-(other)) RETURN path Shortest Path
  27. 27. Quick Demo
  28. 28. Not Scaling More data means less speed
  29. 29. Traditional Solutions Offline databases Batch runs to process data More indexes Not Scaling
  30. 30. Speed Embedded Server Processing requires reading the same data you wrote Fixed Size Records All records of the same type have the same size on disk Index-Free Adjacency Pointer to the next node
  31. 31. Demo Time
  32. 32. Wrong Usage This is not a hammer
  33. 33. Wrong Usage Set Orientated Lists of things with few or no joins Global Operations Made for local graph operations Aggregate Queries Processing requires reading the same data you wrote
  34. 34. Thank You For Staying Till The End! @tobiascode

×