All-new SDN-RX: Reactive Spring Data Neo4j Spring Data Neo4j / Neo4j-OGM Team
Michael Simons Gerrit Meier @rotnroll666 @meistermeier 2
Reactive 3
Reactive Reactive in general does not boost the performance ...but gives you the control over the data flow • e.g. back pressure • allows you to do more with less resources • 4
Reactive streams (defacto) Standard API for stream processing • asynchronous • non-blocking • back pressure • Interfaces define a contract • most important for a client/consumer: Publisher • Exposed by the Neo4j Java Driver • Implementations: Akka, Vert.x, RxJava, Project Reactor... • 5
Project Reactor Developed by Pivotal • Excellent integration in the Spring ecosystem • Publisher implementations • Mono: 0 or 1 element • Flux: 0 or n elements • 6
Working with reactive streams Flux. range (0, 10) Output: .skip(3) .map(value -> value * 2) Process finished .filter(value -> value % 6 == 0) .take(Duration. ofMillis (10)) .doOnEach(value -> System. out .println( "Hello" )); 7
Working with reactive streams Flux. range (0, 10) Output: .skip(3) Hello .map(value -> value * 2) 6 .filter(value -> value % 6 == 0) Hello .take(Duration. ofMillis (10)) 12 .doOnEach(value -> System. out .println( "Hello" )) Hello .subscribe(System. out ::println); 18 Hello Process finished 8
Spring Data 9
Spring Data Consistent programming model ● Abstraction of data access ● Repositories as the main entrance to the data (DDD) ● Seamless integration in the Spring ecosystem ● 10
11
Neo4j Object Graph Mapper (Neo4j-OGM) Embedded Bolt HTTP Java Driver 12
Spring Data Neo4j (SDN) Neo4j Object Graph Mapper (Neo4j-OGM) Embedded Bolt HTTP Java Driver 13
Chosen library by Users Spring Data Neo4j (SDN) Neo4j Object Graph Mapper (Neo4j-OGM) 14
15
Spring Data Neo4j RX 16
Why SDN/RX OGM has a lot of custom class scanning/analysing ● Spring Data Commons has superb support for this ○ OGM’s mapping logic has a lot of caching that prevents ● (reactive) data streaming 17
Dataflow Neo4j Object Graph Mapper (Neo4j-OGM) 18
Dataflow Neo4j Object Graph Mapper (Neo4j-OGM) 19
Dataflow Neo4j Object Graph Mapper (Neo4j-OGM) Application code 20
What is SDN/RX Combination of SDN and OGM in the Spring ecosystem ● No “OGM-Drivers” ● All communication with Neo4j through the Java-Driver using ○ the Bolt-protocol Key focus ● Reactive support ○ Immutability of objects ○ 21
Modular design SDN/RX Spring Data Repositories sdn-rx-spring-boot-starter Neo4j Template Integration with Spring Transactions from here on Neo4j Client Driver java-driver-spring-boot-starter “uses” Neo4j 22
What is in there (yet) Node modelling ● Relationships between nodes ● Full support for all Spring Data built-in repository functions ● Derived finder methods ● Custom Cypher annotated methods ● Conversion and Neo4j type support ● Projections ● Uses the new Spring 5.2 ReactiveTransactionManager ● 23
Example @Node( "Movie" ) @Node( "Person" ) public class MovieEntity { public class PersonEntity { @Id @Id private final String title ; private final String name ; @Relationship (type = "ACTED_IN" , private final Long born ; direction = INCOMING ) private Set<PersonEntity> actors ; public PersonEntity(Long born, String name) { public MovieEntity(String title) { this . born = born; this . title = title; this . name = name; this . description = description; } } } } ACTED_IN :Movie :Person 24
Example public interface MovieRepository extends ReactiveNeo4jRepository<MovieEntity, String> { Mono< MovieEntity > findOneBy Title (String title ); } MATCH (n: Movie ) WHERE n. title = $title return n; 25
Example @Test void findOneMovieByTitle() { movieRepository .findOneByTitle( "The Matrix" ).subscribe(movie -> { assertThat (movie.getTitle()).isEqualTo( "The Matrix" ); assertThat (movie.getDescription()).isEqualTo( "..." ); assertThat (movie.getDirectors()).hasSize( 2); assertThat (movie.getActors()).hasSize( 5); }); ✅ } 26
Example @Test void findOneMovieByTitle() { movieRepository .findOneByTitle( "The Matrix" ).subscribe(movie -> { assertThat (movie.getTitle()).isEqualTo( "The Matrix" ); assertThat (movie.getDescription()).isEqualTo( "..." ); assertThat (movie.getDirectors()).hasSize( 2); assertThat (movie.getActors()).hasSize( 5); fail ( "You won’t get to me" ); ✅ ❌ }); } 27
Example @Test void findOneMovieByTitle() { StepVerifier. create ( movieRepository .findOneByTitle( "The Matrix" )) .assertNext(movie -> { assertThat (movie.getTitle()).isEqualTo( "The Matrix" ); assertThat (movie.getDescription()).isEqualTo( "..." ); assertThat (movie.getDirectors()).hasSize( 2); assertThat (movie.getActors()).hasSize( 5); ✅ }) .verifyComplete(); } 28
Example @RestController public class DisplayMoviesController { private final MovieRepository movieRepository ; public DisplayMoviesController(MovieRepository movieRepository) { this . movieRepository = movieRepository; } @GetMapping(path = "/movies" , produces = MediaType. TEXT_EVENT_STREAM_VALUE ) public Flux<MovieEntity> getMovies() { return movieRepository .findAll(); } } 29
Spring Boot starters 30
Java Driver Users neo4j-java-driver-spring-boot-starter (1.7.x/4.0.x) ● Creates a Java driver bean ● Driver’s configuration properties in application.properties ● Spring Boot health endpoint support ● Spring Boot micrometer support (4.0.x) ● 31
Spring Data Users ● spring-data-neo4j-rx-spring-boot-starter Depends on the Java driver Spring Boot starter ● Creates ● Neo4jClient and Neo4jTemplate beans ○ reactive variants thereof ○ Instantiates all user-defined repositories ● 32
Future plans 33
Future plans estimated “1.0” early 2020 ● needed for a GA from our point of view ● Relationships with properties ○ Finalize Neo4jTemplate ○ Documentation ○ 34
Getting started today 35
Getting started SDN/RX • https://github.com/neo4j/sdn-rx Introduction post • https://medium.com/neo4j/welcome-sdn-rx-22c8fe6cd955 Introduction video • https://youtu.be/Nfk4782Da0E Information on the current state of Neo4j-OGM • https://medium.com/neo4j/neo4j-ogm-3-2-released-cdbaf1be1400 Developer guides for Java Driver, SDN/RX, Spring Boot Starters • https://neo4j.com/developer/java/ 36
Thanks 37
Hunger Games Questions for "All-new SDN-RX: Reactive Spring Data Neo4j" 1. Easy : What should be the general approach to access data in Spring Data? a. Driver b. Repository c. Controller 2. Medium : What is the key focus when developing Spring Data Neo4j RX? a. Reactive data access with immutable objects b. Heavy memory usage c. Cypher-Free 3. Hard : How are user-defined methods in a repository called, that do not force you to write any Cypher query? Answer here: r.neo4j.com/hunger-games 38
Recommend
More recommend