Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Copyright 2014 Emmanuel Bernard and Red Hat Inc.
MythBusters
OxMs and NoSQL
good or bad?
Emmanuel Bernard
Red Hat
Copyright 2014 Emmanuel Bernard and Red Hat Inc.
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
What’s in it for me
• Better understand the usage of NoSQL
• Underst...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Emmanuel Bernard
• Data dude at Red Hat (middleware)
• Hibernate pro...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 1
Once you have chosen a NoSQL product
you don't need any other
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Use case
Create a scalable web shop handling billions of products.
U...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Storing products
• Document store
• Self-describing
schema
• Nested ...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Find patterns
• Graph database
• Good at connections
• (Single node ...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Store comments
• Cassandra-like
• Linear scalability
• Awesome for t...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Other use cases
• Super fast pre computed recommendations
• Key/valu...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Choosing a NoSQL engine
• Based on the read / write characteristics ...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 1
Once you have chosen a NoSQL product
you don't need any other...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 3
Entities feel awkward in NoSQL
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Mapping types and properties
@Entity
public class News {
@Id
private...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Embedded
Collections of Embedded
@Entity
public class News {
@Id
Str...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Association: duplicate on each side
• Mapped to relationships for gr...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Many more details
for a natural mapping
• Embedded id
• Identifier g...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
NoSQL is schemaless
• Well no - at least not without
schema
• Schema...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Handling evolution
• Separate the app constructs from the DB constru...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Handling unknown properties
• Unknown to the application
• Carried o...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 3
Entities feel awkward in NoSQLBeing
busted
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 4
You need native APIs for real NoSQL use
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 4
You need native APIs for real NoSQL use
Part I: CRUD
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
OO stuff
• Domain model description
• JPA covers it
• What about NoS...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Depends on the option scope…
• Declarative options: natural way to a...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 4
You need native APIs for real NoSQL use
Part I: CRUD
Maybe
Ma...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 5
You need native APIs for real NoSQL use
Part II: Query langua...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
JP-QL as the query language
• Queries over object properties and ass...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Specific constructs?
• Full-text search
• Geolocation search
• JP-QL...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Native QL to managed objects
• Taking the problem upside down
• Use ...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 5
You need native APIs for real NoSQL use
Part II: Query langua...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 6
Object mappers don’t add enough value
for NoSQL
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Abstraction
• What does abstraction
brings?
• Automated better
• Mor...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Natural mappings
• Offer natural data mapping
• Specific for each st...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Denormalization engine
• Object and relations are the canonical form...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Polyglot persistence
• Different queries, different backends
• Domai...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Feed event based logics
• Data validation (Hibernate Validator)
• Fu...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 6
Object mappers don’t add enough value
for NoSQL
Busted
Copyright 2014 Emmanuel Bernard and Red Hat Inc.
Object mappers for NoSQL
Heresy or Awesomeness
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Where is Hibernate OGM at
• Increased adoption
• Backends
• MongoDB,...
Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Q&A
• Try Hibernate OGM
• Contribute (feedback, doc, code, hatred/lo...
Upcoming SlideShare
Loading in …5
×

Emmanuel Bernard - MythBusters: ORMs and NoSQL - Good or Evil? - NoSQL matters Paris 2015

1,318 views

Published on

Want to understand what persistence to NoSQL means in a Java architecture? Read on.Java is an object oriented kingdom where ORMs have flourished. Do ORMs still provide benefits in a NoSQL and polyglot era? As a side effect we will discuss the why and how of data design in NoSQL.Busted, plausible or confirmed? Here is a list of non exclusive myths and preconceptions we will explore:Once you have chosen a NoSQL product, you don't need any other.NoSQL is a data duplication nightmare.JPA is not suited to non relational databases.Objects don't fit in the data structures of NoSQL solutions.ORMs cannot abstract the underlying NoSQL models.ORMs speeds things up at runtime.The query language of NoSQL cannot be abstracted.You need access to the native API to make real use of NoSQL.ORMs don’t bring much value in an NoSQL world.And more.While we can't do explosions experiments due to fire safety regulations, your vocal contribution is encouraged. Come and debate!Note: we mention ORMs but it should really be OXMs or ONMs :)

Published in: Software
  • Login to see the comments

Emmanuel Bernard - MythBusters: ORMs and NoSQL - Good or Evil? - NoSQL matters Paris 2015

  1. 1. Copyright 2014 Emmanuel Bernard and Red Hat Inc. MythBusters OxMs and NoSQL good or bad? Emmanuel Bernard Red Hat
  2. 2. Copyright 2014 Emmanuel Bernard and Red Hat Inc.
  3. 3. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. What’s in it for me • Better understand the usage of NoSQL • Understand the merits of Object mappers • Think about overall data in its IT infra
  4. 4. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Emmanuel Bernard • Data dude at Red Hat (middleware) • Hibernate projects, Infinispan, Teiid, Modeshape • JCP and LEADS european projects • Podcasts • The rest is at http://emmanuelbernard.com • @emmanuelbernard
  5. 5. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 1 Once you have chosen a NoSQL product you don't need any other
  6. 6. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Use case Create a scalable web shop handling billions of products. Used by billions of people. Analyse link and patterns between products and people. Store gazzillions of comments (last is more meaningful) Make products searchable in an awesome way. Just another web property in China ;)
  7. 7. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Storing products • Document store • Self-describing schema • Nested structure • Flexible { "_id" : "1234-5678-0123-4567", "title": "iPhone 6", "type": "Phone", "brand": "Apple", "desc": "iBendit", "options": [ { "memory": "16 GB", "price": "$600", "color": "Space pink" }, { "memory": "128 GB", "price": "$999", "color": "Champagne" } ] }
  8. 8. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Find patterns • Graph database • Good at connections • (Single node instance)
  9. 9. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Store comments • Cassandra-like • Linear scalability • Awesome for time series Comments: iPhone 6 20140923000102 { "text": "Love it", "star": 5 "author": "John" } 20140923001002 { "text": "It bent!", "star": 1 "author": "Kelly" } 20140923010643 { "text": "Like it", "star": 4 "author": "Victor" } ... ...
  10. 10. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Other use cases • Super fast pre computed recommendations • Key/value store • Query engine • Full-text search engine
  11. 11. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Choosing a NoSQL engine • Based on the read / write characteristics of use case • Speed, query-ability, CAP theorem • Distribution topology • What happens for another use case? • Duplicate data • Consistency / reconciliation
  12. 12. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 1 Once you have chosen a NoSQL product you don't need any other Busted
  13. 13. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 3 Entities feel awkward in NoSQL
  14. 14. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Mapping types and properties @Entity public class News { @Id private String id; private String title; @Column(name="desc") private String description; @Temporal(DATE) private Date update; } // Collection: News { "_id" : "1234-5678-0123-4567", "title": "On the merits of NoSQL", "desc": "This paper discusses why ...”, "update": ISODate("2012-07-14T00:00:0.000Z") } ENTITY News id: 1234-5678-0123-4567 title: On the merits of NoSQL desc: This paper discusses why ... update: "2012-07-14T00:00:00Z"
  15. 15. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Embedded Collections of Embedded @Entity public class News { @Id String id; @Embedded NewsPaper paper; @ElementCollection Set<Journalist> authors; }   @Embeddable public class Journalist { String firstname; String lastname; } // Collection: News { "_id" : "1234-5678-0123-4567", "paper": { "name": "NoSQL journal of prophecies", "owner": "Delphy" }, "authors": [ { "firstname": "The", "lastname": "Duke" }, { "firstname": "Larry", "lastname": "Ellison" } ] } ENTITY News id: 1234-5678-0123-4567 EMBEDDED NewsPaper name: NoSQL journal of prophecies owner: Delphy EMBEDDED Journalist name: Joseph Pulitzer EMBEDDED Journalist name: Clark Kent
  16. 16. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Association: duplicate on each side • Mapped to relationships for graph databases! @Entity public class Dog { @Id String name; @ManyToMany Set<Cat> hates; }   @Entity public class Cat { @Id String name; @ManyToMany(mappedBy="hates") Set<Dog> hated; } // Collection: Dog { "_id": "Snoopy", "hates": [ "Garfield" ] } // Collection: Cat { "_id": "Garfield", "hated": [ "Snoopy" ] }
  17. 17. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Many more details for a natural mapping • Embedded id • Identifier generators • Big associations • Native optimistic version field • Label vs properties in Neo4J • More query-friendly patterns (graph)
  18. 18. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. NoSQL is schemaless • Well no - at least not without schema • Schema • Evolutive • (Self describing) • Low (upfront) cost on the dev team • Java is schemaful • Classes
  19. 19. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Handling evolution • Separate the app constructs from the DB constructs • n apps - 1 DB • Evolve app without DB field migration • JPA logical vs physical model • @Entity / @Table | @OneToMany / @JoinTable • Declarative converters • Tuple <-> Database model • Structural migration
  20. 20. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Handling unknown properties • Unknown to the application • Carried over transparently by the persistence engine • Tuple level applies change operations • Explicit API to access them @Entity public class Profile {   @Id private long id; private String name;   @AdditionalProperties private Map<String, Object> additionalProperties; } Profile profile = entityManager.find(Profile.class, id); profile.getAdditionalProperties().put("instagram nick", “robert_doisneau");
  21. 21. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 3 Entities feel awkward in NoSQLBeing busted
  22. 22. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 4 You need native APIs for real NoSQL use
  23. 23. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 4 You need native APIs for real NoSQL use Part I: CRUD
  24. 24. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. OO stuff • Domain model description • JPA covers it • What about NoSQL specific tweaks and options?
  25. 25. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Depends on the option scope… • Declarative options: natural way to attach them • Globally, per entity, per property • Also for a specific session • Possible options • R+W > N quorum • Custom mapping structure @Entity @WriteConcern(JOURNALED) @ReadPreference(PRIMARY_PREFERRED) public class Zoo { }
  26. 26. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 4 You need native APIs for real NoSQL use Part I: CRUD Maybe Maybe not
  27. 27. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 5 You need native APIs for real NoSQL use Part II: Query language
  28. 28. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. JP-QL as the query language • Queries over object properties and associations • JP-QL to native query • Split query into individual predicates • Translate into native • JP-QL to inverted index • Hibernate Search QL / Lucene query • No joins on all arbitrary associations
  29. 29. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Specific constructs? • Full-text search • Geolocation search • JP-QL has functions • Hierarchical queries on arbitrary levels select news from News news where within( news.event.location, 2km, Paris)
  30. 30. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Native QL to managed objects • Taking the problem upside down • Use native query • Map the result as managed objects • Best of both worlds • Cost is adherence to the specific NoSQL backend @NamedNativeQuery( name="poems-by-author", query="MATCH ( n:Poem { author:{author_param} } ) return n", resultClass=Poem.class )
  31. 31. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 5 You need native APIs for real NoSQL use Part II: Query language Possibly true But that’s OK
  32. 32. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 6 Object mappers don’t add enough value for NoSQL
  33. 33. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Abstraction • What does abstraction brings? • Automated better • More knowledge • Of the data and links • Of the changes News id: 1234-5678-0123-4567 title: On the merits of NoSQL description: This paper discuss why ... NewsPaper id: 1234-5678-0123-4566 name: NoSQL journal of prophecies owner: Delphy Journalist id: 1234-5678-0123-4565 name: Joseph Pulitzer Journalist id: 1234-5678-0123-4564 name: Clark Kent Change title in News 1234-5678-0123-4567 by On the merits of NoSQL Create new Journalist named Clark Kent Add Journalist 1234-5678-0123-4564 to News 1234-5678-0123-4567
  34. 34. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Natural mappings • Offer natural data mapping • Specific for each storage • Declarative -> less bugs • Storage structure • Optimistic locking • Type conversion • … ?
  35. 35. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Denormalization engine • Object and relations are the canonical form • Can create “materialized views” aka queries • Consistency handled by the ORM engine • Declarative approach • Less bug, or at least not yours
  36. 36. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Polyglot persistence • Different queries, different backends • Domain model as the canonical logical structure News id: 1234-5678-0123-4567 title: On the merits of NoSQL description: This paper discuss why ... NewsPaper id: 1234-5678-0123-4566 name: NoSQL journal of prophecies owner: Delphy Journalist id: 1234-5678-0123-4565 name: Joseph Pulitzer Journalist id: 1234-5678-0123-4564 name: Clark Kent NewsPaper: Delphy 20140923 { "title": "Life on Mars" "desc": "..." } 20140925 { "title": "Debunking climate change" "desc": "..." } 20140928 { "title": "On the merits of NoSQL" "desc": "..." } ... ... // Collection: News { "_id" : "1234-5678-0123-4567", "title": "On the merits of NoSQL", "desc": "This paper discusses why ...", "paper": { "name": "NoSQL journal of prophecies", "owner": "Delphy" }, "authors": [ { "firstname": "Joseph", "lastname": "Pulitzer" }, { "firstname": "Clark", "lastname": "Kent" } ] } For single news detail For news as "time series"
  37. 37. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Feed event based logics • Data validation (Hibernate Validator) • Full-text search (Hibernate Search) • Data historization (Hibernate ORM Envers) • Compensation API • Your own event based magic
  38. 38. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Myth 6 Object mappers don’t add enough value for NoSQL Busted
  39. 39. Copyright 2014 Emmanuel Bernard and Red Hat Inc. Object mappers for NoSQL Heresy or Awesomeness
  40. 40. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Where is Hibernate OGM at • Increased adoption • Backends • MongoDB, Infinispan, EhCache, Neo4J • JP-QL / native query • Per NoSQL Options • Mapping of entities and associations • Future • <Your feature> • Compensation API • Cassandra, CouchDB • Multi-denormalization • Multi-backends
  41. 41. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc. Q&A • Try Hibernate OGM • Contribute (feedback, doc, code, hatred/love) • http://hibernate.org/ogm/ + =

×