Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Object Graph Mapping with Spring Data Neo4j 3

Avatar for Nicki Watt Nicki Watt
November 20, 2013

Object Graph Mapping with Spring Data Neo4j 3

Slides accompanying a talk done by myself and Michael Hunger at Graph Connect 2013 http://graphconnect.com/gc2013 with video available at https://vimeo.com/80618455

Avatar for Nicki Watt

Nicki Watt

November 20, 2013
Tweet

More Decks by Nicki Watt

Other Decks in Technology

Transcript

  1. Object  Graph  Mapping   Spring  Data  Neo4j  3.0   Nicki

     Wa(  @  opencredo     Michael  Hunger  @  neotechnology  
  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. 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  
  4. 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  
  5. 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  
  6. 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)  
  7. 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;   //  .  .  .   }  
  8. SDN  Domain  Entities   @NodeEntity   @TypeAlias(value  =  "Person")  

    public  class  Person  {        @GraphId  Long  graphId;        @Indexed(unique  =  true)  String  name;        @RelatedToVia        Iterable<Attended>  talksAttended;        //    .  .  .   }  
  9. SDN  Domain  Entities  (Relationship)   @RelationshipEntity(type  =  "LISTENED_TO")   public

     class  Attended  {        @GraphId  Long  graphId;          @StartNode  Person  attendee;        @EndNode  Talk  talk;          Integer  rating;          //  .  .  .   }  
  10. 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;        //  .  .  .   }  
  11. SDN  Domain  Entities   @TypeAlias(value  =  "Speaker")   public  class

     Speaker  extends  Person  {        String  bio;        @RelatedTo(type="SPEAKER",  direction  =  INCOMING)        Iterable<Talk>  talks;        //  .  .  .   }  
  12. SDN  Repository  Support   import  org....data.neo4j.repository.GraphRepository;   import  java.util.Set;  

    public  interface  TalkRepository  extends                                                                    GraphRepository<Talk>  {          Set<Talk>  findTalksBySpeakersName(String  name);   }  
  13. @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;        //  .  .  .   }   SDN  Repository  Support   findTalksBySpeakersName  
  14. 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;        //    .  .  .   }  
  15. 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`  
  16. 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);            //  .  .  .   }  
  17. 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  
  18. 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  
  19. 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)  
  20. @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    {    //  .  .  .   }  
  21. @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;        //    .  .  .   }  
  22. 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);   }    
  23. -­‐  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)   Label  Type  Representation  Strategy  
  24. 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  
  25. Future  Plans  -­‐  OGM   •  PotenVally  extract  or  use

     standalone,   cypher-­‐based  Java-­‐Neo4j-­‐OGM   •  Also  useable  without  Spring  (Data)  
  26. 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  
  27. Future  Plans  -­‐  Support   •  Be(er  support  for  

    •  Spring  Boot   •  Spring  Data  Rest   •  Remote  transacVonal  integraVon   •  JDBC-­‐Driver  in  Spring  
  28. Future  Plans  -­‐  Migration   •  Migrate  Type  RepresentaVon  

    •  Index-­‐based  -­‐>  Label  based   •  Refactor/Rename  EnVVes,  Fields,  References   •  Rename  Indexes   •  @TypeAlias  
  29. 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  
  30. 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  
  31. 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  
  32. •  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   SAN/NAS  Provider  -­‐  Lifetime  device   tracking  
  33. 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  
  34. 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   To  SDN  or  Not  to  SDN  ...  
  35. Data-­‐Centric   •  high  volume  database  interacVons   •  reads

     and  writes  (inserts)   •  thin  domain  view  on  top  of  graph  queries   •  remote  Client  to  Neo4j  Server   To  SDN  or  Not  to  SDN  ...  
  36. •  Spring  Data  Book     •  Spring  Data  Neo4j

     Guidebook   •  Spring  Data  Neo4j  on  Spring.io   •  Chapter  in  Neo4j  in  AcVon   •  Cineasts.net  Tutorial   Resources