75
Introduksjon ,l grafdatabaser, Neo4j og Spring Data Aleksander M. Stensby Monokkel A/S

Introduction to Neo4j and Spring Data

Embed Size (px)

DESCRIPTION

Slides from my talk on Neo4j and Spring Data, given at JavaBin Sørlandet and JavaBin Vestfold. The slides are in Norwegian - I will upload an English version soon.

Citation preview

Page 1: Introduction to Neo4j and Spring Data

Introduksjon  ,l  grafdatabaser,  Neo4j  og  Spring  Data  

 Aleksander  M.  Stensby  

Monokkel  A/S  

Page 2: Introduction to Neo4j and Spring Data

•  Daglig  leder  i  Monokkel  AS  

•  Tidligere  COO  i  Integrasco  AS  

•  Persistering,  Prosessering  og  Presentasjon  av  data  

Persistering  –  Prosessering  –  Presentasjon  

Page 3: Introduction to Neo4j and Spring Data

•  Aleksander  M.  Stensby  

•  Daglig  leder  i  Monokkel  AS  

•  Tidligere  COO  i  Integrasco  AS  

www.monokkel.io  

Page 4: Introduction to Neo4j and Spring Data

Agenda  

•  Intro  @l  grafdatabaser  og  modellering    

•  Neo4j  og  Cypher    

•  SpringData  Neo4j  

Page 5: Introduction to Neo4j and Spring Data

Relasjondatabaser...  

Page 6: Introduction to Neo4j and Spring Data

...på  relasjoner  

Page 7: Introduction to Neo4j and Spring Data

Relasjondatabaser  er  fantas@sk  på  aggregering  av  data,  mapping  av  

skjemaer  og  tabell  data!  

Page 8: Introduction to Neo4j and Spring Data

MEN  

Page 9: Introduction to Neo4j and Spring Data

vi  har  en  tendens  @l  å  tvinge    _alle_    

problems@llinger  inn  i  relasjondatabasene  våre!  

Page 10: Introduction to Neo4j and Spring Data

Join-­‐hell..  Rekursjon...  

Null-­‐sjekking...  

Page 11: Introduction to Neo4j and Spring Data

Polyglot  persistering  

Page 12: Introduction to Neo4j and Spring Data

NoSQL  

Page 13: Introduction to Neo4j and Spring Data

Not  only  SQL  

Page 14: Introduction to Neo4j and Spring Data

NoSQL  

•  Key-­‐value  stores  – Amazon  Dynamo  – Eks:  Voldemort,...  

•  BigTable  kloner  – Google’s  BigTable  – Eks:  Hbase,  ...  

•  Dokumentdatbaser  – Lotus  Notes  – Eks:  MongoDB,  CouchDB  

Page 15: Introduction to Neo4j and Spring Data

NoSQL  

•  Grafdatabaser  – Euler  og  graXeori  – Eks:  Neo4j,  VertexDB,  AllegroGraph,  Giraphe,  OrientDB,  etc...  

Page 16: Introduction to Neo4j and Spring Data

NoSQL  Størrelse  

Kompleksitet  

Grafer  

Dokument  

BigTable  

KV  

Page 17: Introduction to Neo4j and Spring Data

NoSQL  Størrelse  

Kompleksitet  

Grafer  

Dokument  

BigTable  

KV  

90%  Milliarder  av  noder  og  relasjoner  

Page 18: Introduction to Neo4j and Spring Data

dagens  dose  buzzwords  

Page 19: Introduction to Neo4j and Spring Data

Venner  av  venner...  

Page 20: Introduction to Neo4j and Spring Data

Person  ID  Navn  

Venner  PersonID  VennID  

Page 21: Introduction to Neo4j and Spring Data

Person  ID  Navn  

Venner  PersonID  VennID  

«Aleks»  sine  venner  

SELECT  p1.Person  FROM  Person  p1  JOIN  Venner  

 ON  Venner.VennID  =  p1.ID  JOIN  Person  p2  

 ON  Venner.PersonID  =  p2.ID  WHERE  p2.Navn  =  'Aleks'  

Page 22: Introduction to Neo4j and Spring Data

Person  ID  Navn  

Venner  PersonID  VennID  

Venner  med  «Aleks»  

SELECT  p1.Person  FROM  Person  p1  JOIN  Venner  

 ON  Venner.PersonID  =  p1.ID  JOIN  Person  p2  

 ON  Venner.VennID  =  p2.ID  WHERE  p2.Navn  =  'Aleks'  

Page 23: Introduction to Neo4j and Spring Data

Person  ID  Navn  

Venner  PersonID  VennID  

«Aleks»  sine  venners  venner?  

