Using Jackson with MongoDB         08/08/2012        Ramesh Pidikiti
Background• Mongo Stores documents in JSON format• Jackson plays roles in serializing and de-  serializing java/scala obje...
Options• What are my options for serialization and  deserialization  – Manual  – Jackson  – Morphia• Which one should I use?
Options Continued• One solution doesn’t fit all• Jackson provides following advantages   – Faster to development compared ...
•   Saving object using Jackson    TestObjectWithDate obj= new TestObjectWithDate(1, "String 1",1, new Date());    DBObjec...
• Retrieve object using Jackson    DBObject queryObj = coll.findOne();    TestObjectWithDate afterSave = mapper.readValue(...
Challenges• Mongo stores dates in bson format so we  need custom serializer and deserializer for  date data type  Regular ...
public class BsonDateSerializer extends StdSerializer<Date> {    public BsonDateSerializer() {      super(Date.class);    ...
public class BsonDateDeserializer extends JsonDeserializer<Date> {    public BsonDateDeserializer() {    }    public Date ...
Benchmarks     •    Mac OS X, 10.7.3, 2.4 GHz, i7, 8GB, 1333 MHz, DDR3 machine250000                                      ...
Benchmarks• Manual  –   Full, objects: 100000, rate: 6461.2 / sec, throughput: 47.15 mb/sec  –   one-level, objects: 10000...
Upcoming SlideShare
Loading in …5
×

Jackson with mongo db

6,540 views

Published on

Quick overview on using jackson with mongodb

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
6,540
On SlideShare
0
From Embeds
0
Number of Embeds
52
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Jackson with mongo db

  1. 1. Using Jackson with MongoDB 08/08/2012 Ramesh Pidikiti
  2. 2. Background• Mongo Stores documents in JSON format• Jackson plays roles in serializing and de- serializing java/scala objects
  3. 3. Options• What are my options for serialization and deserialization – Manual – Jackson – Morphia• Which one should I use?
  4. 4. Options Continued• One solution doesn’t fit all• Jackson provides following advantages – Faster to development compared to Manual technique – Unified annotations across all layers compared to Morphia – Handles the types more friendlier way, example: Changing data type from long to int is transparently handled by jackson compared to manual technique – We can also have different mappers between DB and Jersey layers (Example: Date format in REST calls can be different from date formats in mongo)
  5. 5. • Saving object using Jackson TestObjectWithDate obj= new TestObjectWithDate(1, "String 1",1, new Date()); DBObject dbo = (DBObject)JSON.parse(mapper.writeValueAsString(obj)); coll.save(dbo);• Saving object using manual mapping (this is top level object, if more levels in object graph , this code can definitely much more lengthier) TestObjectWithDate obj2 = new TestObjectWithDate(1, "String 1",1, new Date()); BasicDBObject dbo2 = new BasicDBObject(); dbo.put("_id", obj.getId()); dbo.put("stringVal", obj.getStringVal());• dbo.put("longVal", obj.getLongVal()); dbo.put("dateVal", obj.getDateVal()); coll.save(dbo2);
  6. 6. • Retrieve object using Jackson DBObject queryObj = coll.findOne(); TestObjectWithDate afterSave = mapper.readValue(queryObj.toString(),TestObjectWithDate.class);• Retrieve object using manual mapping BasicDBObject dbObj = (BasicDBObject)coll.findOne(); TestObjectWithDate obj= new TestObjectWithDate(); obj.setId(dbObj.getLong("_id")); obj.setStringVal(dbObj.getString("stringVal")); obj.setLongVal(dbObj.getLong("longVal")); obj.setDateVal((Date)dbo.get("dateVal"));
  7. 7. Challenges• Mongo stores dates in bson format so we need custom serializer and deserializer for date data type Regular Date output: {dt: "2012-08-03T14:57:40.813Z"} For mongo to store using date data type we need following format {dt:{“$date “:"2012-08-03T14:57:40.813Z"}}
  8. 8. public class BsonDateSerializer extends StdSerializer<Date> { public BsonDateSerializer() { super(Date.class); } @Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); serializeContents(value, jgen, provider); jgen.writeEndObject(); } private void serializeContents(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeFieldName("$date"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ"); jgen.writeString(dateFormat.format(value)); }}
  9. 9. public class BsonDateDeserializer extends JsonDeserializer<Date> { public BsonDateDeserializer() { } public Date deserialize(JsonParser jp, com.fasterxml.jackson.databind.DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode tree = jp.readValueAsTree(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ"); try{ return dateFormat.parse(tree.get("$date").textValue()); }catch(Throwable t){ throw new IOException(t.getMessage(), t); } }}How fast is Jackson compared to other options?
  10. 10. Benchmarks • Mac OS X, 10.7.3, 2.4 GHz, i7, 8GB, 1333 MHz, DDR3 machine250000 238095200000 170357 173010 15822715000010000050000 38183 26205 27624 28296 6461 3792 3925 4379 0 Manual Jackson Jackson 2 Morphia Manual Jackson Jackson 2 Morphia Manual Jackson Jackson 2 Morphia Full Object One level Top level
  11. 11. Benchmarks• Manual – Full, objects: 100000, rate: 6461.2 / sec, throughput: 47.15 mb/sec – one-level, objects: 100000, rate: 38182.51 / sec, throughput: 27.5 mb/sec – top-level, objects: 100000, rate: 238095.24 / sec, throughput: 20.61 mb/sec• Jackson – Full, objects: 100000, , rate: 3792.04, throughput: 27.67 mb/sec – one-level, objects: 100000, rate: 26205.45, throughput: 18.87 mb/sec – top-level, objects: 100000, ate: 158227.85, throughput: 13.7 mb/sec• Jackson2 – Full, objects: 100000, rate: 3925.57, throughput: 28.64 mb/sec – one-level, objects: 100000, rate: 27624.31, throughput: 19.89 mb/sec – top-level, objects: 100000, rate: 170357.75, throughput: 14.75 mb/sec• Morphia – Full, objects: 100000, rate: 4379.43, throughput: 31.96 mb/sec – one-level, objects: 100000, rate: 27624.31, throughput: 19.89 mb/sec – top-level, objects: 100000, rate: 173010.38, throughput: 14.98 mb/sec

×