Object	
  Graph	
  Mapping	
  
Spring	
  Data	
  Neo4j	
  3.0	
  
Nicki	
  Wa(	
  @	
  opencredo	
  	
  
Michael	
  Hunger...
Agenda	
  
1.	
  Why	
  Object-­‐Graph-­‐Mapping?	
  	
  
2.	
  What	
  is	
  Spring	
  Data	
  (Neo4j)?	
  	
  
3.	
  Cur...
1.	
  Why	
  	
  
Object-­‐Graph-­‐Mapping?	
  
Why	
  OGM	
  ?	
  
-­‐	
  Convenience	
  	
  
-­‐	
  Simplify	
  interac2on	
  with	
  underlying	
  data	
  
source:	
  ...
Why	
  OGM	
  ?	
  
But	
  …	
  
-­‐  IndirecVon,	
  addiVonal	
  abstracVon	
  layer	
  adds	
  
performance,	
  overhead...
2.	
  What	
  is	
  	
  
Spring	
  Data	
  Neo4j?	
  
Spring	
  Data	
  Neo4j	
  
-­‐	
  OGM	
  aimed	
  at	
  Java	
  world	
  &	
  uses	
  Spring	
  
-­‐	
  Neo4j	
  Implemen...
With	
  and	
  without	
  SDN	
  ...	
  
With	
  and	
  without	
  SDN	
  ...	
  
Spring	
  Data	
  Neo4j	
  -­‐	
  features	
  
-­‐	
  Two	
  mapping	
  modes:	
  Simple	
  &	
  Advanced	
  
-­‐	
  Provi...
Example	
  Domain:	
  	
  
“Conference	
  Management”	
  
Sample	
  graph	
  
High	
  Level	
  Object	
  Model	
  
DePining	
  Your	
  Entities	
  
SDN	
  Domain	
  Entities	
  
@NodeEntity	
  
@TypeAlias(value	
  =	
  "Conference")	
  
public	
  class	
  Conference	
  ...
SDN	
  Domain	
  Entities	
  
@NodeEntity	
  
@TypeAlias(value	
  =	
  "Person")	
  
public	
  class	
  Person	
  {	
  
	
...
SDN	
  Domain	
  Entities	
  (Relationship)	
  
@RelationshipEntity(type	
  =	
  "LISTENED_TO")	
  
public	
  class	
  Att...
SDN	
  Domain	
  Entities	
  
@NodeEntity	
  
@TypeAlias(value	
  =	
  "Talk")	
  
public	
  class	
  Talk	
  {	
  
	
  	
...
SDN	
  Domain	
  Entities	
  

@TypeAlias(value	
  =	
  "Speaker")	
  
public	
  class	
  Speaker	
  extends	
  Person	
  ...
SDN	
  Repository	
  Support	
  -­‐	
  
Derived	
  Finder	
  Methods	
  
SDN	
  Repository	
  Support	
  
import	
  org....data.neo4j.repository.GraphRepository;	
  
import	
  java.util.Set;	
  
...
SDN	
  Repository	
  Support	
  
findTalksBySpeakersName	
  
@NodeEntity	
  
@TypeAlias(value	
  =	
  "Talk")	
  
public	
 ...
SDN	
  Repository	
  Support	
  
findTalksBySpeakersName	
  
@TypeAlias(value	
  =	
  "Speaker")	
  
public	
  class	
  Spe...
SDN	
  Repository	
  Support	
  
import	
  org....data.neo4j.repository.GraphRepository;	
  
import	
  java.util.Set;	
  
...
SDN	
  Repository	
  Support	
  -­‐	
  
Dynamic	
  Queries	
  
SDN	
  Repository	
  Support	
  
import	
  org....data.neo4j.repository.GraphRepository;	
  
import	
  java.util.Set;	
  
...
3.	
  Current	
  	
  
State	
  Of	
  Affairs	
  
Current	
  State	
  Of	
  Affairs	
  
-­‐	
  Current	
  ProducVon	
  Version:	
  2.3.2.RELEASE	
  
-­‐	
  Neo4j	
  1.9.3	
...
4.	
  SDN	
  3.0	
  
Disclaimer	
  !!	
  
Disclaimer	
  
SDN	
  3.0	
  s)ll	
  in	
  progress	
  ...	
  
•  Subject	
  to	
  change	
  
•  Primary	
  changes	
  are...
SDN	
  3.0	
  
-­‐	
  Base	
  Neo4j	
  2.0	
  support	
  
-­‐	
  Spring	
  3.2.5	
  (eventually	
  4	
  as	
  well)	
  sup...
Neo4j	
  2.0	
  Support	
  	
  
-­‐	
  Will	
  be	
  able	
  to	
  run	
  against	
  a	
  Neo4j	
  2.X	
  DB	
  
-­‐	
  Qu...
Label	
  Type	
  Representation	
  Strategy	
  
@NodeEntity	
  
@TypeAlias(value	
  =	
  "Person")	
  
public	
  class	
  Person	
  {	
  
	
  @Query("MATCH	
  (person)<-­...
@NodeEntity	
  
@TypeAlias(value	
  =	
  "Person")	
  
public	
  class	
  Person	
  {	
  
	
  	
  	
  @GraphId	
  Long	
  ...
Index	
  vs	
  Label	
  TRS	
  
public	
  interface	
  ConferenceRepository	
  extends	
  	
  
	
  	
  	
  	
  	
  	
  	
 ...
Label	
  Type	
  Representation	
  Strategy	
  
-­‐	
  Will	
  allow	
  for	
  schema	
  indexes**	
  to	
  be	
  used	
  ...
5.	
  The	
  Future	
  
Future	
  Plans	
  -­‐	
  Cypher	
  based	
  
• 
• 
• 
• 
• 

Use	
  all	
  Cypher	
  for	
  the	
  mapping	
  layer	
  
A...
Future	
  Plans	
  -­‐	
  OGM	
  
•  PotenVally	
  extract	
  or	
  use	
  standalone,	
  

cypher-­‐based	
  Java-­‐Neo4j...
Future	
  Plans	
  -­‐	
  Query	
  Results	
  
•  Project	
  query	
  results	
  into	
  object	
  trees/graphs	
  
•  Use...
Future	
  Plans	
  -­‐	
  Support	
  
•  Be(er	
  support	
  for	
  
• 
• 
• 
• 

Spring	
  Boot	
  
Spring	
  Data	
  Res...
Future	
  Plans	
  -­‐	
  Migration	
  
•  Migrate	
  Type	
  RepresentaVon	
  
•  Index-­‐based	
  -­‐>	
  Label	
  based...
6.	
  Some	
  Use	
  Cases	
  
WhyOwnIt:	
  Ownership	
  Sharing	
  Service	
  
WhyOwnIt:	
  Ownership	
  Sharing	
  Service	
  
•  Sharing	
  Culture	
  	
  
•  You	
  own	
  things	
  you	
  only	
  s...
WhyOwnIt:	
  Ownership	
  Sharing	
  Service	
  
WhyOwnIt:	
  Ownership	
  Sharing	
  Service	
  
WhyOwnIt:	
  Ownership	
  Sharing	
  Service	
  
Music	
  Sharing/Discovery/Recommendation	
  
Service	
  
Music	
  Sharing/Discovery/Recommendation	
  
Service	
  
•  Recommend	
  new	
  arVsts	
  and	
  songs	
  to	
  your	
  
...
Music	
  Sharing/Discovery/Recommendation	
  
Service	
  
Music	
  Sharing/Discovery/Recommendation	
  
Service	
  
Other	
  Use-­‐Cases	
  
Other	
  Use-­‐Cases	
  
•  Financial	
  Services	
  -­‐	
  Asset	
  /	
  Account	
  Management	
  
•  Bank	
  -­‐	
  Perm...
SAN/NAS	
  Provider	
  -­‐	
  Lifetime	
  device	
  
tracking	
  
•  Networked	
  Storage	
  Boxes	
  
•  many	
  componen...
Game	
  Retailer	
  
•  Buy	
  and	
  Download	
  &	
  Game	
  on	
  Demand	
  
•  Fraud	
  detecVon	
  
•  delayed	
  pay...
Does	
  it	
  love	
  me	
  or	
  does	
  it	
  
not?	
  
To	
  SDN	
  or	
  Not	
  to	
  SDN	
  ...	
  
Domain-­‐Centric	
  
•  Integrate	
  in	
  a	
  Springframework	
  Context	...
To	
  SDN	
  or	
  Not	
  to	
  SDN	
  ...	
  
Data-­‐Centric	
  
•  high	
  volume	
  database	
  interacVons	
  
•  read...
Want	
  More	
  Info	
  ?	
  
Resources	
  
• 
• 
• 
• 
• 

Spring	
  Data	
  Book	
  	
  
Spring	
  Data	
  Neo4j	
  Guidebook	
  
Spring	
  Data	
  Ne...
Thanks	
  ☺	
  
Questions	
  ?	
  
@techiewa(	
  	
  
@mesirii	
  
Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @ GraphConnect London 2013
Upcoming SlideShare
Loading in …5
×

Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @ GraphConnect London 2013

5,378 views

Published on

Nicki and Michael have recently been working together on the project to develop/upgrade the Spring Data Neo4j 3 (SDN) library to take advantage of some of the latest Neo4j 2.0 features. This talk takes a look at what can be expected of the new framework, and how it can be used to help model various different use cases with a simple Java domain model backed by a Neo4j database.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,378
On SlideShare
0
From Embeds
0
Number of Embeds
46
Actions
Shares
0
Downloads
95
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @ GraphConnect London 2013

  1. 1. Object  Graph  Mapping   Spring  Data  Neo4j  3.0   Nicki  Wa(  @  opencredo     Michael  Hunger  @  neotechnology  
  2. 2. Agenda   1.  Why  Object-­‐Graph-­‐Mapping?     2.  What  is  Spring  Data  (Neo4j)?     3.  Current  State  of  affairs   4.  SDN  3.0     5.  The  Future   6.  Some  Spring  Data  Neoj  Use-­‐Cases  
  3. 3. 1.  Why     Object-­‐Graph-­‐Mapping?  
  4. 4. Why  OGM  ?   -­‐  Convenience     -­‐  Simplify  interac2on  with  underlying  data   source:  Framework/Library  handles  basic  CRUD  type   funcVonality  for  you   -­‐  Provides  ability  to  work  with  na2ve  domain   language  constructs:  eg  domain  classes/objects   (POJOs  in  Java)  in  your  applicaVon     -­‐  Use  Cases:  examples  at  end  of  talk  
  5. 5. Why  OGM  ?   But  …   -­‐  IndirecVon,  addiVonal  abstracVon  layer  adds   performance,  overhead   -­‐  Encourages  users  to  pull  larger  parts  of  the   database  into  memory  to  process  instead  of   le_ng  the  db  do  its  job  
  6. 6. 2.  What  is     Spring  Data  Neo4j?  
  7. 7. Spring  Data  Neo4j   -­‐  OGM  aimed  at  Java  world  &  uses  Spring   -­‐  Neo4j  Implementa2on  of  the  Spring  Data   project:  SpringSource/VMWare/Pivotal  iniVaVve  to  give   Spring  developers  easy  access  to  the  emerging  world  of   NOSQL.  Was  the  iniVal  and  first  Spring  Data  project  by  Rod   and  Emil   -­‐  Simple  programming  model:  annotaVon-­‐based   programming  for  applicaVons  with  rich  domains  
  8. 8. With  and  without  SDN  ...  
  9. 9. With  and  without  SDN  ...  
  10. 10. Spring  Data  Neo4j  -­‐  features   -­‐  Two  mapping  modes:  Simple  &  Advanced   -­‐  Provides  Spring  Data  Repository  Support   -­‐  Ability  to  drop  down  to  Neo4j  Core  API  if   required   -­‐  (Neo4j  Server  Support)  
  11. 11. Example  Domain:     “Conference  Management”  
  12. 12. Sample  graph  
  13. 13. High  Level  Object  Model  
  14. 14. DePining  Your  Entities  
  15. 15. SDN  Domain  Entities   @NodeEntity   @TypeAlias(value  =  "Conference")   public  class  Conference    {        @GraphId  Long  graphId;        @Indexed  String  name;        Date  date;          @RelatedTo(type="ATTENDEE")        Set<Person>  attendees;        @RelatedTo(type="TALK")        Set<Talk>  talks;   //  .  .  .   }  
  16. 16. SDN  Domain  Entities   @NodeEntity   @TypeAlias(value  =  "Person")   public  class  Person  {        @GraphId  Long  graphId;        @Indexed(unique  =  true)  String  name;        @RelatedToVia        Iterable<Attended>  talksAttended;        //    .  .  .   }  
  17. 17. SDN  Domain  Entities  (Relationship)   @RelationshipEntity(type  =  "LISTENED_TO")   public  class  Attended  {        @GraphId  Long  graphId;          @StartNode  Person  attendee;        @EndNode  Talk  talk;          Integer  rating;          //  .  .  .   }  
  18. 18. SDN  Domain  Entities   @NodeEntity   @TypeAlias(value  =  "Talk")   public  class  Talk  {        @GraphId  Long  graphId;        @Indexed  String  name;        String  description;          @RelatedTo(type="SPEAKER")        Set<Speaker>  speakers;        @RelatedToVia(direction  =  INCOMING)        Set<Attended>  attendees;        //  .  .  .   }  
  19. 19. SDN  Domain  Entities   @TypeAlias(value  =  "Speaker")   public  class  Speaker  extends  Person  {        String  bio;        @RelatedTo(type="SPEAKER",  direction  =  INCOMING)        Iterable<Talk>  talks;        //  .  .  .   }  
  20. 20. SDN  Repository  Support  -­‐   Derived  Finder  Methods  
  21. 21. SDN  Repository  Support   import  org....data.neo4j.repository.GraphRepository;   import  java.util.Set;   public  interface  TalkRepository  extends                                                                    GraphRepository<Talk>  {          Set<Talk>  findTalksBySpeakersName(String  name);   }  
  22. 22. SDN  Repository  Support   findTalksBySpeakersName   @NodeEntity   @TypeAlias(value  =  "Talk")   public  class  Talk  {        @GraphId  Long  graphId;        @Indexed  String  name;        String  description;          @RelatedTo(type="SPEAKER")        Set<Speaker>  speakers;        @RelatedToVia(direction  =  INCOMING)        Set<Attended>  attendees;        //  .  .  .   }  
  23. 23. SDN  Repository  Support   findTalksBySpeakersName   @TypeAlias(value  =  "Speaker")   public  class  Speaker  extends  Person  {          String  bio;        @RelatedTo(type="SPEAKER",  direction  =  INCOMING)        Iterable<Talk>  talks;   @NodeEntity        //  .  .  .   @TypeAlias(value  =  "Person")   }   public  class  Person  {        @GraphId  Long  graphId;        @Indexed(unique  =  true)  String  name;        @RelatedToVia(type="ATTENDED")        Iterable<Attended>  talksAttended;        //    .  .  .   }  
  24. 24. SDN  Repository  Support   import  org....data.neo4j.repository.GraphRepository;   import  java.util.Set;   public  interface  TalkRepository  extends                                                                    GraphRepository<Talk>  {          Set<Talk>  findTalksBySpeakersName(String  name);   }    START  `talk_speakers`=node:`Person`(`name`={0})    MATCH  (`talk`)-­‐[:`SPEAKER`]-­‐>(`talk_speakers`)    RETURN  `talk`  
  25. 25. SDN  Repository  Support  -­‐   Dynamic  Queries  
  26. 26. SDN  Repository  Support   import  org....data.neo4j.repository.GraphRepository;   import  java.util.Set;   public  interface  TalkRepository  extends                                                                    GraphRepository<Talk>  {        //  Repository  Query  Method        @Query(value  =          "MATCH  (talk:Talk)-­‐[:SPEAKER]-­‐>(speaker:Speaker)  "  +          "WHERE  speaker.name={0}  RETURN  talk")        Set<Talk>  findTalksBySpeakersNameQuery(String  name);            //  .  .  .   }  
  27. 27. 3.  Current     State  Of  Affairs  
  28. 28. Current  State  Of  Affairs   -­‐  Current  ProducVon  Version:  2.3.2.RELEASE   -­‐  Neo4j  1.9.3  support  
  29. 29. 4.  SDN  3.0  
  30. 30. Disclaimer  !!  
  31. 31. Disclaimer   SDN  3.0  s)ll  in  progress  ...   •  Subject  to  change   •  Primary  changes  are  Neo4j  2.0  compliance  -­‐   not  finalised  yet   •  follow  progress  on  githubh"p:// spring.neo4j.org/source  
  32. 32. SDN  3.0   -­‐  Base  Neo4j  2.0  support   -­‐  Spring  3.2.5  (eventually  4  as  well)  support   -­‐  New  Type  Representa2on  Strategy  -­‐  Labels     -­‐  Migra2on  towards  more  Cypher  driven   founda2on   -­‐  Various  bug  fixes  and  enhancements  
  33. 33. Neo4j  2.0  Support     -­‐  Will  be  able  to  run  against  a  Neo4j  2.X  DB   -­‐  Query  using  Labels   -­‐  Ability  to  use  new  Cypher  Syntax   -­‐  Schema  Indexes  (Work  in  Progress)  
  34. 34. Label  Type  Representation  Strategy  
  35. 35. @NodeEntity   @TypeAlias(value  =  "Person")   public  class  Person  {    @Query("MATCH  (person)<-­‐[:ATTENDEE]-­‐(gathering:Conference)"  +                  "  WHERE    id(person)  =  {self}  "  +                  "  RETURN  gathering")    public  Iterable<Conference>  findAllConferencesAttended;    //    .  .  .   }       @NodeEntity   @TypeAlias(value  =  "Conference")   public  class  Conference    {    //  .  .  .   }  
  36. 36. @NodeEntity   @TypeAlias(value  =  "Person")   public  class  Person  {        @GraphId  Long  graphId;        @Indexed(unique  =  true)  String  name;        @RelatedToVia(type="ATTENDED")        Iterable<Attended>  talksAttended;        //    .  .  .   }   @TypeAlias(value  =  "Speaker")   public  class  Speaker  extends  Person  {          String  bio;        @RelatedTo(type="SPEAKER",  direction  =  INCOMING)        Iterable<Talk>  talks;        //  .  .  .   }  
  37. 37. Index  vs  Label  TRS   public  interface  ConferenceRepository  extends                                                                    GraphRepository<Conference>  {    /*              Query  generated  when  using  Index  Based  TRS                          START  `conference`=node:__types__(className="Conference")                          WHERE  `conference`.`date`  =  {0}                          RETURN  `conference`                Query  generated  when  using  Label  Based  TRS                          MATCH  (`conference`:`Conference`)                          WHERE  `conference`.`date`  =  {0}                          RETURN  `conference`      */        Set<Conference>  findAllByDate(Date  date);   }    
  38. 38. Label  Type  Representation  Strategy   -­‐  Will  allow  for  schema  indexes**  to  be  used   -­‐  Will  add  addiVonal  __TYPE__XX  labels  to  nodes   -­‐  No  addiVonal  properVes  need  to  be  stored  on   nodes  to  represent  hierarchy  (If  Index  Based  TRS  used)   -­‐  No  addiVonal  nodes  need  to  be  created  to   represent  hierarchy  (If  Sub  Reference  based  TRS  used)  
  39. 39. 5.  The  Future  
  40. 40. Future  Plans  -­‐  Cypher  based   •  •  •  •  •  Use  all  Cypher  for  the  mapping  layer   Align  embedded  and  remote  use   Benefit  from  Cypher  enhancements   Leverage  complex  query  mechanisms   Support  more  OGM  type  funcVonality   •  Client  side  caching   •  Lazy  loading  
  41. 41. Future  Plans  -­‐  OGM   •  PotenVally  extract  or  use  standalone,   cypher-­‐based  Java-­‐Neo4j-­‐OGM   •  Also  useable  without  Spring  (Data)  
  42. 42. Future  Plans  -­‐  Query  Results   •  Project  query  results  into  object  trees/graphs   •  Useful  for  direct  view-­‐layer  integraVon   •  Less  overhead  by  an  intermediate  domain   model  that  has  to  be  converted  
  43. 43. Future  Plans  -­‐  Support   •  Be(er  support  for   •  •  •  •  Spring  Boot   Spring  Data  Rest   Remote  transacVonal  integraVon   JDBC-­‐Driver  in  Spring  
  44. 44. Future  Plans  -­‐  Migration   •  Migrate  Type  RepresentaVon   •  Index-­‐based  -­‐>  Label  based   •  Refactor/Rename  EnVVes,  Fields,  References   •  Rename  Indexes   •  @TypeAlias  
  45. 45. 6.  Some  Use  Cases  
  46. 46. WhyOwnIt:  Ownership  Sharing  Service  
  47. 47. WhyOwnIt:  Ownership  Sharing  Service   •  Sharing  Culture     •  You  own  things  you  only  seldomly  need   •  You  need  things  you  don‘t  own   •  Match  needs  and  available  devices/tools   •  add  raVngs,  trust,  recommendaVons   •  locaVon  based,  close  to  you  for  pick-­‐up   •  Startup  
  48. 48. WhyOwnIt:  Ownership  Sharing  Service  
  49. 49. WhyOwnIt:  Ownership  Sharing  Service  
  50. 50. WhyOwnIt:  Ownership  Sharing  Service  
  51. 51. Music  Sharing/Discovery/Recommendation   Service  
  52. 52. Music  Sharing/Discovery/Recommendation   Service   •  Recommend  new  arVsts  and  songs  to  your   users   •  take  their  previous  listen-­‐history  into   account   •  look  at  what‘s  hot  amongst  their  friends   •  infer  broader  selecVon  via  bands  and  genres   •   Detect  duplicates  using  media-­‐fingerprints   •  copyright  infringements  
  53. 53. Music  Sharing/Discovery/Recommendation   Service  
  54. 54. Music  Sharing/Discovery/Recommendation   Service  
  55. 55. Other  Use-­‐Cases  
  56. 56. Other  Use-­‐Cases   •  Financial  Services  -­‐  Asset  /  Account  Management   •  Bank  -­‐  Permission  Management  and  AuthorizaVon   •  EnVtlement   •  Data  Sopware  Provider  -­‐  Impact  analysis  of   changes   •  ImplicaVon  of  changes  on  the  company  core  asset:   DATA   •  Change  tracking   •  Root  Cause  Analysis  
  57. 57. SAN/NAS  Provider  -­‐  Lifetime  device   tracking   •  Networked  Storage  Boxes   •  many  components,  disks,  controllers,  power  supply,   sensors   •  Tracking  of  measurement  data   •  Impact  analyVcs   •  Early  detecVon  of  anomalies   •  Intelligent  fail  over   •  Huge  number  of  domain  enVVes  (~450)   •  generaVng  domain  classes  and  repositories  
  58. 58. Game  Retailer   •  Buy  and  Download  &  Game  on  Demand   •  Fraud  detecVon   •  delayed  payments,  charge  backs,  fradulent  keys  are   invalid   •  Convert  rules  to  traversals   •  co-­‐occurrance  of  sudden  large  sales   •  fradulent  e-­‐mail  pa(erns   •  CombinaVon  with  Oracle  Infrastructure   •  Cross-­‐Store  
  59. 59. Does  it  love  me  or  does  it   not?  
  60. 60. To  SDN  or  Not  to  SDN  ...   Domain-­‐Centric   •  Integrate  in  a  Springframework  Context   •  ExisVng  ApplicaVons  with  POJO  Domains   •  Move  a  manageable  amount  of  data  in  and   out  of  the  database  into  Domain  Objects   •  Rich,  connected  domain   •  MulVple  projecVons  
  61. 61. To  SDN  or  Not  to  SDN  ...   Data-­‐Centric   •  high  volume  database  interacVons   •  reads  and  writes  (inserts)   •  thin  domain  view  on  top  of  graph  queries   •  remote  Client  to  Neo4j  Server  
  62. 62. Want  More  Info  ?  
  63. 63. Resources   •  •  •  •  •  Spring  Data  Book     Spring  Data  Neo4j  Guidebook   Spring  Data  Neo4j  on  Spring.io   Chapter  in  Neo4j  in  AcVon   Cineasts.net  Tutorial  
  64. 64. Thanks  ☺   Questions  ?   @techiewa(     @mesirii  

×