MongoDB for Java Developers

Preview:

DESCRIPTION

 

Citation preview

MongoDBJBoss User Group Brno

dagi@gooddata.com

Thursday 3 May 2012

About me

• Roman Pichlík

• GoodData

• CZJUG

• CZ Podcast

• Follow me on Twitter

• @_dagi

Thursday 3 May 2012

Agenda

• What’s wrong...

• Demo

• Using MongoDB in Java

• MongoDB deployment in GoodData

• Q&A

Thursday 3 May 2012

A typical java app with RDBMS

Domain model

Data Access Layer

Business Logic Layer

Web Layer

Thursday 3 May 2012

A typical java app with RDBMS

Domain model

Data Access Layer

Business Logic Layer

Web Layer

Thursday 3 May 2012

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

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

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

Data Access Layer

Thursday 3 May 2012

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

Data Access Layer

CRUD interface

Thursday 3 May 2012

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

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

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

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

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

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?

Time for MongoDB

Thursday 3 May 2012

MongoDB

• Document oriented database

• Schema less

• Driver API

• NoSQL but Query friendly

• Open source

• AGPL server, Apache 2.0 driver

Thursday 3 May 2012

Data organization

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Data organizationRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Data organizationMongoDB

FruitDocument

collectionRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

Data organizationMongoDB

FruitDocument

collectionRDBMS

Fruitid

weightcolor

discriminatorcountry

curvature

table

Thursday 3 May 2012

document - JSONschemaless (documents, fields)

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)

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)

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)

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)

Fruit demo :-)

Thursday 3 May 2012

> 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

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

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

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

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

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

Thank you

• Join us and work with MongoDB ;-)

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

• Q&A

Thursday 3 May 2012