33
MongoDB JBoss User Group Brno [email protected] Thursday 3 May 2012

MongoDB for Java Developers

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: MongoDB for Java Developers

MongoDBJBoss User Group Brno

[email protected]

Thursday 3 May 2012

Page 2: MongoDB for Java Developers

About me

• Roman Pichlík

• GoodData

• CZJUG

• CZ Podcast

• Follow me on Twitter

• @_dagi

Thursday 3 May 2012

Page 3: MongoDB for Java Developers

Agenda

• What’s wrong...

• Demo

• Using MongoDB in Java

• MongoDB deployment in GoodData

• Q&A

Thursday 3 May 2012

Page 4: MongoDB for Java Developers

A typical java app with RDBMS

Domain model

Data Access Layer

Business Logic Layer

Web Layer

Thursday 3 May 2012

Page 5: MongoDB for Java Developers

A typical java app with RDBMS

Domain model

Data Access Layer

Business Logic Layer

Web Layer

Thursday 3 May 2012

Page 6: MongoDB for Java Developers

A typical java app with RDBMS

Domain model

Data Access Layer

Business Logic Layer

Web Layer

a metadata for mapping to RDBMS schema

Thursday 3 May 2012

Page 7: MongoDB for Java Developers

A typical java app with RDBMS

Domain model

Data Access Layer

Business Logic Layer

Web Layer

a metadata for mapping to RDBMS schemaa black magic for Object/Relational

conversion and vice versa

Thursday 3 May 2012

Page 8: MongoDB for Java Developers

A typical java app with RDBMS

Domain model

Data Access Layer

Business Logic Layer

Web Layer

a metadata for mapping to RDBMS schemaa black magic for Object/Relational

conversion and vice versa

and transactions...

Thursday 3 May 2012

Page 9: MongoDB for Java Developers

Data Access Layer

Thursday 3 May 2012

It’s not only complicated on this picture -> required 3rd party libraries on classpath

Page 10: MongoDB for Java Developers

Data Access Layer

CRUD interface

Thursday 3 May 2012

It’s not only complicated on this picture -> required 3rd party libraries on classpath

Page 11: MongoDB for Java Developers

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

Page 12: MongoDB for Java Developers

Data Access Layer

CRUD interface

Spring ORM Bridge

Persistence context logic (queries, merge...)

Thursday 3 May 2012

It’s not only complicated on this picture -> required 3rd party libraries on classpath

Page 13: MongoDB for Java Developers

Data Access Layer

CRUD interface

Spring ORM Bridge

Persistence context logic (queries, merge...)

ORM framework (Hibernate/JPA)

Thursday 3 May 2012

It’s not only complicated on this picture -> required 3rd party libraries on classpath

Page 14: MongoDB for Java Developers

Data Access Layer

CRUD interface

Spring ORM Bridge

Persistence context logic (queries, merge...)

ORM framework (Hibernate/JPA)

Why the hell it’s so complicated?!

Thursday 3 May 2012

It’s not only complicated on this picture -> required 3rd party libraries on classpath

Page 15: MongoDB for Java Developers

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?

Page 16: MongoDB for Java Developers

Time for MongoDB

Thursday 3 May 2012

Page 17: MongoDB for Java Developers

MongoDB

• Document oriented database

• Schema less

• Driver API

• NoSQL but Query friendly

• Open source

• AGPL server, Apache 2.0 driver

Thursday 3 May 2012

Page 18: MongoDB for Java Developers

Data organization

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Page 19: MongoDB for Java Developers

Data organizationRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Page 20: MongoDB for Java Developers

Data organizationMongoDB

FruitDocument

collectionRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Page 21: MongoDB for Java Developers

Data organizationMongoDB

FruitDocument

collectionRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Page 22: MongoDB for Java Developers

Data organizationMongoDB

FruitDocument

collectionRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

id discriminator color weight country curvature1 apple red 10 cz null

2 banana null 5.1 br 3row

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Page 23: MongoDB for Java Developers

Data organizationMongoDB

FruitDocument

collectionRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

id discriminator color weight country curvature1 apple red 10 cz null

2 banana null 5.1 br 3row

{   "_id":"1",   "apple":{      "weight":10,      "country":"cz",      "color":"red"   }}

{   "_id":"2",   "banana":{      "weight":5.1,      "country":"br",      "curvature":3   }}

document

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Page 24: MongoDB for Java Developers

Data organizationMongoDB

FruitDocument

collectionRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

id discriminator color weight country curvature1 apple red 10 cz null

2 banana null 5.1 br 3row

{   "_id":"1",   "apple":{      "weight":10,      "country":"cz",      "color":"red"   }}

{   "_id":"2",   "banana":{      "weight":5.1,      "country":"br",      "curvature":3   }}

document

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Page 25: MongoDB for Java Developers

Data organizationMongoDB

FruitDocument

collectionRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

id discriminator color weight country curvature1 apple red 10 cz null

2 banana null 5.1 br 3row

{   "_id":"1",   "apple":{      "weight":10,      "country":"cz",      "color":"red"   }}

{   "_id":"2",   "banana":{      "weight":5.1,      "country":"br",      "curvature":3   }}

document

column

field

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Page 26: MongoDB for Java Developers

Fruit demo :-)

Thursday 3 May 2012

Page 27: MongoDB for Java Developers

> use fruitdbswitched to db fruitdb

> db.createCollection("fruit");{ "ok" : 1 }

> db.fruit.insert({apple:{weight:10, country:"cz", color:"red"}});

> db.fruit.findOne();{ "_id" : ObjectId("4fa0f21591d1fb43c578fa26"), "apple" : { "color" : "red", "country" : "cz", "weight" : 10 } }

> db.fruit.insert({banana:{weight:5.1, country: "br", curvature:3}});

> db.fruit.update({"apple.color":"green"}, {$set:{"apple.color":"red"}});

> db.fruit.remove({"_id":"4fa0f3a791d1fb43c578fa27"})

> db.fruit.find({"apple.color":"red"}); { "_id" : ObjectId("4fa0f21591d1fb43c578fa26"), "apple" : { "color" : "red", "country" : "cz", "weight" : 10 } }

change/create DB

insert document

get first document

update document

find document

remove document

Thursday 3 May 2012

Page 28: MongoDB for Java Developers

Using MongoDB in Java

{   "_id":"...",   "apple":{      "weight":10,      "country":"cz",      "color":"red"   }}

JSON document

BSON

MongoDB Java Application

MongoDB driverFailover

BSON API

Domain model

Data Access Layer

Business Logic Layer

Web Layer

Connectivity

Auth.

Thursday 3 May 2012

Page 29: MongoDB for Java Developers

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

Page 30: MongoDB for Java Developers

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

Page 31: MongoDB for Java Developers

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

• redundancyThursday 3 May 2012

Page 32: MongoDB for Java Developers

MongoDB cluster in GoodData

• GoodData platform hosted on Amazon WS

• 3-node

• Journal enabled

• EBS used for data

• backup/restore

• Application specific backup/restore

Master

Slave Slave

replication

Client

No slave reads

Sync. write, M

aster + Slave

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

Page 33: MongoDB for Java Developers

Thank you

• Join us and work with MongoDB ;-)

• http://www.gooddata.com/about/careers

• Q&A

Thursday 3 May 2012