It’s not “Never SQL” It’s “Not Only SQL” NOSQL \no-seek-wool\ n. Describes ongoing trend where developers increasingly opt for non-relational databases to help solve their problems, in an effort to use the right tool for the right job.
Tool für das Problem nutzen ๏Beispiele: •RDBMS für strukturierte Daten und eine Graphendatenbank für die Relationen zwischen den Entitäten •Domänenmodell in der Graphendatenbank und große Dokumente in einer Dokumenten Datenbank
Text Hypertext RSS Blogs Wikis user generated tagging RDF Ontologien Semantic Web Giant Global Graph web 1.0 web 2.0 web 3.0 Application Applications Services / Applications
๏Beziehungen sind oft genauso wichtig wie die Dinge ๏Netze = Ganzes > Summe der Teile ๏komplexe Zusammenhänge ๏ständiger Wandel, Veränderung auch von Strukturen ๏Graph: Beziehungen sind Teil der Daten ๏RDBMS: Beziehungen sind Teil des starren Schemas 23
mehr als eine Kategorie passt? ๏Tags ๏Kategorie über Beziehungen „IS_A“ ๏beliebig viele, schnelle Änderung ๏„virtuelle“ Beziehungen - Traversals ๏Kategorie dynamisch aus den Fragestellungen 25
DRIVES LOVES name: “James” age: 32 twitter: “@spam” name: “Mary” age: 35 brand: “Volvo” model: “V70” item type: “car” •Nodes •Relationships between Nodes •Relationships have Labels •Relationships are directed, but traversed at equal speed in both directions •The semantics of the direction is up to the application (LIVES WITH is reflexive, LOVES is not) •Nodes have key-value properties •Relationships have key-value properties
50 Freunde 44 Database # persons query time Relational database Neo4j Graph Database Neo4j Graph Database 1 000 2 000 ms 1 000 2 ms 1 000 000 2 ms Tobias Emil Johan Peter
Edition •alle Features, Embedded und Server •GPL ๏Advanced •+ Monitoring & Support •AGPL oder Commercial ๏Enterprise •HA •Online Backup •AGPL oder Commercial
Erfolg ๏durch GPL noch einmal verstärkt ๏sehr aktive Mailingliste (700 / Monat) ๏GitHub •http://github.com/neo4j •http://github.com/neo4j-examples ๏Wiki •http://wiki.neo4j.org ๏Alle Bindings / Driver von der Community entwickelt •http://neo4j.org/community/languages/
collaborating with Neo Technology, the company behind the Neo4j graph database. ๏Improved programming model: Annotation-based programming model for graph applications ๏Cross-store persistence: Extend existing JPA application with NOSQL persistence ๏Spring Roo support: Add graph persistence with Roo add-on
directly persisted •For all other types, Spring conversion support can be used • Enum and Date conversion is provided out-of-the-box •Transient fields not persisted 55 @NodeEntity public class Actor { private String name; private int age; private HairColor hairColor; private transient String nickname; }
a node (@NodeEntity) 56 @NodeEntity public class Movie {} @NodeEntity public class Person { private Movie favoriteMovie; } @NodeEntity public class Movie { private Actor topActor; } @NodeEntity public class Actor { // Mirrors topActor in Movie @RelatedTo(type = ”topActor”, direction = Direction.INCOMING) private Movie wasTopActorIn; }
entities: @RelatedToVia ๏Read only view of relationship entities 58 @RelationshipEntity public class Role { @StartNode private Actor actor; @EndNode private Movie movie; private String roleName; } @NodeEntity public class Actor { @RelatedToVia(type = “ACTS_IN”) private Iterable<Role> roles; }
@Indexed private HairColor hairColor; GraphRepository<Actor> actorRepo = repoFactory.createGraphRepository(Actor.class); Actor kevin = actorRepo.findByPropertyValue(“name”, “Kevin Bacon”); Iterable<Actor> allBlondActors = actorRepo.findAllByPropertyValue(“hairColor”, “blond”); It can then be looked up: By annotating an entity field with @Indexed it becomes searchable:
String name; ๏Index name defaults to domain class name ๏Index key defaults to field name ๏Fulltext indices are possible with @Indexed(fulltext = true) ๏Repository query methods for any Lucene query, including ranges: Iterable<Actor> allKevinsOlderThan32 = actorRepo.findAllByQuery(“name:Kevin* AND age>32”); Iterable<Actor> youngActors = actorRepo.findAllByRange(“age”, 3, 18);
Eclipse with current AJDT plugin •IntelliJ IDEA 10.5 compile + run, some editor quirks ‣full AspectJ support in IDEA 11 ๏Build Systems •Maven •Gradle •Ant / Ivy •...
node, Class<T> relationshipEntityType, String relationshipType ); @NodeEntity public class Actor { public Role actsIn(Movie movie, String roleName) { Role role = relateTo(movie, Role.class, “ACTS_IN”); role.setName(roleName); return role; } } usage:
•In transaction •Detached •Outside transaction entity.persist() entity.persist() beginTx() modify entity / data stored in mem beginTx() Create new entity create outside tx create in tx modify entity / data written through tx.commit() tx.commit() modify entity / data stored in mem Detached Attached
graphDatabaseService="restGraphDatabaseService"/> ๏drop-in replacement for the embedded GraphDatabase ๏works transparently with POJO-Entity-Mapping and Neo4j-Template
{ super(new String[]{"spring/helloWorldServer-Context.xml"}, Pair.of("worldRepository", WorldRepository.class), Pair.of("graphRepositoryFactory", GraphRepositoryFactory.class)); } } ๏integrate Spring Data Graph config with already running Graph-Database in Neo4j-Server ๏expose Spring Beans as Jersey Injectables
non-JPA persistence providers ๏Spring Data Graph was the first NOSQL persistence Roo Add-On ๏See the chapter on Spring Data Graph in the latest O’Reilly Roo book, Getting Started with Roo.
with a great Guide Book, featuring: •Forewords by Rod Johnson and Emil Eifrem •An easy to read, narrative tutorial walkthrough for cineasts.net •A comprehensive reference for all the details •Check it out here: http://bit.ly/sdg-book 76 “I’m excited about Spring Data Graph.... Spring Data Graph makes working with Neo4j amazingly easy, and therefore has the potential to make you more successful as a developer.” Rod Johnson, founder of Spring
site for more info: http://bit.ly/sdg-home ๏Check out the 5 minute intro at GitHub: http://bit.ly/sdg-repo ๏Again, don’t miss our fantastic e-book on Spring Data Graph: http://bit.ly/sdg-book ๏Spring Data Forum at http://bit.ly/sd-forum ๏All about Neo4j: http://neo4j.org ๏Neo4j videos and webinars: http://video.neo4j.org
m-[ACTS_IN]-actor return actor") Iterable<Actor> getActorsCypher(@Param("movie") Movie m); @Query("start movie =(%movie) match (movie)<-[role:ACTS_IN]-(actor) return actor.name, role.title") Iterable<Map<String,Object>> getCast(@Param("movie") Movie m); @Query(value = "g.v(movie).out('ACTS_IN')", type = QueryType.Gremlin) Iterable<Person> getActorsGremlin(@Param("movie") Movie m); }
social networking features by using a NOSQL database - what are your options? A. Complete re-write of the app •Costly and time consuming •Upside: remove JPA and build it all on a NOSQL database B. Bolt on some social networking features with a completely separate data model •No integration between the JPA and NOSQL data models •Harder to maintain code due to this split C. Extend JPA data model with cross-store persistence, creating a unified data model ๏What would you choose?
can share a data model ๏Could be the entire entity, or some fields of an entity ๏We call this cross-store persistence •One transaction manager to coordinate the NOSQL database with the JPA relational database •AspectJ support to manage the NOSQL entities and fields