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 Slide

  2. Neo4j

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. Neo4j
    Cypher Queries
    MATCH 

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

    View Slide

  7. Neo4j
    Cypher Queries

    View Slide

  8. Neo4j - Endpoints
    •Embedded

    •HTTP

    •Bolt

    View Slide

  9. Neo4j - Bolt
    •Java

    •C#

    •JavaScript

    •Python

    View Slide

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

    View Slide

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

    View Slide

  12. Neo4j - OGM
    •Annotations

    •Configuration

    •Basic Data Access

    • Domain Methods

    • Custom Cypher Queries

    •Mapping Java Object <> Graph Model

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

  17. Neo4j - OGM Configuration
    •Database Access Configuration

    • Mode

    • Connection Pool Size

    • Encryption

    •Packages to scan for Entities

    •Auto Indexing

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. Spring Data Neo4j

    View Slide

  27. ?
    Spring Data Neo4j

    View Slide

  28. Me
    •Software Engineer @neo4j

    • Spring Data Neo4j

    • Neo4j Object Graph Mapper

    •Co-Leader @JUG_Ostfalen

    View Slide

  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

    View Slide

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

    • save, findById, delete, count…

    •Graph specific

    • “second” parameter depth

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

    View Slide

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

  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

    View Slide

  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

    View Slide

  36. DEMO

    View Slide

  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.

    View Slide

  38. About the Demo

    View Slide

  39. QUESTIONS?

    View Slide

  40. Gerrit Meier
    @meistermeier
    THANKS!

    View Slide