java development with mongodb

22
Java Development Go.. Go… JVM!

Upload: scott-hernandez

Post on 01-Nov-2014

32 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Java Development with MongoDB

Java Development

Go.. Go… JVM!

Page 2: Java Development with MongoDB

Library Choices Raw MongoDB Driver

Map<String, Object> view of objects Rough but dynamic

Morphia (type-safe mapper) POJOs Annotation based (similar to JPA) Syntactic sugar and helpers

Others Code generators, other jvm languages

Page 3: Java Development with MongoDB

MongoDB Java Driver BSON Package

Types Encode/Decode DBObject (Map<String, Object>)

Nested Maps Directly encoded to binary format (BSON)

MongoDB Package Mongo DBObject (BasicDBObject/Builder) DB/DBColletion DBQuery/DBCursor

Page 4: Java Development with MongoDB

BSON Package Types

int and long Array/ArrayList String byte[] – binData Double (IEEE 754 FP) Date (secs since epoch) Null Boolean JavaScript String Regex

Page 5: Java Development with MongoDB

MongoDB Package Mongo

Connection, ThreadSafe WriteConcern*

DB Auth, Collections getLastError() Command(), eval() RequestStart/Done

DBCollection Insert/Save/Find/Remove/Update/FindAndModify ensureIndex

Page 6: Java Development with MongoDB

Simple ExampleDBCollection coll = new Mongo().getDB(“test”);coll.save(

new BasicDBObjectBuilder(“name”, “scott”).append(“sex”, “male”).append(“height”, 178)).get());

Page 7: Java Development with MongoDB

Simple Example, AgainDBCollection coll = new Mongo().getDB(“test”);

Map<String, Object> fields = new …fields.add(“name”, “scott”); fields.add(“sex”, “male”);fields.add(“height”, 178);

coll.insert(new BasicDBObject(fields));

Page 8: Java Development with MongoDB

DBObject <-> (B/J)SON{name:”scott”, sex:“male”, height: 178 }

new BasicDBObjectBuilder().append(“name”, “scott”) .append(“sex”, “male”) .append(“height”, 178) .get();

String name = (String)dbObj.get(“name”);

Page 9: Java Development with MongoDB

JSON.parse(…)DBObject dbObj = JSON.parse(“

{‘name’:’scott’, ‘height’: 178, ‘sex’:’male’}”);

Page 10: Java Development with MongoDB

ListsDBObject dbObj = JSON.parse(“

{‘name’:’scott’, height: 178, sex:’male’}”);

List<String> activities = new …activities.add(“mongodb”);activities.add(“java”);dbObj.put(“activities”, activities);

{…, activities: [‘mongodb’, ‘java’]}

Page 11: Java Development with MongoDB

Maps of Maps Can represent object graph/tree Always keyed off String (field)

Page 12: Java Development with MongoDB

Morphia: MongoDB Mapper Maps POJO Type-safe Access Patterns: DAO/Datastore/??? Data Types Performs well JPA like Many concepts came from Objectify (GAE)

Page 13: Java Development with MongoDB

Annotations @Entity(“collectionName”) @Id @Transient (not transient) @Indexed(…) @Property(“fieldAlias”) @AlsoLoad({aliases}) @Reference @Serialized [@Embedded]

Page 14: Java Development with MongoDB

Lifecycle Events @PrePersist @PreSave @PostPersist @PreLoad @PostLoad

EntityListeners EntityInterceptor

Page 15: Java Development with MongoDB

Basic POJO@Entityclass Person { @Id String name; SexEnum sex; @Indexed Integer height;}

Page 16: Java Development with MongoDB

Datastore Basics get(class, id) find(class, […]) save(entity, […]) delete(query) getCount(query) update/First(query, upOps) findAndModify/Delete(query, upOps)

Page 17: Java Development with MongoDB

Add, Get, DeletePerson me = new Person(“scott”, Sex.Male,

179)

Datastore ds = new Morphia().createDatastore()

ds.save(me);

Person meAgain = ds.get(Person.class, “scott”)

ds.delete(me);

Page 18: Java Development with MongoDB

QueriesDatastore ds = …

Query q = ds.createQuery(Person.class);

q.field(“height”).greaterThan(155).limit(5);

for(Person p : q.fetch()) print(p);

Person me = q.field(“name”).startsWith(“sc”).get();

Page 19: Java Development with MongoDB

UpdateDatastore ds = …Query q = ds.find(Person.class, “name”,

“scott”);UpdateOperation uo =

ds.createUpdateOperations(cls)

uo.set(“city”, “seattle”).set(“lastUpdated”, new Date());

UpdateResults res = ds.update(q, uo);if(res.getUpdatedCount() > 0) //do something?

Page 20: Java Development with MongoDB

Update Operations set(field, val) unset(field)

inc(field, [val]) dec(field)

add(field, val) addAdd(field, vals)

removeFirst/Last(field) removeAll(field, vals)

Page 21: Java Development with MongoDB

Relationships [@Embedded]

Loaded/Saved with Entity Update

@Reference Stored as DBRef(s) Loaded with Entity Not automatically saved

Key<T> (DBRef) Stored as DBRef(s) Just a link, but resolvable by Datastore/Query

Page 22: Java Development with MongoDB

Questions?

Look around…

[email protected]