All-new SDN-RX: Reactive Spring Data Neo4j Spring Data Neo4j / - - PowerPoint PPT Presentation
All-new SDN-RX: Reactive Spring Data Neo4j Spring Data Neo4j / - - PowerPoint PPT Presentation
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
2
Michael Simons @rotnroll666 Gerrit Meier @meistermeier
Reactive
3
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
- (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
Reactive streams
- Developed by Pivotal
- Excellent integration in the Spring ecosystem
- Publisher implementations
- Mono: 0 or 1 element
- Flux: 0 or n elements
6
Project Reactor
7
Working with reactive streams
Flux.range(0, 10) .skip(3) .map(value -> value * 2) .filter(value -> value % 6 == 0) .take(Duration. ofMillis(10)) .doOnEach(value -> System. out.println("Hello")); Output: Process finished
8
Working with reactive streams
Flux.range(0, 10) .skip(3) .map(value -> value * 2) .filter(value -> value % 6 == 0) .take(Duration. ofMillis(10)) .doOnEach(value -> System. out.println("Hello")) .subscribe(System. out::println); Output: Hello 6 Hello 12 Hello 18 Hello Process finished
Spring Data
9
- Consistent programming model
- Abstraction of data access
- Repositories as the main entrance to the data (DDD)
- Seamless integration in the Spring ecosystem
10
Spring Data
11
12
Neo4j Object Graph Mapper (Neo4j-OGM) Bolt Java Driver Embedded HTTP
13
Neo4j Object Graph Mapper (Neo4j-OGM)
Bolt Java Driver Embedded HTTP
Spring Data Neo4j (SDN)
14
Neo4j Object Graph Mapper (Neo4j-OGM) Spring Data Neo4j (SDN)
Chosen library by Users
15
Spring Data Neo4j RX
16
- 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
Why SDN/RX
18
Neo4j Object Graph Mapper (Neo4j-OGM)
Dataflow
19
Neo4j Object Graph Mapper (Neo4j-OGM)
Dataflow
20
Neo4j Object Graph Mapper (Neo4j-OGM)
Dataflow
Application code
- 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
What is SDN/RX
22
Modular design
Neo4j Driver Neo4j Client Spring Data Repositories SDN/RX Integration with Spring Transactions from here on java-driver-spring-boot-starter sdn-rx-spring-boot-starter “uses” Neo4j Template
- 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
What is in there (yet)
24
Example
@Node("Movie") public class MovieEntity { @Id private final String title; @Relationship (type = "ACTED_IN", direction = INCOMING) private Set<PersonEntity> actors; public MovieEntity(String title) { this.title = title; this.description = description; } } @Node("Person") public class PersonEntity { @Id private final String name; private final Long born; public PersonEntity(Long born, String name) { this.born = born; this.name = name; } }
:Person :Movie ACTED_IN
25
Example
public interface MovieRepository extends ReactiveNeo4jRepository<MovieEntity, String> { Mono<MovieEntity> findOneByTitle(String title); }
MATCH (n:Movie) WHERE n.title=$title return n;
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); }); }
✅
27
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"); }); }
✅ ❌
28
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(); }
✅
29
@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(); } }
Example
Spring Boot starters
30
- 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
Java Driver 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
Spring Data Users
Future plans
33
- estimated “1.0” early 2020
- needed for a GA from our point of view
○ Relationships with properties ○ Finalize Neo4jTemplate ○ Documentation
34
Future plans
Getting started today
35
- 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
Getting started
Thanks
37
38
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