SELECT  p1.Person  AS  PERSON,  p2.Person  AS  VENN_AV_VENN  FROM  Venner  v1  JOIN  Person  p1  

 ON  v1.PersonID  =  p1.ID  JOIN  Venner  v2  

 ON  v2.VennID  =  v1.VennID  JOIN  Person  p2  

 ON  v2.VennID  =  p2.  ID  WHERE  p1.Navn  =  'Aleks‘  AND  v2.VennID  <>  p1.ID  

Page 24: Introduction to Neo4j and Spring Data

Kilde:  Neo4j  in  Ac@on  

1,000,000  personer  

Dybde   RDBMS   Neo4j   Antall  rader  

2   0.016   0.01   ~  2500  3   30.267   0.168   ~  110,000  4   1543.505   1.359   ~  600,000  5   DNF   2.132   ~  800,000  

Page 25: Introduction to Neo4j and Spring Data
Page 26: Introduction to Neo4j and Spring Data

En  grafdatabase...  

•  benyper  grafstrukturer  med  – Noder  – Relasjoner    – Apribuper    

•  @l  å  lagre  informasjon!  

•  Ypperlig  @l  relasjoner  –  men  ikke  best  på  aggregering  av  data  

Page 27: Introduction to Neo4j and Spring Data

Grafdatabaser  og  neo4j  

•  Visuelt  –  Skjemaløst!    

•  Dobbelt-­‐lenkede-­‐lister    – hver  node  har  en  liste  med  innkommende  og  utgående  relasjoner  

•  Direkte  lookup  =  O(1)  

Page 28: Introduction to Neo4j and Spring Data

En  graf  lagrer  data  i  noder  

ALEKS   TARJEI  

Page 29: Introduction to Neo4j and Spring Data

Relasjoner  knyper  noder  sammen  

FRIENDS_WITH  

ALEKS   TARJEI  

Page 30: Introduction to Neo4j and Spring Data

Noder  har  a\ribu\er  

ALEKS   TARJEI  

Age:  29  First  Name:  Aleksander  Last  Name:  Stensby    

Age:  30  First  Name:  Tarjei  Last  Name:  Romtveit    

FRIENDS_WITH  

Page 31: Introduction to Neo4j and Spring Data

Relasjoner  kan  også  ha  a\ribu\er  

ALEKS   TARJEI  

Age:  29  First  Name:  Aleksander  Last  Name:  Stensby    

Age:  30  First  Name:  Tarjei  Last  Name:  Romtveit    

FRIENDS_WITH  Since:  01.01.2004  

Page 32: Introduction to Neo4j and Spring Data

Relasjoner  kan  gå  flere  veier  

ALEKS   TARJEI  

Age:  29  First  Name:  Aleksander  Last  Name:  Stensby    

Age:  30  First  Name:  Tarjei  Last  Name:  Romtveit    

FRIENDS_WITH  Since:  01.01.2004  

FRIENDS_WITH  Since:  01.01.2004  

Page 33: Introduction to Neo4j and Spring Data

ALEKS   TARJEI  

Age:  29  First  Name:  Aleksander  Last  Name:  Stensby    

Age:  30  First  Name:  Tarjei  Last  Name:  Romtveit    

FRIENDS_WITH  Since:  01.01.2004  

Page 34: Introduction to Neo4j and Spring Data

ALEKS   TARJEI  

Age:  29  First  Name:  Aleksander  Last  Name:  Stensby  Type:  Person    

Age:  30  First  Name:  Tarjei  Last  Name:  Romtveit  Type:  Person    

FRIENDS_WITH  Since:  01.01.2004  

forskjellige  typer  av  noder  

Page 35: Introduction to Neo4j and Spring Data

Person   Person  

Sport  

Programmeringspråk  

Drikke  

Person  

Page 36: Introduction to Neo4j and Spring Data

Noen  bruksområder  

•  Sosiale  Medier  •  Recommenda@ons  •  Geo  rou@ng  og  logis@kk  •  Brukerkontroll  /  @lgangshåndtering  •  Network  management  •  Finans  /  Svindel  

Page 37: Introduction to Neo4j and Spring Data

Neo4j  –  [ER_EN]  -­‐>  Property  Graph  

Page 38: Introduction to Neo4j and Spring Data

(node)  –  [relasjon]  -­‐>  (node)  

Page 39: Introduction to Neo4j and Spring Data

(Aleks)  –  [FRIENDS_WITH]  -­‐>  (Tarjei)  

Page 40: Introduction to Neo4j and Spring Data

Cypher  

"Make  the  simple  things  simple,  and  the  complex  things  possible"  

Page 41: Introduction to Neo4j and Spring Data

Intro  @l  Cypher  

•  START  •  MATCH  •  WHERE  •  RETURN  •  CREATE  •  DELETE  •  SET  •  FOREACH  •  WITH  

Page 42: Introduction to Neo4j and Spring Data

MATCH  <papern>    WHERE  RETURN    

