Professional Documents
Culture Documents
2
Agenda
• Why NoSQL?
• Overview of NoSQL databases
• Introduction to Spring Data
• Database APIs
- MongoDB
- HyperSQL
- Neo4J
3
Relational databases are great
4
The trouble with relational databases
5
NoSQL databases have emerged…
6
… but there are few commonalities
7
NoSQL databases have emerged…
8
Agenda
• Why NoSQL?
• Overview of NoSQL databases
• Introduction to Spring Data
• Database APIs
- MongoDB
- HyperSQL
- Neo4J
10
Redis
• Advanced key-value store
- Think memcached on steroids (the good kind)
- Values can be binary strings, Lists, Sets, Ordered Sets, Hash maps, ..
- Operations for each data type, e.g. appending to a list, adding to a
set, retrieving a slice of a list, …
- Provides pub/sub-based messaging K1 V1
• Very fast: K2 V2
- In-memory operations
- ~100K operations/second on entry-level hardware K3 V2
• Persistent
- Periodic snapshots of memory OR append commands to log file
- Limits are size of keys retained in memory.
• Has “transactions”
- Commands can be batched and executed atomically
11
Redis use cases
• Use in conjunction with another database as the SOR
• Drop-in replacement for Memcached
- Session state
- Cache of data retrieved from SOR
- Denormalized datastore for high-performance queries
• Hit counts using INCR command
• Randomly selecting an item – SRANDMEMBER
• Queuing – Lists with LPOP, RPUSH, ….
• High score tables – Sorted sets
14
vFabric Gemfire - Elastic data fabric
• High performance data grid
• Enhanced parallel disk persistence
• Non Disruptive up/down scalability
• Session state
- Cache of data retrieved from SOR
- Denormalized datastore for high-performance queries
• Heterogenous data sharing
• Java
• .net
• C++
• Co-located Transactions
14
Gemfire - Use Cases
14
Neo4j
15
Neo4j Data Model
16
Neo4j Use Cases
• Use Cases
- Anything social
- Cloud/Network management, i.e. tracking/managing physical/virtual resources
- Any kind of geospatial data
- Master data management
- Bioinformatics
- Fraud detection
- Metadata management
• Who is using it?
- StudiVZ (the largest social network in Europe)
- Fanbox
- The Swedish military
- And big organizations in datacom, intelligence, and finance that wish to remain anonymous
19
MongoDB
• Document-oriented database
- JSON-style documents: Lists, Maps, primitives
- Documents organized into collections (~table)
• Full or partial document updates
- Transactional update in place on one document
- Atomic Modifiers
• Rich query language for dynamic queries
• Index support – secondary and compound
• GridFS for efficiently storing large files
• Map/Reduce
20
Data Model = Binary JSON documents
{
"name" : "Ajanta",
One document
"type" : "Indian",
=
"serviceArea" : [
"94619", one DDD aggregate
"94618"
],
"openingHours" : [
{
• Sequence of bytes on disk = fast I/O
- No joins/seeks
"dayOfWeek" : Monday,
"open" : 1730,
- In-place updates when possible => no index updates
"close" : 2130 • Transaction = update of single document
}
],
"_id" : ObjectId("4bddc2f49d1505567c6220a0")
}
21
MongoDB query by example
• Find a restaurant that serves the 94619 zip code and is open at 6pm on a Monday
{
serviceArea:"94619",
openingHours: {
$elemMatch : {
"dayOfWeek" : "Monday",
"open": {$lte: 1800},
"close": {$gte: 1800}
}
}
} DBCursor cursor = collection.find(qbeObject);
while (cursor.hasNext()) {
DBObject o = cursor.next();
…
}
23
MongoDB use cases
• Use cases
- Real-time analytics
- Content management systems
- Single document partial update
- Caching
- High volume writes
• Who is using it?
- Shutterfly, Foursquare
- Bit.ly Intuit
- SourceForge, NY Times
- GILT Groupe, Evite,
- SugarCRM
25
Other NoSQL databases
• SimpleDB – “key-value”
• Cassandra – column oriented database
• CouchDB – document-oriented
• Membase – key-value
• Riak – key-value + links
• Hbase – column-oriented…
26
Agenda
• Why NoSQL?
• Overview of NoSQL databases
• Introduction to Spring Data
• Database APIs
- MongoDB
- HyperSQL
- Neo4J
27
NoSQL Java APIs
Database Libraries
Redis Jedis, JRedis, JDBC-Redis, RJC
Neo4j Vendor-provided
MongoDB Vendor-provided Java driver
Gemfire Pure Java map API, Spring-Gemfire templates
But
• Usage patterns
• Tedious configuration
• Repetitive code
• Error prone code
•…
28
Spring Data Project Goals
30
Spring Data sub-projects
http://www.springsource.org/spring-data
31
Many entry points to use
32
Cloud Foundry supports NoSQL
33
Agenda
• Why NoSQL?
• Overview of NoSQL databases
• Introduction to Spring Data
• Database APIs
- MongoDB
- HyperSQL
- Neo4J
34
Three databases for today’s talk
Document database
Relational database
Graph database
35
Three persistence strategies for today’s talk
36
Spring Template Patterns
• Resource Management
• Callback methods
• Exception Translation
• Simple Query API
37
Repository Implementation
38
• Also known as HSQLDB or Hypersonic SQL
• Relational Database
• Table oriented data model
• SQL used for for queries
• … you know the rest…
39
Spring Data Repository Support
40
• Type safe queries for multiple backends including JPA, SQL and MongoDB in Java
• Generate Query classes using Java APT
• Code completion in IDE
• Domain types and properties can be referenced safely
• Adopts better to refactoring changes in domain types
http://www.querydsl.com
41
QueryDSL
• Repository Support
• Spring Data JPA
• Spring data Mongo
• Spring Data JDBC extensions
• QueryDslJdbcTemplate
42
Spring Data Neo4J
43
Spring Data Graph Neo4J cross-store
44
A cross-store scenario ...
You have a traditional web app using JPA to persist data to a relational
database ...
45
JPA Data Model
46
8/3/11 Slide 46
Cross-Store Data Model
47