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

65
Object Graph Mapping Spring Data Neo4j 3.0 Nicki Wa( @ opencredo Michael Hunger @ neotechnology

description

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.

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

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

Object  Graph  Mapping  Spring  Data  Neo4j  3.0  

Nicki  Wa(  @  opencredo    Michael  Hunger  @  neotechnology  

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

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  

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

1.  Why    Object-­‐Graph-­‐Mapping?  

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

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  

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

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  

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

2.  What  is    Spring  Data  Neo4j?  

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

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  

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

With  and  without  SDN  ...  

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

With  and  without  SDN  ...  

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

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)  

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

Example  Domain:    “Conference  Management”  

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

Sample  graph  

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

High  Level  Object  Model  

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

DePining  Your  Entities  

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

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;  //  .  .  .  }  

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

SDN  Domain  Entities  

@NodeEntity  @TypeAlias(value  =  "Person")  public  class  Person  {        @GraphId  Long  graphId;        @Indexed(unique  =  true)  String  name;        @RelatedToVia        Iterable<Attended>  talksAttended;        //    .  .  .  }  

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

SDN  Domain  Entities  (Relationship)  

@RelationshipEntity(type  =  "LISTENED_TO")  public  class  Attended  {        @GraphId  Long  graphId;          @StartNode  Person  attendee;        @EndNode  Talk  talk;          Integer  rating;          //  .  .  .  }  

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

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;        //  .  .  .  }  

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

SDN  Domain  Entities  

@TypeAlias(value  =  "Speaker")  public  class  Speaker  extends  Person  {        String  bio;        @RelatedTo(type="SPEAKER",  direction  =  INCOMING)        Iterable<Talk>  talks;        //  .  .  .  }  

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

SDN  Repository  Support  -­‐  Derived  Finder  Methods  

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

SDN  Repository  Support  

import  org....data.neo4j.repository.GraphRepository;  import  java.util.Set;  public  interface  TalkRepository  extends                                                                    GraphRepository<Talk>  {          Set<Talk>  findTalksBySpeakersName(String  name);  }  

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

@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  

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

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;        //    .  .  .  }  

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

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`  

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

SDN  Repository  Support  -­‐  Dynamic  Queries  

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

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);            //  .  .  .  }  

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

3.  Current    State  Of  Affairs  

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

Current  State  Of  Affairs  -­‐  Current  ProducVon  Version:  2.3.2.RELEASE  -­‐  Neo4j  1.9.3  support  

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

4.  SDN  3.0  

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

Disclaimer  !!  

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

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  

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

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  

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

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)  

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

Label  Type  Representation  Strategy  

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

@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    {    //  .  .  .  }  

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

@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;        //    .  .  .  }  

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

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);  }    

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

-­‐  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  

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

5.  The  Future  

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

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  

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

Future  Plans  -­‐  OGM  

•  PotenVally  extract  or  use  standalone,  cypher-­‐based  Java-­‐Neo4j-­‐OGM  

•  Also  useable  without  Spring  (Data)  

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

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  

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

Future  Plans  -­‐  Support  

•  Be(er  support  for  •  Spring  Boot  •  Spring  Data  Rest  •  Remote  transacVonal  integraVon  •  JDBC-­‐Driver  in  Spring  

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

Future  Plans  -­‐  Migration  •  Migrate  Type  RepresentaVon  

•  Index-­‐based  -­‐>  Label  based  •  Refactor/Rename  EnVVes,  Fields,  References  •  Rename  Indexes  •  @TypeAlias  

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

6.  Some  Use  Cases  

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

WhyOwnIt:  Ownership  Sharing  Service  

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

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  

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

WhyOwnIt:  Ownership  Sharing  Service  

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

WhyOwnIt:  Ownership  Sharing  Service  

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

WhyOwnIt:  Ownership  Sharing  Service  

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

Music  Sharing/Discovery/Recommendation  Service  

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

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  •  de-­‐duplicate  data  

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

Music  Sharing/Discovery/Recommendation  Service  

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

Music  Sharing/Discovery/Recommendation  Service  

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

Other  Use-­‐Cases  

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

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  

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

•  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  

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

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  

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

Does  it  love  me  or  does  it  not?  

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

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  ...  

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

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  ...  

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

Want  More  Info  ?  

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

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

Resources  

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

Thanks  ☺  Questions  ?  

@techiewa(    @mesirii