Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Neo4j

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Neo4j Cypher Queries

Slide 8

Slide 8 text

Neo4j - Endpoints •Embedded •HTTP •Bolt

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Neo4j - OGM •Annotations •Configuration •Basic Data Access • Domain Methods • Custom Cypher Queries •Mapping Java Object <> Graph Model

Slide 13

Slide 13 text

Neo4j - OGM Annotations :Hero :PART_OF :Team

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

@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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Neo4j - OGM Configuration •Database Access Configuration • Mode • Connection Pool Size • Encryption •Packages to scan for Entities •Auto Indexing

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

{name: “Emil Eifrem”} Spring Data Neo4j

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Spring Data Neo4j

Slide 27

Slide 27 text

? Spring Data Neo4j

Slide 28

Slide 28 text

Me •Software Engineer @neo4j • Spring Data Neo4j • Neo4j Object Graph Mapper •Co-Leader @JUG_Ostfalen

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Spring Data Neo4j public interface HeroRepository extends Neo4jRepository {} •CRUD methods • save, findById, delete, count… •Graph specific • “second” parameter depth

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Spring Data Neo4j 5.x •Spring Framework 5 •Spring Boot 2 (only) •Schema-based Loading •Persistence Constructor •Spring Expression Language (SpEL) support in @Query

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

DEMO

Slide 37

Slide 37 text

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.

Slide 38

Slide 38 text

About the Demo

Slide 39

Slide 39 text

QUESTIONS?

Slide 40

Slide 40 text

Gerrit Meier @meistermeier THANKS!