Page 43: Introduction to Neo4j and Spring Data

Beskriv  hva  du  ønsker  å  hente  ut  med  PATTERNS  

(a)-­‐[r]-­‐>(b)  

Page 44: Introduction to Neo4j and Spring Data

(Aleks)  –  [FRIENDS_WITH]  -­‐>  (Tarjei)  

Page 45: Introduction to Neo4j and Spring Data

Path  depth  

(a)-­‐[*]-­‐>(b)      

Page 46: Introduction to Neo4j and Spring Data

START  

START  n=node(1)  RETURN  n  

Page 47: Introduction to Neo4j and Spring Data

MATCH  

MATCH  (movie:Movie)  RETURN  movie  

Page 48: Introduction to Neo4j and Spring Data

WHERE  

MATCH  movie  WHERE  move.@tle  =  ‘Blade  Runner'  RETURN  movie  

Page 49: Introduction to Neo4j and Spring Data

START  a=node(*)  MATCH  (a:Person)  WHERE  a.name='Danny  DeVito'  RETURN  a  

Page 50: Introduction to Neo4j and Spring Data

START  a=node(*)  MATCH  (a:Person)  WHERE  a.name='Danny  DeVito'  RETURN  a  

Page 51: Introduction to Neo4j and Spring Data

START  a=node(*)  MATCH  (a:Person  {name:  'Danny  DeVito'}  )  WHERE  a.name='Danny  DeVito'  RETURN  a  

Neo4j  2.0.1  

Page 52: Introduction to Neo4j and Spring Data

Venners  venner...  

MATCH  (aleks)-­‐[:KNOWS*2..2]-­‐>(friend_of_friend)  WHERE  aleks.firstName=  'Aleks'  RETURN  friend_of_friend.firstName  

Page 53: Introduction to Neo4j and Spring Data

Venners  venner...  

MATCH  (aleks)-­‐[:KNOWS*2..2]-­‐>(friend_of_friend)  WHERE  aleks.firstName=  'Aleks'  AND  NOT  (aleks)-­‐[:KNOWS]-­‐>(friend_of_friend)  RETURN  friend_of_friend.firstName  

Page 54: Introduction to Neo4j and Spring Data

hpp://docs.neo4j.org/refcard/2.1.5/  

Page 55: Introduction to Neo4j and Spring Data

         

på  1-­‐2-­‐3    DEMO  

 

Page 56: Introduction to Neo4j and Spring Data

Og  mye  mye  mer...  •  Traversals  navigerer  grafen  •  Traversals  iden@fiserer  paths  •  Paths  ordner  noder  •  Cypher  parametere  •  Indexes  •  Neo4j  embedded,  Neo4j  REST  •  Neo4j  2.x  –  Labels  og  automaaske  indekser  •  Gremlin  (procedural)  –  Cypher  (declara@ve)  •  Enterprise  (mul@-­‐instance  cluster,  online  backup)  •  Neo4j  Spaaal  

Page 57: Introduction to Neo4j and Spring Data

Språkdrivere  

Page 58: Introduction to Neo4j and Spring Data

Contrib  

Page 59: Introduction to Neo4j and Spring Data
Page 60: Introduction to Neo4j and Spring Data

Intro  @l  SpringData  Neo4j  

•  SpringData  •  Repositories  •  Kjapt  eksempel  på  oppsep  

<dependencies>          <dependency>                  <groupId>org.springframework.data</groupId>                  <ar@factId>spring-­‐data-­‐neo4j</ar@factId>                  <version>3.2.0.RELEASE</version>          </dependency>  </dependencies>  

neo4j      2.1.4  spring-­‐data-­‐neo4j    3.2.0  spring      4.1.0  

Page 61: Introduction to Neo4j and Spring Data

SpringData  Neo4j  Config  @Configuration  @EnableNeo4jRepositories("no.stensby.javabin.neo.repositories")  public  class  Config  extends  Neo4jConfiguration  {  }  

@Autowired  protected  Neo4jTemplate  template;  

Page 62: Introduction to Neo4j and Spring Data

SpringData  Neo4j  Config  @Configuration  @EnableNeo4jRepositories("no.stensby.javabin.neo.repositories")  public  class  Config  extends  Neo4jConfiguration  {      public  Config  (){                  setBasePackage("no.stensby.javabin.neo.domain");          }    }  

@Autowired  protected  Neo4jTemplate  template;  

Page 63: Introduction to Neo4j and Spring Data

SpringData  Neo4j  Config  

@Bean  GraphDatabaseService  graphDatabaseService()  {  return  new  GraphDatabaseFactory().newEmbeddedDatabase("tmp/neo4j");  }  

Embedded:  

REST:    @Bean  GraphDatabaseService  graphDatabaseService()  {  return  new  SpringRestGraphDatabase("http://localhost:7474/db/data");  }  

