The document discusses using MongoDB as an alternative to a relational database in Java applications. It outlines some of the drawbacks of using an ORM with an RDBMS, such as complexity in the data access layer. MongoDB offers a simpler document model where data is stored in JSON-like documents without a predefined schema. The presentation demonstrates basic CRUD operations in MongoDB from the Java driver and discusses architecture considerations like lack of transactions when using MongoDB. It also provides an example of how MongoDB is deployed in a clustered configuration for the GoodData platform.
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
MongoDB for Java Developers
1. MongoDB
JBoss User Group Brno
dagi@gooddata.com
Thursday 3 May 2012
2. About me
• Roman Pichlík
• GoodData
• CZJUG
• CZ Podcast
• Follow me on Twitter
• @_dagi
Thursday 3 May 2012
3. Agenda
• What’s wrong...
• Demo
• Using MongoDB in Java
• MongoDB deployment in GoodData
• Q&A
Thursday 3 May 2012
4. A typical java app with
RDBMS
Web Layer
Domain
Business Logic Layer
model
Data Access Layer
Thursday 3 May 2012
5. A typical java app with
RDBMS
Web Layer
Domain
Business Logic Layer
model
Data Access Layer
Thursday 3 May 2012
6. A typical java app with
RDBMS
Web Layer
Domain
Business Logic Layer
model
am
eta
dat
Data Access Layer
a fo
rm
app
ing
to
RD
BM
S sc
hem
a
Thursday 3 May 2012
7. A typical java app with
RDBMS
Web Layer
Domain
Business Logic Layer
model
am
eta
dat
Data Access Layer
a fo
rm
app al
ing e lation
to b ject/R sa
RD ic for O ice ver
BM k mag on and v
S sc
hem a blac versi
a con
Thursday 3 May 2012
8. A typical java app with
RDBMS
Web Layer
Domain
Business Logic Layer
model
am
eta
dat
Data Access Layer
a fo
rm
app al
ing e lation
to b ject/R sa
RD ic for O ice ver
BM k mag on and v
S sc
hem a blac versi
a con
and transactions...
Thursday 3 May 2012
9. Data Access Layer
Thursday 3 May 2012
It’s not only complicated on this picture -> required 3rd party libraries on classpath
10. Data Access Layer
CRUD interface
Thursday 3 May 2012
It’s not only complicated on this picture -> required 3rd party libraries on classpath
11. Data Access Layer
CRUD interface
Persistence context logic (queries, merge...)
Thursday 3 May 2012
It’s not only complicated on this picture -> required 3rd party libraries on classpath
12. Data Access Layer
CRUD interface
Persistence context logic (queries, merge...)
Spring ORM Bridge
Thursday 3 May 2012
It’s not only complicated on this picture -> required 3rd party libraries on classpath
13. Data Access Layer
CRUD interface
Persistence context logic (queries, merge...)
Spring ORM Bridge
ORM framework (Hibernate/JPA)
Thursday 3 May 2012
It’s not only complicated on this picture -> required 3rd party libraries on classpath
14. Data Access Layer
CRUD interface
Persistence context logic (queries, merge...)
Why the hell it’s so complicated?!
Spring ORM Bridge
ORM framework (Hibernate/JPA)
Thursday 3 May 2012
It’s not only complicated on this picture -> required 3rd party libraries on classpath
15. Abstraction faux pas
• We get used to live/think in Object world
• We store data in RDBMS world
• We build a bridge between Object/
RDBMS worlds
• The bridge (ORM) is very complicated
Thursday 3 May 2012
How many of you know patterns like Open Session in View, DTO?
How many of you know the difference between first and second level cache?
How many of you know the difference between Session#load and Session#get method?
19. Data organization
RDBMS
table
Fruit
id
weight
color
discriminator
country
curvature
Thursday 3 May 2012
document - JSON
schemaless (documents, fields)
20. Data organization
MongoDB RDBMS
collection table
Fruit Fruit
Document id
weight
color
discriminator
country
curvature
Thursday 3 May 2012
document - JSON
schemaless (documents, fields)
21. Data organization
MongoDB RDBMS
collection table
Fruit Fruit
Document id
weight
color
discriminator
country
curvature
Thursday 3 May 2012
document - JSON
schemaless (documents, fields)
22. Data organization
MongoDB RDBMS
collection table
Fruit Fruit
Document id
weight
color
discriminator
country
curvature
id discriminator color weight country curvature
row 1 apple red 10 cz null
2 banana null 5.1 br 3
Thursday 3 May 2012
document - JSON
schemaless (documents, fields)
23. Data organization
MongoDB RDBMS
collection table
Fruit Fruit
Document id
weight
document
color
{ discriminator
"_id":"1", country
"apple":{ curvature
"weight":10,
"country":"cz",
"color":"red" id discriminator color weight country curvature
}
} row 1 apple red 10 cz null
2 banana null 5.1 br 3
{
"_id":"2",
"banana":{
"weight":5.1,
"country":"br",
"curvature":3
}
}
Thursday 3 May 2012
document - JSON
schemaless (documents, fields)
24. Data organization
MongoDB RDBMS
collection table
Fruit Fruit
Document id
weight
document
color
{ discriminator
"_id":"1", country
"apple":{ curvature
"weight":10,
"country":"cz",
"color":"red" id discriminator color weight country curvature
}
} row 1 apple red 10 cz null
2 banana null 5.1 br 3
{
"_id":"2",
"banana":{
"weight":5.1,
"country":"br",
"curvature":3
}
}
Thursday 3 May 2012
document - JSON
schemaless (documents, fields)
25. Data organization
MongoDB RDBMS
collection table
Fruit Fruit
Document id
weight
document
color
{ discriminator
"_id":"1", country
"apple":{ curvature
"weight":10,
"country":"cz",
"color":"red" id discriminator color weight country curvature
}
} row 1 apple red 10 cz null
2 banana null 5.1 br 3
{
"_id":"2", column
"banana":{
"weight":5.1, field
"country":"br",
"curvature":3
}
}
Thursday 3 May 2012
document - JSON
schemaless (documents, fields)
28. Using MongoDB in Java
MongoDB Java Application
BSON Web Layer
Domain
JSON document Business Logic Layer
{ model
"_id":"...",
"apple":{
Data Access Layer
"weight":10,
"country":"cz",
"color":"red"
BSON API Auth.
}
}
MongoDB driver
Failover Connectivity
Thursday 3 May 2012
29. Using MongoDB in Java
Mongo m = new Mongo("localhost" , 27017 );
DB db = m.getDB( "fruitdb" );
DBCollection coll = db.getCollection("fruit");
BasicDBObject doc = new BasicDBObject();
BasicDBObject apple = new BasicDBObject();
apple.put("weight", 10);
apple.put("country", "cz");
apple.put("color", "red");
doc.put("apple", apple);
fruit.insert(doc);
Thursday 3 May 2012
30. Sweet MongoDB
• Thin Data Access Layer
• No magic behind the
scene
• Flexible
• schemaless
• new document types
• new collections
• No Alter DDL
Thursday 3 May 2012
31. Architecture impact
• No transactions
• only atomic update on document level
• atomic FindAndUpdate operation
• No constraints
• application logic handles data
inconsistency
• Self sufficient documents over Joins
Thursday 3 May 2012
• redundancy
32. MongoDB cluster in GoodData
Sync. write, Master + Slave
• GoodData platform
No slave reads
hosted on Amazon WS Client
• 3-node
• Journal enabled Master
• EBS used for data
replication
• backup/restore
Slave Slave
• Application specific
backup/restore
Thursday 3 May 2012
* Master elected automatically on start or when the previous one goes down
* Master writes to journal -> Ops log replicated to slaves
33. Thank you
• Join us and work with MongoDB ;-)
• http://www.gooddata.com/about/careers
• Q&A
Thursday 3 May 2012