Slide 1

Slide 1 text

Gerrit Meier @meistermeier What the Graph?

Slide 2

Slide 2 text

Neo4j

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Ecosystem Neo4j Professional Services 300+ partners 47,000 group members 61,000 trained engineers 3.5M downloads Mindset “Graph Thinking” is all about considering connections in data as important as the data itself. Native Graph Platform Neo4j is an internet-scale, native graph database which executes connected workloads faster than any other database management system. Neo4j

Slide 5

Slide 5 text

Graph Databases

Slide 6

Slide 6 text

Graph Databases Hero

Slide 7

Slide 7 text

Graph Databases Label Type

Slide 8

Slide 8 text

Graph Databases Person KNOWS {
 name: “Mr. X” dob: 26.02.1982
 } {
 met: “Spring IO” contact: 16.05.2019
 }

Slide 9

Slide 9 text

Graph Databases Person KNOWS Person Topic INTERESTED_IN INTERESTED_IN {
 term: “Spring Data” description: “ … “
 }

Slide 10

Slide 10 text

Data Access

Slide 11

Slide 11 text

Cypher Cypher Query Language is to Neo4j what 
 SQL is for relational databases. Open Source http://www.opencypher.org/ and ready for other Graph Database technology.

Slide 12

Slide 12 text

Cypher :Person :KNOWS :Person :Topic :INTERESTED_IN :INTERESTED_IN

Slide 13

Slide 13 text

Cypher Person KNOWS Person Topic INTERESTED_IN INTERESTED_IN MATCH 
 (person:Person)-[:INTERESTED_IN]"->(t:Topic)"<-[:INTERESTED_IN]-(otherPerson:Person) WHERE 
 (person)-[:KNOWS]-(otherPerson)
 RETURN
 otherPerson

Slide 14

Slide 14 text

Connect to Neo4j •Bolt - Binary protocol •HTTP - First remote access, no library needed •Embedded - Running Neo4j in the same JVM

Slide 15

Slide 15 text

Bolt drivers •Languages - Java, Python, JavaScript, C#, Go •Seabolt - connector written in C •Community drivers - R, Rust, Elixir…

Slide 16

Slide 16 text

Neo4j-OGM Java Driver Neo4j-OGM Embedded Bolt HTTP

Slide 17

Slide 17 text

Neo4j-OGM •Annotations - Describe your Graph •Configuration - Driver, Domain packages,… •Mapping - Java Object Graph <> Graph Model •Basic Data Access - CRUD operations built-in

Slide 18

Slide 18 text

Neo4j-OGM :Person :Topic :INTERESTED_IN

Slide 19

Slide 19 text

