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 :)
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. 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. 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. 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. 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. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Find patterns
• Graph database
• Good at connections
• (Single node instance)
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. 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. 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. 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
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. 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. 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. 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. 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. 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. 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");
23. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 4
You need native APIs for real NoSQL use
Part I: CRUD
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. 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. 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. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 5
You need native APIs for real NoSQL use
Part II: Query language
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. 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. 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. 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
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. 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. 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. 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. 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. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Myth 6
Object mappers don’t add enough value
for NoSQL
Busted
39. Copyright 2014 Emmanuel Bernard and Red Hat Inc.
Object mappers for NoSQL
Heresy or Awesomeness
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. Copyright 2014-2015 Emmanuel Bernard and Red Hat Inc.
Q&A
• Try Hibernate OGM
• Contribute (feedback, doc, code, hatred/love)
• http://hibernate.org/ogm/
+ =