@Bean(destroyMethod  =  "shutdown")  @Scope(SCOPE_PROTOTYPE)  public  GraphDatabaseService  graphDatabaseService()  {  return  new  TestGraphDatabaseFactory().newImpermanentDatabase();  }  

TEST:    

Page 64: Introduction to Neo4j and Spring Data

SpringData  Neo4j  -­‐  Domene  

@NodeEntity  public  class  Person  {  @GraphId  public  Long  nodeId;    @Indexed(unique  =  true)  public  int  id;    public  String  firstName;  public  String  lastName;  }  

Page 65: Introduction to Neo4j and Spring Data

1-­‐1  relasjoner  

@NodeEntity  public  class  Address  {    private  Country  country;    ...  }  

Page 66: Introduction to Neo4j and Spring Data

1-­‐1  relasjoner  

@NodeEntity  public  class  Address  {    @Fetch  private  Country  country;    ...  }  

Page 67: Introduction to Neo4j and Spring Data

1-­‐m  relasjoner  -­‐  @RelatedTo  

@RelatedTo(type  =  "ADDRESS")  private  Set<Address>  addresses  =  new  HashSet<Address>();  

@RelatedTo  private  Set<Person>  venner  =  new  HashSet<Person>  ();  

Page 68: Introduction to Neo4j and Spring Data

m-­‐m  relasjoner  -­‐  @RelatedToVia  

@Fetch  @RelatedToVia  (type  =  "ACTS_IN")  Set<Role>  roles  =  new  HashSet<Role>();  

@Fetch  @RelatedToVia(type  =  "ACTS_IN",  direction  =  Direction.INCOMING)    Set<Role>  cast  =  new  HashSet<Role>();  

Actor  

Movie  

@RelationshipEntity(type  =  "ACTS_IN")  public  class  Role  {  @GraphId  Long  nodeId;  @StartNode  Actor  actor;  @EndNode  Movie  movie;  

Role  

Page 69: Introduction to Neo4j and Spring Data

SpringData  Neo4j  -­‐  Repositories  

public  interface  PersonRepository  extends  GraphRepository<Person>{}  

@Autowired  PersonRepository  repository;  

Page 70: Introduction to Neo4j and Spring Data

SpringData  Neo4j  -­‐  Repositories  

public  interface  PersonRepository  extends  GraphRepository<Person>{      findByFirstName(String  firstName);    }  

Page 71: Introduction to Neo4j and Spring Data

SpringData  Neo4j  -­‐  Repositories  

public  interface  PersonRepository  extends  GraphRepository<Person>{      Person  findByFirstName(String  firstName);    @Query("MATCH  (p:Person{firstName:{0}})  RETURN  p")    Person  getPersonWithFirstName  (String  firstName);    }  

Page 72: Introduction to Neo4j and Spring Data

SpringData  Neo4j  -­‐  Repositories  

public  interface  PersonRepository  extends  GraphRepository<Person>{      Person  findByFirstName(String  firstName);    @Query("MATCH  (p:Person{firstName:{0}})  RETURN  p")    Person  getPersonWithFirstName  (String  firstName);      @Query(  "MATCH  (p:Person{firstName:{0}})-­‐[:knows]-­‐>friends  "  +  

 "  RETURN  friends")    Iterable<Person>  findFriendsOfPerson(String  firstName);      }  

Page 73: Introduction to Neo4j and Spring Data

Lessons  Learned  •  Vær  forsik@g  med  versjoner  og  oppgraderinger…  – Neo  1.9.x  -­‐>  Neo  2.x  kan  by  på  mange  problemer  – Mange  breaking  changes  underveis…  –  Spring  Data  Neo4j  !=  Neo4j    

•  Noder  er  “first-­‐class”  ci@zens  I  grafen  – Hyperedges  støpes  ikke  -­‐>  kan  bruke  “event  nodes”    

•  Dyrt  å  aggregere  -­‐  caching  av  stats  på  noder!  

•  Unike  og  ekspressive  relasjonstyper  

Page 74: Introduction to Neo4j and Spring Data

Pi�alls  •  Ikke  bruk  graphid  som  ID  

•  Ini@aliser  alle  Set  

•  Bruk  @Fetch,  men  vær  forsik@g  

•  Bruk  standard  gepers/sepers  

•  Vær  varsom  med  String  escaping  

Page 75: Introduction to Neo4j and Spring Data

Inspirasjon  •  Single  Malt  Scotch  Whisky  

hpp://gist.neo4j.org/?8139605    

•  Chess  Games  and  Posiaons  hpp://gist.neo4j.org/?6506717    

•  Movie  Recommendaaons  hpp://gist.neo4j.org/?8173017  

•  Food  Recipes  Recommendaaon  hpp://gist.neo4j.org/?8731452