Created
September 2, 2017 12:28
-
-
Save abyrd/683a2a2de4dfedb83a7436664dcb093c to your computer and use it in GitHub Desktop.
Example code for serializing and deserializing plain Java objects into MongoDB with Morphia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.conveyal.datatools.common.persistence; | |
import com.conveyal.datatools.manager.DataManager; | |
import com.conveyal.datatools.manager.models.FeedSource; | |
import com.conveyal.datatools.manager.models.Project; | |
import com.mongodb.BasicDBObject; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.MongoClient; | |
import com.mongodb.MongoClientOptions; | |
import com.mongodb.client.MongoCollection; | |
import com.mongodb.client.MongoDatabase; | |
import org.bson.Document; | |
import org.bson.codecs.configuration.CodecRegistry; | |
import org.bson.codecs.pojo.PojoCodecProvider; | |
import org.mongodb.morphia.Datastore; | |
import org.mongodb.morphia.Morphia; | |
import org.mongodb.morphia.query.Query; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import static org.bson.codecs.configuration.CodecRegistries.fromProviders; | |
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries; | |
/** | |
* Test out the Morphia Java -> Mongo library. | |
* | |
*/ | |
public class MorphiaMain { | |
public static final Logger LOG = LoggerFactory.getLogger(MorphiaMain.class); | |
public static void main (String[] args) { | |
final Morphia morphia = new Morphia(); | |
try { | |
DataManager.loadConfig(args); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
// Register the classes we want to be able to serialize with Morphia. | |
morphia.mapPackage("com.conveyal.datatools.manager.models"); | |
// create the Datastore connecting to the default port on the local host | |
final Datastore datastore = morphia.createDatastore(new MongoClient(), "data_tools_test"); | |
datastore.ensureIndexes(); | |
Project project = new Project(); | |
project.feedSources = new ArrayList<>(); | |
FeedSource feedSource = new FeedSource(); | |
feedSource.projectId = "HELLO!"; | |
project.feedSources.add(feedSource); | |
datastore.save(project); | |
Project deserializedProject = | |
datastore.createQuery(Project.class).field("id").equal(project.id).get(); | |
LOG.info("Deserialized object has ID: " + deserializedProject.id); | |
LOG.info("Try to fetch the JSON document directly from Mongo using the newer API objects."); | |
for (Document document : new MongoClient().getDatabase("data_tools_test").getCollection("Project").find()) { | |
LOG.info(document.toJson()); | |
} | |
LOG.info("Now fetch the (older deprecated) DB object used by Morphia."); | |
DB mongoDb = datastore.getDB(); | |
DBCollection collection = mongoDb.getCollection("Project"); | |
collection.find().forEach(dbo -> { | |
BasicDBObject basicDBObject = (BasicDBObject)dbo; | |
LOG.info(basicDBObject.toJson()); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment