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

Time to Graph Up with Spring Data Neo4j

Time to Graph Up with Spring Data Neo4j

Gerrit Meier

May 25, 2018
Tweet

More Decks by Gerrit Meier

Other Decks in Programming

Transcript

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

    View full-size slide

  2. Neo4j
    The Whiteboard Model Is the Physical Model
    Hero
    PART_OF
    Team
    FIGHTS
    Bad Person

    View full-size slide

  3. Neo4j
    The Whiteboard Model Is the Physical Model
    {realName: “Anthony Stark”}
    {event: “Infinity War”}
    :Hero
    :PART_OF
    :Team
    :FIGHTS
    :Bad_Person

    View full-size slide

  4. Neo4j
    The Whiteboard Model Is the Physical Model
    Iron Man
    :PART_OF
    Avengers
    :FIGHTS
    Thanos
    Groot
    :PART_OF
    Guardians
    of the
    Galaxy
    :FIGHTS

    View full-size slide

  5. Neo4j
    Cypher Queries
    MATCH 

    (t:Team)<-[:PART_OF]-(h:Hero)-[f:FIGHTS]->(b:Bad_Person)
    WHERE badPerson.name=“Thanos”
    RETURN *

    View full-size slide

  6. Neo4j
    Cypher Queries

    View full-size slide

  7. Neo4j - Endpoints
    •Embedded

    •HTTP

    •Bolt

    View full-size slide

  8. Neo4j - Bolt
    •Java

    •C#

    •JavaScript

    •Python

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  11. Neo4j - OGM
    •Annotations

    •Configuration

    •Basic Data Access

    • Domain Methods

    • Custom Cypher Queries

    •Mapping Java Object <> Graph Model

    View full-size slide

  12. Neo4j - OGM Annotations
    :Hero
    :PART_OF
    :Team

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. @NodeEntity
    public class Team {
    @Id @GeneratedValue
    private Long id;
    private String name;
    }
    Neo4j - OGM Annotations
    :PART_OF
    :Hero

    View full-size slide

  16. Neo4j - OGM Configuration
    •Database Access Configuration

    • Mode

    • Connection Pool Size

    • Encryption

    •Packages to scan for Entities

    •Auto Indexing

    View full-size slide

  17. Neo4j - OGM Data Access
    Session session = sessionFactory.openSession();
    Hero hero = new Hero();
    session.save(hero);

    View full-size slide

  18. Neo4j - OGM Data Access
    Hero hero = session.load(Hero.class, heroId);
    Collection heroes = session.loadAll(Hero.class);

    View full-size slide

  19. 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());

    View full-size slide

  20. Neo4j - OGM Data Access
    session.delete(hero);
    session.deleteAll(Hero.class);

    View full-size slide

  21. Neo4j Object Graph Mapper (OGM)
    Java Driver
    Embedded Bolt HTTP
    Spring Data Neo4j (SDN)
    Neo4jRepository
    Spring Data Neo4j

    View full-size slide

  22. {name: “Emil Eifrem”}
    Spring Data Neo4j

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  25. Spring Data Neo4j

    View full-size slide

  26. ?
    Spring Data Neo4j

    View full-size slide

  27. Me
    •Software Engineer @neo4j

    • Spring Data Neo4j

    • Neo4j Object Graph Mapper

    •Co-Leader @JUG_Ostfalen

    View full-size slide

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

    View full-size slide

  29. Spring Data Neo4j
    public interface HeroRepository extends Neo4jRepository {}
    •CRUD methods

    • save, findById, delete, count…

    •Graph specific

    • “second” parameter depth

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. Spring Data Neo4j 5.x
    •Spring Framework 5

    •Spring Boot 2 (only)

    •Schema-based Loading

    •Persistence Constructor

    •Spring Expression Language (SpEL) support in @Query

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  36. About the Demo

    View full-size slide

  37. Gerrit Meier
    @meistermeier
    THANKS!

    View full-size slide