Time to Graph Up with Spring Data Neo4j

Time to Graph Up with Spring Data Neo4j

34ecacd077244d141a23c46ea094df5c?s=128

Gerrit Meier

May 25, 2018
Tweet

Transcript

  1. Gerrit Meier @meistermeier TIME TO GRAPH UP WITH SPRING DATA

    NEO4J
  2. Neo4j

  3. Neo4j The Whiteboard Model Is the Physical Model Hero PART_OF

    Team FIGHTS Bad Person
  4. Neo4j The Whiteboard Model Is the Physical Model {realName: “Anthony

    Stark”} {event: “Infinity War”} :Hero :PART_OF :Team :FIGHTS :Bad_Person
  5. Neo4j The Whiteboard Model Is the Physical Model Iron Man

    :PART_OF Avengers :FIGHTS Thanos Groot :PART_OF Guardians of the Galaxy :FIGHTS
  6. Neo4j Cypher Queries MATCH 
 (t:Team)<-[:PART_OF]-(h:Hero)-[f:FIGHTS]->(b:Bad_Person) WHERE badPerson.name=“Thanos” RETURN *

  7. Neo4j Cypher Queries

  8. Neo4j - Endpoints •Embedded •HTTP •Bolt

  9. Neo4j - Bolt •Java •C# •JavaScript •Python

  10. Neo4j - Java Java Driver Neo4j Object Graph Mapper (OGM)

    TransactionManger SessionFactory
  11. Neo4j - OGM Java Driver Neo4j Object Graph Mapper (OGM)

    Embedded Bolt HTTP
  12. Neo4j - OGM •Annotations •Configuration •Basic Data Access • Domain

    Methods • Custom Cypher Queries •Mapping Java Object <> Graph Model
  13. Neo4j - OGM Annotations :Hero :PART_OF :Team

  14. Neo4j - OGM Annotations @NodeEntity(label = "Hero") public class Hero

    { @Id @GeneratedValue private Long id; @Property(name = "name") private String superheroName; private String realName; @Relationship(type = "PART_OF", direction = Relationship.OUTGOING) private PartOf team; } :PART_OF :Team
  15. @RelationshipEntity(type = "PART_OF") public class PartOf { @Id @GeneratedValue private

    Long id; @StartNode private Hero hero; @EndNode private Team team; private String role; } Neo4j - OGM Annotations :Hero :Team
  16. @NodeEntity public class Team { @Id @GeneratedValue private Long id;

    private String name; } Neo4j - OGM Annotations :PART_OF :Hero
  17. Neo4j - OGM Configuration •Database Access Configuration • Mode •

    Connection Pool Size • Encryption •Packages to scan for Entities •Auto Indexing
  18. Neo4j - OGM Data Access Session session = sessionFactory.openSession(); Hero

    hero = new Hero(); session.save(hero);
  19. Neo4j - OGM Data Access Hero hero = session.load(Hero.class, heroId);

    Collection<Hero> heroes = session.loadAll(Hero.class);
  20. Neo4j - OGM Data Access Result result = session.query("MATCH (h:Hero)

    return h", emptyMap());
 Hero hero = session.queryForObject(Hero.class,
 “MATCH (h:Hero) return h", emptyMap());
  21. Neo4j - OGM Data Access session.delete(hero); session.deleteAll(Hero.class);

  22. Neo4j Object Graph Mapper (OGM) Java Driver Embedded Bolt HTTP

    Spring Data Neo4j (SDN) Neo4jRepository Spring Data Neo4j
  23. {name: “Emil Eifrem”} Spring Data Neo4j

  24. {name: “Emil Eifrem”} {name: “Rod Johnson”} Spring Data Neo4j

  25. {name: “Emil Eifrem”} {name: “Rod Johnson”} Spring Data Neo4j

  26. Spring Data Neo4j

  27. ? Spring Data Neo4j

  28. Me •Software Engineer @neo4j • Spring Data Neo4j • Neo4j

    Object Graph Mapper •Co-Leader @JUG_Ostfalen
  29. Spring Data Neo4j •Abstraction from Cypher (if wanted) •Derived Query

    Methods •Spring Data-ish way to access your data • CRUD support out-of-the-box •Specialisation for Graph Operations
  30. Spring Data Neo4j public interface HeroRepository extends Neo4jRepository<Hero, Long> {}

    •CRUD methods • save, findById, delete, count… •Graph specific • “second” parameter depth
  31. Spring Data Neo4j public interface HeroRepository extends Neo4jRepository<Hero, Long> {

    @Query("MATCH (h:Hero)-[:FIGHTS]->(b:Bad_Person) WHERE b.name={0} return h”) List<Hero> findAllies(String opponent); Hero findByRealName(String realName); }
  32. Spring Data Neo4j public interface HeroRepository extends Neo4jRepository<Hero, Long> {

    @Query("MATCH (h:Hero)-[:FIGHTS]->(b:Bad_Person) WHERE b.name={0} return h”) List<Hero> findAllies(String opponent); Hero findByRealName(String realName); }
  33. Spring Data Neo4j public interface HeroRepository extends Neo4jRepository<Hero, Long> {

    @Query("MATCH (h:Hero)-[:FIGHTS]->(b:Bad_Person) WHERE b.name={0} return h”) List<Hero> findAllies(String opponent); Hero findByRealName(String realName); } MATCH (h:Hero) where h.realName=realName
  34. Spring Data Neo4j 5.x •Spring Framework 5 •Spring Boot 2

    (only) •Schema-based Loading •Persistence Constructor •Spring Expression Language (SpEL) support in @Query
  35. Spring Data Neo4j - The Don’ts •Complex Custom Cyphers Statements

    •Processing high Amount of Nodes • e.g. Batch operations •Domain Models do not represent the Graph
  36. DEMO

  37. About the Demo •Download Neo4j Desktop from https://offshoreleaks.icij.org/ •Data description

    • Officer: a person or company who plays a role in an offshore entity. • Intermediary: go-between […]usually a law-firm or a middleman[…] • Entity: a company, trust or fund created in a low-tax, offshore jurisdiction[…] • Address: postal address […] • Other: additional information items.
  38. About the Demo

  39. QUESTIONS?

  40. Gerrit Meier @meistermeier THANKS!