0

I've been playing with the MongoInputFormat that allows having all documents in a MongoDB collection put through a MapReduce job written in Hadoop.

As you can see in the provided examples (this, this and this) the type the document is in that is provided to the mapper is a BSONObject (an Interface in Java).

Now I also like Morphia very much which allows mapping the raw data from MongoDB into POJOs that are much easier to use.

Since I can only get a BSONObject as input I thought about using the method described at the bottom of this page of the Morphia wiki:

BlogEntry blogEntry = morphia.fromDBObject(BlogEntry.class, blogEntryDbObj);

My problem is that this method requires a DBObject instead of a BSONObject. A DBObject is really:

public interface DBObject extends BSONObject

So as you can see I cannot simply cast from BSONObject to DBObject and call the provided method.

How do I handle this the best way?

Niels Basjes
  • 10,424
  • 9
  • 50
  • 66

2 Answers2

1

You'll notice that the BSONObject and DBObject interfaces are very similar. Just because a converson doesn't exist doesn't mean it's not easy to create a trivial one:

class BSONDBObject extends BasicBSONObject implements DBObject {
    boolean ispartial = false;
    public BSONDBObject(BSONObject source) {
        this.putAll(source);
    }
    public boolean isPartialObject() {
        return ispartial;
    }
    public void markAsPartialObject() {
        this.ispartial = true;
    }
}

Now, you just need to

BSONObject bson; // Filled by the MongoInputFormat
BSONBDObject dbo = BSONDBObject(bson);
EntityCache = entityCache = new DefaultEntityCache();
BlogEntry blogEntry = morphia().fromDBObject(BlogEntry.class, dbo, entityCache);
Dave
  • 10,964
  • 3
  • 32
  • 54
-1

I found a solution that works for me:

  • First make it into a JSON text string
  • Parse this into a DBObject
  • Map that using Morphia to a useful instance.

Effectively I now have something like this:

BSONObject bson; // Filled by the MongoInputFormat

EntityCache = entityCache = new DefaultEntityCache();
String json = JSON.serialize(bson)    
DBObject dbObject = (DBObject) JSON.parse(json);
BlogEntry blogEntry = morphia().fromDBObject(BlogEntry.class, dbObject, entityCache);
Niels Basjes
  • 10,424
  • 9
  • 50
  • 66
  • -1: This is not the way to do this. There is no need to serialize and unserialize your data just to perform such a trivial conversion – Dave Dec 09 '13 at 01:00
  • Your answer is better, but it is also more than a year AFTER the point in time when I needed it. I think this downvote is not very nice of you. – Niels Basjes Dec 09 '13 at 08:49
  • The reason that questions persist is so that they can be helpful to *others*. This is a really bad way of doing this, and I want to indicate that to future viewers. I only found this question because I needed the answer for something I'm working on now. – Dave Dec 09 '13 at 09:07