11
Using Jackson with MongoDB 08/08/2012 Ramesh Pidikiti

Jackson with mongo db

Embed Size (px)

DESCRIPTION

Quick overview on using jackson with mongodb

Citation preview

Page 1: Jackson with mongo db

Using Jackson with MongoDB

08/08/2012Ramesh Pidikiti

Page 2: Jackson with mongo db

Background

• Mongo Stores documents in JSON format• Jackson plays roles in serializing and de-

serializing java/scala objects

Page 3: Jackson with mongo db

Options

• What are my options for serialization and deserialization– Manual – Jackson– Morphia

• Which one should I use?

Page 4: Jackson with mongo db

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)

Page 5: Jackson with mongo db

• 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);

Page 6: Jackson with mongo db

• Retrieve object using Jackson

• 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"));

DBObject queryObj = coll.findOne(); TestObjectWithDate afterSave = mapper.readValue(queryObj.toString(),TestObjectWithDate.class);

Page 7: Jackson with mongo db

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"}}

Page 8: Jackson with mongo db

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-dd'T'HH:mm:ss'Z'"); jgen.writeString(dateFormat.format(value)); }

}

Page 9: Jackson with mongo db

How fast is Jackson compared to other options?

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-dd'T'HH:mm:ss'Z'"); try{ return dateFormat.parse(tree.get("$date").textValue()); }catch(Throwable t){ throw new IOException(t.getMessage(), t); } }

}

Page 10: Jackson with mongo db

Benchmarks

• Mac OS X, 10.7.3, 2.4 GHz, i7, 8GB, 1333 MHz, DDR3 machine

Manual Jackson Jackson 2 Morphia Manual Jackson Jackson 2 Morphia Manual Jackson Jackson 2 MorphiaFull Object One level Top level

0

50000

100000

150000

200000

250000

6461 3792 3925 4379

38183

26205 27624 28296

238095

158227

170357 173010

Page 11: Jackson with mongo db

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