Neo4j-OGM :Topic :INTERESTED_IN @NodeEntity public class Person { @Id @GeneratedValue private Long id; @Property private String name; @Relationship(type = “INTERESTED_IN") private Set topic; }

Slide 20

Slide 20 text

Neo4j-OGM :INTERESTED_IN @NodeEntity public class Topic { @Id @GeneratedValue private Long id; private String term; private String description; } :Person

Slide 21

Slide 21 text

Neo4j-OGM :Person :Topic :INTERESTED_IN Session session = sessionFactory.openSession(); Person person = new Person(“Mr. X”); person.addTopic(new Topic(“Spring”)); session.save(person);

Slide 22

Slide 22 text

Neo4j-OGM :Person :Topic :INTERESTED_IN Person person = session.load(Person.class, personId); Collection people = session.loadAll(Person.class);

Slide 23

Slide 23 text

Neo4j-OGM :Person :Topic :INTERESTED_IN Person person = session.queryForObject(Person.class,
 "MATCH (p:Person) where p.name = ${name} return p",
 singletonMap("name", “Mr. X”) );

Slide 24

Slide 24 text

Spring Data Neo4j

Slide 25

Slide 25 text

Spring Data Neo4j •Convenient Data Access - Spring Data’s Domain-Driver-Design way •Derived Finder Methods - no Cypher needed •Abstraction from Cypher - if wanted •Specialisation for Graph Operations - graph-ish way to work with data

Slide 26

Slide 26 text

Spring Data Neo4j •CRUD Methods - save, findById, delete, count… •Graph specific - depth as “second” parameter interface PersonRepository extends Neo4jRepository {}

Slide 27

Slide 27 text

Spring Data Neo4j interface PersonRepository extends Neo4jRepository { Person findByName(String name); }

Slide 28

Slide 28 text

Spring Data Neo4j interface PersonRepository extends Neo4jRepository { Person findByName(String name); } MATCH 
 (person:Person)-[int:INTERESTED_IN]"->(topic:Topic) WHERE 
 person.name=$name 
 RETURN
 person, int, topic

Slide 29

Slide 29 text

Spring Data Neo4j interface PersonRepository extends Neo4jRepository { @Query("MATCH (p:Person)-[:INTERESTED_IN]->(t:Topic) WHERE t.term={0} return p”) List customFindByInterest(String interest); }

Slide 30

Slide 30 text

Spring Data Neo4j 5.x (Lovelace/Moore) •Spring Boot 2.x only •Schema-based Loading •Persistence Constructor

Slide 31

Slide 31 text

The Don’ts

Slide 32

Slide 32 text

The Don’ts •Complex Custom Cypher •Batch Operations •Domain Model does not represent the Graph •Unwrapping the Session / Driver all the time

Slide 33

Slide 33 text

Getting started

Slide 34

Slide 34 text

Import •LOAD CSV - File, HTTP(S) or FTP •APOC - Support for JSON, JDBC… •ETL Tool - Import and map data from SQL source WITH “jdbc:mysql://database” AS url CALL apoc.load.jdbc(url) YIELD value UNWIND value.items AS item MERGE (:Item {title=item.title} LOAD CSV WITH HEADERS 
 FROM “file:paradise.csv” AS row CREATE 
 (:Officer {name: row.n.name, country: row.n.country,…

Slide 35

Slide 35 text

Sandbox

Slide 36

Slide 36 text

Thanks

Slide 37

Slide 37 text

Spring Data Neo4j ⚡ RX* *working title

Slide 38

Slide 38 text

SDN⚡RX •Complete rewrite of Spring Data Neo4j & Neo4j-OGM •(not only) Reactive Support •(Reactive)Neo4jClient •Cypher DSL •real Immutable Mapping •new Spring Boot Autoconfiguration

Slide 39

Slide 39 text

SDN⚡RX Node personNode = node("Person"); Statement statement = match(personNode) .where(personNode.property("name").isEqualTo(literalOf("Mr.X"))) .returning(personNode) .build();

Slide 40

Slide 40 text

SDN⚡RX Node personNode = node("Person"); Statement statement = match(personNode) .where(personNode.property("name").isEqualTo(literalOf("Mr.X"))) .returning(personNode) .build(); ReactiveNeo4jClient client = ReactiveNeo4jClient.create(driver); Flux people = client .newQuery(renderer.render(statement)) .fetchAs(Person.class) .mappedBy(record !-> {!/*!..!*/}) !// optional mapping .all() .subscribe(person !-> {!/*!..!*/});

Slide 41

Slide 41 text

SDN⚡RX public class Person { @Id private final Long id; private final String name; private Person(Long id, String name) { this.id = id; this.name = name; } public Person withId(Long newId) { return new Person(newId, this.name); } public Person withName(String newName) { return new Person(this.id, newName); } } @AllArgsConstructor public class PersonWithAllConstructor { @Id private final Long id; private final String name; private final String firstName; private final String sameValue; private final Boolean cool; private final Long personNumber; private final LocalDate bornOn; }

Slide 42

Slide 42 text

SDN⚡RX @Node data class KotlinPerson(@Id val id: Long, val name: String)

Slide 43

Slide 43 text

Links •Neo4j Desktop - https://neo4j.com/download •Neo4j Sandbox - https://neo4j.com/sandbox-v2/ •This Talk - https://speakerdeck.com/meistermeier •Free Graph Algorithms book - https://neo4j.com/graph-algorithms-book

Slide 44

Slide 44 text

Gerrit Meier @meistermeier Thanks!