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.

Intro to Spring Data Neo4j

7,196 views

Published on

These webinar slides provide a quick tour through graphs, graph databases, neo4j and spring data neo4j. The example app discussed is a twitter-graph.

Published in: Technology, Education

Intro to Spring Data Neo4j

  1. 1. Spring Data Neo4j 2.1 IntroductionMichael Hunger, Neo Technology
  2. 2. Me & You ? ๏ Me: Michael Hunger, Neo Technology • Neo4j Community Team • Spring Data Neo4j Project Lead • Neo4j Cloud Hosting • michael.hunger@neotechnology.com / @mesirii ๏ You: • Java & Spring ! • Enterprise ? • NOSQL ? • Cloud (CF, Heroku) ?
  3. 3. About the Webinar ๏ Graphs are EveryWhere ๏ Graph Database ๏ Intro Neo4j ๏ Sample App - Twitter Graph • Domain Model • Code ๏ Resources, Books, GraphConnect ๏ Questions answered during webinar and Q & A at the end ๏ All your questions will be answered in a follow up blog post, don‘t hesitate to ask
  4. 4. To become part of todays Twitter-Graph
  5. 5. To become part of todays Twitter-Graphplease tweet with #neo4j #sdn
  6. 6. To become part of todays Twitter-Graphplease tweet with #neo4j #sdn •your attendance •questions •remarks •....
  7. 7. Graphs areeverywhere 5
  8. 8. 76
  9. 9. Science76
  10. 10. ScienceBiologyChemistry 7 6
  11. 11. ScienceBiologyChemistry Proteins Reactions Environments 7 6 Genes
  12. 12. Science Biology Chemistry Proteins Reactions Politics Environments 7 6 GenesSociety History
  13. 13. Science Biology Work FriendsNeighbor Family Chemistry Proteins Reactions Politics Environments 7 6 Genes Society History
  14. 14. Computers Internet Science Biology Work FriendsNeighbor Family Chemistry Proteins Reactions Politics Environments 7 6 Genes Society History
  15. 15. Devices Users Software Networks Services Computers Internet Science Biology Work FriendsNeighbor Family Chemistry Proteins Reactions Politics Environments 7 6 Genes Society History
  16. 16. Devices Users Software Networks Services Computers Internet Business Science Biology Work FriendsNeighbor Family Chemistry Proteins Reactions Politics Environments 7 6 Genes Society History
  17. 17. Devices Users Software Networks Services Computers Internet Business Science ERP Biology Work Geo Friends RetailNeighbor Family Cloud Chemistry Travel Proteins Reactions Politics Environments 7 6 Genes Society History
  18. 18. Devices Users Software Networks Services Computers Internet Art Business Science ERP Biology Work Geo Friends RetailNeighbor Family Cloud Chemistry Travel Proteins Reactions Politics Environments 7 6 Genes Society History
  19. 19. Devices Stories Users Software Creativity Visuals Networks Services Computers Internet Art Business Science ERP Biology Work Geo Friends RetailNeighbor Family Cloud Chemistry Travel Proteins Reactions Politics Environments 7 6 Genes Society History
  20. 20. A Graph Database Node Node
  21. 21. A Graph Database Typed relationship Node Node
  22. 22. A Graph Database Typed relationship Node Node key: value key: value
  23. 23. Neo4j Intro
  24. 24. 9
  25. 25. 10
  26. 26. Java N D _I N TE E ME PL IM10
  27. 27. Java N D _I N TE E ME PL IM Server RUN S_A SRuby JS S _APython Java NS .net RUSpring embedded 10
  28. 28. Java N D _I N TE E ME PL IM Server RUN S_A SRuby RU JS S _A NSPython Java _O NS .net RU NSpring embedded Heroku 10
  29. 29. Java N D _I N TE E ME PL IM PROVIDES ACID Server RUN S_A TX SRuby RU JS S _A NSPython Java _O NS .net RU NSpring embedded Heroku 10
  30. 30. Java Lucene N D _I TE Index N ME ES E T PL RA G IM TE IN PROVIDES ACID Server RUN S_A TX SRuby RU JS S _A NSPython Java _O NS .net RU NSpring embedded Heroku 10
  31. 31. Java Lucene N D _I TE Index N ME ES E T PL RA G IM TE IN PROVIDES ACID Server RUN S_A TX S ORuby RU ES_T JS S _A NSPython Java _O NS SC AL .net RU NSpring 34bn embedded Heroku Nodes 10
  32. 32. Java Lucene 1 M/s N D _I TE Index N ALS ME ES VERS E T PL RA G IM TE TRA IN PROVIDES ACID Server RUN S_A TX S ORuby RU ES_T JS S _A NSPython Java _O NS SC AL .net RU NSpring 34bn embedded Heroku Nodes 10
  33. 33. Java Lucene 1 M/s Master/ N D _I Slave TE Index N ALS ME HIG H_A ES VERS E VA T PL IL. RA G IM TE TRA IN PROVIDES ACID Server RUN S_A TX S ORuby RU ES_T JS S _A NSPython Java _O NS SC AL .net RU NSpring 34bn embedded Heroku Nodes 10
  34. 34. Java Lucene 1 M/s Master/ N D _I Slave TE Index N ALS ME HIG H_A ES VERS E VA T PL IL. RA G IM TE TRA IN PROVIDES ACID Server RUN S_A LI CE TX S NS ED _L IK ORuby RU E ES_T JS MySQL S _A NSPython Java _O NS SC AL .net RU NSpring Mongo 34bn embedded Heroku Nodes 10
  35. 35. IntroducingThe Twitter-Graph Domain Model
  36. 36. Whiteboard it - abstract :Tweet :Tweet
  37. 37. Whiteboard it - abstract :User PO ST ED :Tweet :User :Tweet POSTED
  38. 38. Whiteboard it - abstract :User PO ST ED TAGGED :Tag :Tweet TAGG ED :Tag ED G AG :User :Tweet T POSTED
  39. 39. Whiteboard it - abstract :User PO ST ED TAGGED :Tag :Tweet TAGG ED :Tag NS O ED TI G MEN AG :User :Tweet T POSTED
  40. 40. Whiteboard friendly - example Attending the #SDN #Neo4j webinar by @mesirii Looking forward to my #SDN webinar
  41. 41. Whiteboard friendly - example @peterneubauer PO ST ED Attending the #SDN #Neo4j webinar by @mesirii Looking forward@mesirii to my #SDN webinar POSTED
  42. 42. Whiteboard friendly - example @peterneubauer PO ST ED TAGGED #Neo4j Attending the #SDN #Neo4j TAGG webinar by ED @mesirii #SDN ED G Looking forward TAG@mesirii to my #SDN webinar POSTED
  43. 43. Whiteboard friendly - example @peterneubauer PO ST ED TAGGED #Neo4j Attending the #SDN #Neo4j TAGG webinar by ED @mesirii #SDN NS O ED TI G MEN Looking forward TAG@mesirii to my #SDN webinar POSTED
  44. 44. Cypher Query Language๏ Declarative query language • Describe what you want, not how • Based on pattern matching๏ Examples: START peter=node:User(userName=”peterneubauer”) # index lookup MATCH peter-[:POSTED]->tweet-[:MENTIONS]->user WHERE user.country = ”SE” and not peter-[:FOLLOWS]-user RETURN user START tag=node:Tag(tag = ”Neo4j”) MATCH tag<-[:TAGGED]-tweet-[:TAGGED]->co_tag RETURN co_tag.tag, COUNT(*) as cnt ORDER cnt DESC LIMIT 10 14
  45. 45. Spring Data Neo4j 2.1
  46. 46. Spring Data๏ Spring Data: VMWare/SpringSource initiative to give Spring developers easy access to the emerging world of NOSQL๏ Build on top of common infrastructure๏ Spring Data Neo4j is the integration library for Neo4j๏ http://springsource.org/spring-data๏ Support for other NOSQL approaches • Redis • MongoDB • Hadoop • GemFire
  47. 47. Spring Data Neo4j ๏ The brain child of Rod Johnson & Emil Eifrém • Wrote functional initial prototype • Developed by Neo Technology and SpringSource teams ๏ Uses annotations to define graph entities ๏ Entity state backed by graph database ๏ Two modes of Object Graph Mapping • simple POJO Graph Mapping • advanced, seamless AspectJ backed Mapping ๏ SD-Repositories support ๏ Neo4j Server Support
  48. 48. Classic Neo4j domain classpublic class Tag { private final Node underlyingNode; Tag( final Node node ) { underlyingNode = node; } public Node getUnderlyingNode() { return underlyingNode; } public final String getTag() { return (String) underlyingNode.getProperty( “tag” ); } public void setTag( final String tag ) { underlyingNode.setProperty( “tag”, tag ); }}
  49. 49. Spring Data Neo4j domain class@NodeEntitypublic class Tag { @GraphId private Long id; private String tag;}
  50. 50. Spring Data Neo4j domain class@NodeEntitypublic class Tag { @GraphId private Long id; @Indexed(unique = true) private String tag;}
  51. 51. Defining entity classes
  52. 52. Defining entity classes• @NodeEntity • Represents a node in the graph • Fields saved as properties on node • References stored as relationships between nodes • Instantiated using Java ‘new’ keyword, like any POJO • Also returned by lookup mechanisms • Type information stored in the graph
  53. 53. Defining entity classes
  54. 54. Defining entity classes• @RelationshipEntity • Represents a relationship in the graph • Fields saved as properties on relationship • Special fields for start- and end-nodes • Only returned by lookup methods
  55. 55. Tweet domain class@NodeEntitypublic class Tweet { @GraphId private Long id; @Indexed(unique=true) Long tweetId; String text; @Fetch User sender; // eager loading @RelatedTo(type="TAGGED") Collection<Tag> tags; @RelatedTo(type="MENTIONS") Set<User> mentions; @RelatedTo(type="SOURCE") Tweet source;}
  56. 56. Compare to Core-API approach@NodeEntityclass Tweet { ... public Collecti on<Tag> getTags final List<Tag> () { tags = new Link for ( Relations edList<Tag>(); hip rel : under RelTypes.TAGGED lyingNode.getRe , Direction.OUT lationships( movies.add( new GOING ) ) { Tag( rel.getEnd Old } Node() ) ); return tags; } @RelatedTo(type="TAGGED") gs; New private Collection<Tag> ta
  57. 57. Interface based Repositories๏ based on Repository infrastructure in Spring Data Commons๏ just define the interface and the namespace configuration๏ provide out-of-the-box support for • CRUD-Operations • Index-Lookups • Traversal-Execution • Annotated Graph-Queries (Cypher, Gremlin) • Derived Queries • Spatial Queries๏ extensible via custom methods with provided implementations 24
  58. 58. Repositoriesinterface TweetRepository extends GraphRepository<Tweet> { Tweet findByTweetId(String id); Collection<Tweet> findByTagsTag(String tag);}<neo4j:repositories base-package=“org.neo4j.twitter_graph.repositories„/>@Controllerpublic class TwitterController { @Autowired TweetRepository tweetRepository; @RequestMapping(value = "/tweet/{id}",...) public String show(Model model, @PathVariable String id) { Tweet tweet = tweetRepository.findByTweetId(id); model.addAttribute("tweet", tweet); return "/tweet/show"; }} 25
  59. 59. Neo4j-Template (I)๏ well known Spring Template Pattern๏ Resource / Transaction Management๏ Convenience Methods๏ Nodes and Entities handling & conversion๏ Fluent Query Result Handling๏ Works also via REST with Neo4j-Server๏ Exception Translation 26
  60. 60. REST-Client-Support <bean id="restGraphDatabaseService" class="org.sf.data.neo4j.rest.SpringRestGraphDatabase"> <constructor-arg value="http://localhost:7473/db/data" /> </bean> <neo4j:config graphDatabaseService="restGraphDatabaseService"/> ๏ drop-in replacement for the embedded GraphDatabase ๏ works transparently with POJO-Entity-Mapping and Neo4j-Template 27
  61. 61. Coding the Twitter-Graph 28
  62. 62. Todays Coding Exercise๏ uses Spring Social (minimally)๏ Simple Domain: Tweets, Users, Tags • connected Entities • Repositories, Service๏ standalone App with • embedded Neo4j-Database • (local Neo4j-Server) 29
  63. 63. Spring Data Neo4j Guidebook “Good Relationships”“I’m excited about Spring Data Neo4j.... Spring Data Neo4j makes workingwith Neo4j amazingly easy, and therefore has the potential to make youmore successful as a developer.” Rod Johnson, founder of Spring๏ Spring Data Neo4j comes with a great Guide Book, featuring: • Forewords by Rod Johnson and Emil Eifrem • Ancineasts.net narrative tutorial walkthrough for easy to read, • A comprehensive reference for all the details • Check it out: http://bit.ly/sdn-book • Free e-book and printed 30
  64. 64. http://cineasts.net Check Out: http://spring.neo4j.org/tutorial
  65. 65. O‘Reilly Spring Data Book „Modern Data Access for Enterprise Java“ • upcoming book by the Spring Data project leads • introduction to Spring Data & SD-repositories • covers all subprojects • available in O‘Reilly OFPS and O‘Reilly store (e-book) • http://bit.ly/sd-ofps 32
  66. 66. y wh ere ver ect e E onn s Ar aph .Share. 2 Gr ate C v no , 201 In ber 5-6 CT NEisco No vem HCON ncGR AP a n FrRegency Sa tt Hya Presented by 33
  67. 67. Continue here ๏ See the Spring Data Neo4j site for more info: http://spring.neo4j.org ๏ Again, don’t miss our guidebook on Spring Data Neo4j published by InfoQ also printed http://bit.ly/sdn-book ๏ All about Neo4j: ๏ local Neo4j meetup groups http://neo4j.org http://neo4j.meetup.com ๏ Neo4j & SDN videos and webinars: http://video.neo4j.org ๏ Spring Data Forum at http://spring.neo4j.org/discussions ๏ Neo4j Google Group • http://neo4j.org/forums
  68. 68. Thank You!Feel free to reach out with questions on•Twitter,•Spring-Forums,•Neo4j-Google-Group•Stack-Overflow 35

×