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. Neo4j The Whiteboard Model Is the Physical Model {realName: “Anthony

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

    :PART_OF Avengers :FIGHTS Thanos Groot :PART_OF Guardians of the Galaxy :FIGHTS
  3. Neo4j - OGM •Annotations •Configuration •Basic Data Access • Domain

    Methods • Custom Cypher Queries •Mapping Java Object <> Graph Model
  4. 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
  5. @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
  6. @NodeEntity public class Team { @Id @GeneratedValue private Long id;

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

    Connection Pool Size • Encryption •Packages to scan for Entities •Auto Indexing
  8. Neo4j - OGM Data Access Hero hero = session.load(Hero.class, heroId);

    Collection<Hero> heroes = session.loadAll(Hero.class);
  9. 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());
  10. Neo4j Object Graph Mapper (OGM) Java Driver Embedded Bolt HTTP

    Spring Data Neo4j (SDN) Neo4jRepository Spring Data Neo4j
  11. Me •Software Engineer @neo4j • Spring Data Neo4j • Neo4j

    Object Graph Mapper •Co-Leader @JUG_Ostfalen
  12. 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
  13. Spring Data Neo4j public interface HeroRepository extends Neo4jRepository<Hero, Long> {}

    •CRUD methods • save, findById, delete, count… •Graph specific • “second” parameter depth
  14. 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); }
  15. 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); }
  16. 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
  17. Spring Data Neo4j 5.x •Spring Framework 5 •Spring Boot 2

    (only) •Schema-based Loading •Persistence Constructor •Spring Expression Language (SpEL) support in @Query
  18. 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
  19. 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.