object graph mapping with spring data neo4j 3 - nicki watt & michael hunger @ graphconnect...
DESCRIPTION
Nicki and Michael have recently been working together on the project to develop/upgrade the Spring Data Neo4j 3 (SDN) library to take advantage of some of the latest Neo4j 2.0 features. This talk takes a look at what can be expected of the new framework, and how it can be used to help model various different use cases with a simple Java domain model backed by a Neo4j database.TRANSCRIPT
Object Graph Mapping Spring Data Neo4j 3.0
Nicki Wa( @ opencredo Michael Hunger @ neotechnology
Agenda 1. Why Object-‐Graph-‐Mapping? 2. What is Spring Data (Neo4j)? 3. Current State of affairs 4. SDN 3.0 5. The Future 6. Some Spring Data Neoj Use-‐Cases
1. Why Object-‐Graph-‐Mapping?
Why OGM ? -‐ Convenience -‐ Simplify interac2on with underlying data source: Framework/Library handles basic CRUD type funcVonality for you
-‐ Provides ability to work with na2ve domain language constructs: eg domain classes/objects (POJOs in Java) in your applicaVon
-‐ Use Cases: examples at end of talk
Why OGM ? But … -‐ IndirecVon, addiVonal abstracVon layer adds performance, overhead
-‐ Encourages users to pull larger parts of the database into memory to process instead of le_ng the db do its job
2. What is Spring Data Neo4j?
Spring Data Neo4j -‐ OGM aimed at Java world & uses Spring
-‐ Neo4j Implementa2on of the Spring Data project: SpringSource/VMWare/Pivotal iniVaVve to give Spring developers easy access to the emerging world of NOSQL. Was the iniVal and first Spring Data project by Rod and Emil
-‐ Simple programming model: annotaVon-‐based programming for applicaVons with rich domains
With and without SDN ...
With and without SDN ...
Spring Data Neo4j -‐ features -‐ Two mapping modes: Simple & Advanced -‐ Provides Spring Data Repository Support -‐ Ability to drop down to Neo4j Core API if required
-‐ (Neo4j Server Support)
Example Domain: “Conference Management”
Sample graph
High Level Object Model
DePining Your Entities
SDN Domain Entities @NodeEntity @TypeAlias(value = "Conference") public class Conference { @GraphId Long graphId; @Indexed String name; Date date; @RelatedTo(type="ATTENDEE") Set<Person> attendees; @RelatedTo(type="TALK") Set<Talk> talks; // . . . }
SDN Domain Entities
@NodeEntity @TypeAlias(value = "Person") public class Person { @GraphId Long graphId; @Indexed(unique = true) String name; @RelatedToVia Iterable<Attended> talksAttended; // . . . }
SDN Domain Entities (Relationship)
@RelationshipEntity(type = "LISTENED_TO") public class Attended { @GraphId Long graphId; @StartNode Person attendee; @EndNode Talk talk; Integer rating; // . . . }
SDN Domain Entities @NodeEntity @TypeAlias(value = "Talk") public class Talk { @GraphId Long graphId; @Indexed String name; String description; @RelatedTo(type="SPEAKER") Set<Speaker> speakers; @RelatedToVia(direction = INCOMING) Set<Attended> attendees; // . . . }
SDN Domain Entities
@TypeAlias(value = "Speaker") public class Speaker extends Person { String bio; @RelatedTo(type="SPEAKER", direction = INCOMING) Iterable<Talk> talks; // . . . }
SDN Repository Support -‐ Derived Finder Methods
SDN Repository Support
import org....data.neo4j.repository.GraphRepository; import java.util.Set; public interface TalkRepository extends GraphRepository<Talk> { Set<Talk> findTalksBySpeakersName(String name); }
@NodeEntity @TypeAlias(value = "Talk") public class Talk { @GraphId Long graphId; @Indexed String name; String description; @RelatedTo(type="SPEAKER") Set<Speaker> speakers; @RelatedToVia(direction = INCOMING) Set<Attended> attendees; // . . . }
SDN Repository Support findTalksBySpeakersName
SDN Repository Support findTalksBySpeakersName
@TypeAlias(value = "Speaker") public class Speaker extends Person { String bio; @RelatedTo(type="SPEAKER", direction = INCOMING) Iterable<Talk> talks; // . . . }
@NodeEntity @TypeAlias(value = "Person") public class Person { @GraphId Long graphId; @Indexed(unique = true) String name; @RelatedToVia(type="ATTENDED") Iterable<Attended> talksAttended; // . . . }
SDN Repository Support
import org....data.neo4j.repository.GraphRepository; import java.util.Set; public interface TalkRepository extends GraphRepository<Talk> { Set<Talk> findTalksBySpeakersName(String name); }
START `talk_speakers`=node:`Person`(`name`={0}) MATCH (`talk`)-‐[:`SPEAKER`]-‐>(`talk_speakers`) RETURN `talk`
SDN Repository Support -‐ Dynamic Queries
SDN Repository Support import org....data.neo4j.repository.GraphRepository; import java.util.Set; public interface TalkRepository extends GraphRepository<Talk> { // Repository Query Method @Query(value = "MATCH (talk:Talk)-‐[:SPEAKER]-‐>(speaker:Speaker) " + "WHERE speaker.name={0} RETURN talk") Set<Talk> findTalksBySpeakersNameQuery(String name); // . . . }
3. Current State Of Affairs
Current State Of Affairs -‐ Current ProducVon Version: 2.3.2.RELEASE -‐ Neo4j 1.9.3 support
4. SDN 3.0
Disclaimer !!
Disclaimer SDN 3.0 s)ll in progress ... • Subject to change • Primary changes are Neo4j 2.0 compliance -‐
not finalised yet • follow progress on githubh"p://
spring.neo4j.org/source
SDN 3.0 -‐ Base Neo4j 2.0 support -‐ Spring 3.2.5 (eventually 4 as well) support -‐ New Type Representa2on Strategy -‐ Labels -‐ Migra2on towards more Cypher driven founda2on
-‐ Various bug fixes and enhancements
Neo4j 2.0 Support -‐ Will be able to run against a Neo4j 2.X DB -‐ Query using Labels -‐ Ability to use new Cypher Syntax -‐ Schema Indexes (Work in Progress)
Label Type Representation Strategy
@NodeEntity @TypeAlias(value = "Person") public class Person { @Query("MATCH (person)<-‐[:ATTENDEE]-‐(gathering:Conference)" + " WHERE id(person) = {self} " + " RETURN gathering") public Iterable<Conference> findAllConferencesAttended; // . . . } @NodeEntity @TypeAlias(value = "Conference") public class Conference { // . . . }
@TypeAlias(value = "Speaker") public class Speaker extends Person { String bio; @RelatedTo(type="SPEAKER", direction = INCOMING) Iterable<Talk> talks; // . . . }
@NodeEntity @TypeAlias(value = "Person") public class Person { @GraphId Long graphId; @Indexed(unique = true) String name; @RelatedToVia(type="ATTENDED") Iterable<Attended> talksAttended; // . . . }
Index vs Label TRS public interface ConferenceRepository extends GraphRepository<Conference> {
/* Query generated when using Index Based TRS START `conference`=node:__types__(className="Conference") WHERE `conference`.`date` = {0} RETURN `conference` Query generated when using Label Based TRS MATCH (`conference`:`Conference`) WHERE `conference`.`date` = {0} RETURN `conference` */ Set<Conference> findAllByDate(Date date); }
-‐ Will allow for schema indexes** to be used -‐ Will add addiVonal __TYPE__XX labels to nodes -‐ No addiVonal properVes need to be stored on nodes to represent hierarchy (If Index Based TRS used)
-‐ No addiVonal nodes need to be created to represent hierarchy (If Sub Reference based TRS used)
Label Type Representation Strategy
5. The Future
Future Plans -‐ Cypher based • Use all Cypher for the mapping layer • Align embedded and remote use • Benefit from Cypher enhancements • Leverage complex query mechanisms • Support more OGM type funcVonality
• Client side caching • Lazy loading
Future Plans -‐ OGM
• PotenVally extract or use standalone, cypher-‐based Java-‐Neo4j-‐OGM
• Also useable without Spring (Data)
Future Plans -‐ Query Results
• Project query results into object trees/graphs • Useful for direct view-‐layer integraVon • Less overhead by an intermediate domain
model that has to be converted
Future Plans -‐ Support
• Be(er support for • Spring Boot • Spring Data Rest • Remote transacVonal integraVon • JDBC-‐Driver in Spring
Future Plans -‐ Migration • Migrate Type RepresentaVon
• Index-‐based -‐> Label based • Refactor/Rename EnVVes, Fields, References • Rename Indexes • @TypeAlias
6. Some Use Cases
WhyOwnIt: Ownership Sharing Service
WhyOwnIt: Ownership Sharing Service
• Sharing Culture • You own things you only seldomly need • You need things you don‘t own
• Match needs and available devices/tools • add raVngs, trust, recommendaVons • locaVon based, close to you for pick-‐up
• Startup
WhyOwnIt: Ownership Sharing Service
WhyOwnIt: Ownership Sharing Service
WhyOwnIt: Ownership Sharing Service
Music Sharing/Discovery/Recommendation Service
Music Sharing/Discovery/Recommendation Service • Recommend new arVsts and songs to your
users • take their previous listen-‐history into
account • look at what‘s hot amongst their friends • infer broader selecVon via bands and genres
• Detect duplicates using media-‐fingerprints • copyright infringements • de-‐duplicate data
Music Sharing/Discovery/Recommendation Service
Music Sharing/Discovery/Recommendation Service
Other Use-‐Cases
Other Use-‐Cases • Financial Services -‐ Asset / Account Management • Bank -‐ Permission Management and AuthorizaVon
• EnVtlement • Data Sopware Provider -‐ Impact analysis of
changes • ImplicaVon of changes on the company core asset:
DATA • Change tracking • Root Cause Analysis
• Networked Storage Boxes • many components, disks, controllers, power supply,
sensors • Tracking of measurement data • Impact analyVcs • Early detecVon of anomalies • Intelligent fail over • Huge number of domain enVVes (~450)
• generaVng domain classes and repositories
SAN/NAS Provider -‐ Lifetime device tracking
Game Retailer • Buy and Download & Game on Demand • Fraud detecVon
• delayed payments, charge backs, fradulent keys are invalid
• Convert rules to traversals • co-‐occurrance of sudden large sales • fradulent e-‐mail pa(erns
• CombinaVon with Oracle Infrastructure • Cross-‐Store
Does it love me or does it not?
Domain-‐Centric • Integrate in a Springframework Context • ExisVng ApplicaVons with POJO Domains • Move a manageable amount of data in and
out of the database into Domain Objects • Rich, connected domain • MulVple projecVons
To SDN or Not to SDN ...
Data-‐Centric • high volume database interacVons
• reads and writes (inserts) • thin domain view on top of graph queries • remote Client to Neo4j Server
To SDN or Not to SDN ...
Want More Info ?
• Spring Data Book • Spring Data Neo4j Guidebook • Spring Data Neo4j on Spring.io • Chapter in Neo4j in AcVon • Cineasts.net Tutorial
Resources
Thanks ☺ Questions ?
@techiewa( @mesirii