0
Java Development with MongoDB James Williams Software Engineer, BT/Ribbit
Agenda <ul><ul><li>Java Driver basics </li></ul></ul><ul><ul><ul><li>Making Connections </li></ul></ul></ul><ul><ul><ul><l...
Making a Connection <ul><li>import com.mongodb.Mongo;  </li></ul><ul><li>import com.mongodb.DB;  </li></ul><ul><li>Mongo m...
Working with Collections <ul><ul><li>Getting all collections in the database </li></ul></ul><ul><li>Set<String> colls = db...
Inserting Documents <ul><li>BasicDBObject doc = new BasicDBObject();  </li></ul><ul><li>doc.put(&quot;name&quot;, &quot;Mo...
BasicDBObjectBuilder <ul><ul><li>Utility for building objects </li></ul></ul><ul><ul><li>Can coerce Maps (and possibly JSO...
Document Queries <ul><li>DBObject myDoc = coll.findOne(); // can also use BasicDBObjectBuilderBasicDBObject query = new Ba...
GridFS <ul><ul><li>mechanism for storing files larger than 4MB </li></ul></ul><ul><ul><li>files are chunked allowing fetch...
Saving a file to GridFS <ul><li>def mongo = new Mongo(host) </li></ul><ul><li>def gridfs = new GridFS(mongo.getDB(&quot;db...
Retrieving/Deleting a file <ul><li>def retrieveFile(String filename) { </li></ul><ul><li>     return gridfs.findOne(filena...
Morphia <ul><ul><li>Apache 2 Licensed </li></ul></ul><ul><ul><li>brings Hibernate/JPA paradigms to MongoDB </li></ul></ul>...
Creating a Morphia POJO <ul><li>import com.google.code.morphia.annotations.Entity; </li></ul><ul><li>@Entity(&quot;collect...
Mapping a POJO to a Mongo doc <ul><li>Morphia morphia = ...; </li></ul><ul><li>Mongo mongo = ...; </li></ul><ul><li>DB db ...
Getting a POJO from a Mongo doc <ul><li>Morphia morphia = ...; </li></ul><ul><li>Mongo mongo = ...; </li></ul><ul><li>DB d...
Beyond the Java Language
MongoDB with Groovy <ul><ul><li>Metaprogramming with MongoDB can reduce LOC </li></ul></ul><ul><ul><li>Dynamic finders </l...
Groovy + MongoDB <ul><li>J ava </li></ul><ul><li>BasicDBObject doc = new BasicDBObject(); </li></ul><ul><li>doc.put(&quot;...
Dynamic Finders <ul><ul><li>can build complex queries at runtime </li></ul></ul><ul><ul><li>can even reach into objects fo...
How dynamic finders work <ul><ul><li>Groovy receives the request for the method </li></ul></ul><ul><ul><li>The method is n...
MongoDB Grails Plugin <ul><ul><li>Replaces JDBC layer in Grails applications </li></ul></ul><ul><ul><li>Can use dynamic fi...
Links <ul><ul><li>Personal Blog:  http://jameswilliams.be/blog </li></ul></ul><ul><ul><li>Twitter:  http://twitter.com/ecs...
Upcoming SlideShare
Loading in...5
×

Java Development with MongoDB (James Williams)

6,928

Published on

Published in: Technology

Transcript of "Java Development with MongoDB (James Williams)"

  1. 1. Java Development with MongoDB James Williams Software Engineer, BT/Ribbit
  2. 2. Agenda <ul><ul><li>Java Driver basics </li></ul></ul><ul><ul><ul><li>Making Connections </li></ul></ul></ul><ul><ul><ul><li>Managing Collections </li></ul></ul></ul><ul><ul><ul><li>BasicDBObjectBuilder </li></ul></ul></ul><ul><ul><ul><li>Document Queries </li></ul></ul></ul><ul><ul><ul><li>GridFS </li></ul></ul></ul><ul><ul><li>Morphia </li></ul></ul><ul><ul><li>Beyond the Java language </li></ul></ul><ul><ul><ul><li>Groovy utilities </li></ul></ul></ul><ul><ul><ul><li>Grails plugin </li></ul></ul></ul>
  3. 3. Making a Connection <ul><li>import com.mongodb.Mongo;  </li></ul><ul><li>import com.mongodb.DB;  </li></ul><ul><li>Mongo m = new Mongo();  </li></ul><ul><li>Mongo m = new Mongo( &quot;localhost&quot; );  </li></ul><ul><li>Mongo m = new Mongo( &quot;localhost&quot; , 27017 );  </li></ul><ul><li>DB db = m.getDB( &quot;mydb&quot; ); </li></ul>
  4. 4. Working with Collections <ul><ul><li>Getting all collections in the database </li></ul></ul><ul><li>Set<String> colls = db.getCollectionNames();  </li></ul><ul><li>for (String s : colls) {  </li></ul><ul><li>  System.out.println(s);  </li></ul><ul><li>} </li></ul><ul><ul><li>Getting a single collection </li></ul></ul><ul><li>DBCollection coll = db.getCollection(&quot;testCollection&quot;) </li></ul>
  5. 5. Inserting Documents <ul><li>BasicDBObject doc = new BasicDBObject(); </li></ul><ul><li>doc.put(&quot;name&quot;, &quot;MongoDB&quot;); </li></ul><ul><li>doc.put(&quot;type&quot;, &quot;database&quot;); </li></ul><ul><li>doc.put(&quot;count&quot;, 1); </li></ul><ul><li>BasicDBObject info = new BasicDBObject(); </li></ul><ul><li>info.put(&quot;x&quot;, 203); </li></ul><ul><li>info.put(&quot;y&quot;, 102); </li></ul><ul><li>doc.put(&quot;info&quot;, info); </li></ul><ul><li>coll.insert(doc); </li></ul>
  6. 6. BasicDBObjectBuilder <ul><ul><li>Utility for building objects </li></ul></ul><ul><ul><li>Can coerce Maps (and possibly JSON*) to DBObjects </li></ul></ul><ul><ul><li>  Example: </li></ul></ul><ul><li>BasicDBObjectBuilder.start() </li></ul><ul><li>     .add( &quot;name&quot; , &quot;eliot&quot; ) </li></ul><ul><li>     .add( &quot;number&quot; , 17 ) </li></ul><ul><li>     .get(); </li></ul>
  7. 7. Document Queries <ul><li>DBObject myDoc = coll.findOne(); // can also use BasicDBObjectBuilderBasicDBObject query = new BasicDBObject(); query.put(&quot;i&quot;, 71); Cursor cur = coll.find(query); </li></ul>
  8. 8. GridFS <ul><ul><li>mechanism for storing files larger than 4MB </li></ul></ul><ul><ul><li>files are chunked allowing fetching of a portion or out of order </li></ul></ul><ul><ul><li>chunking is mostly transparent to underlying operating system </li></ul></ul><ul><ul><li>can store files in buckets, a MongoDB metaphor for folders </li></ul></ul><ul><ul><li>default is the fs bucket </li></ul></ul>
  9. 9. Saving a file to GridFS <ul><li>def mongo = new Mongo(host) </li></ul><ul><li>def gridfs = new GridFS(mongo.getDB(&quot;db&quot;)) </li></ul><ul><li>def save(inputStream, contentType, filename) { </li></ul><ul><li>     def inputFile = gridfs.createFile(inputStream) </li></ul><ul><li>     inputFile.setContentType(contentType) </li></ul><ul><li>     inputFile.setFilename(filename) </li></ul><ul><li>     inputFile.save() </li></ul><ul><li>} </li></ul>
  10. 10. Retrieving/Deleting a file <ul><li>def retrieveFile(String filename) { </li></ul><ul><li>     return gridfs.findOne(filename) </li></ul><ul><li>} </li></ul><ul><li>def deleteFile(String filename) { </li></ul><ul><li>     gridfs.remove(filename) </li></ul><ul><li>} </li></ul>
  11. 11. Morphia <ul><ul><li>Apache 2 Licensed </li></ul></ul><ul><ul><li>brings Hibernate/JPA paradigms to MongoDB </li></ul></ul><ul><ul><li>allows annotating of POJOs to make converting them between MongoDB and Java very easy </li></ul></ul><ul><ul><li>supports DAO abstractions </li></ul></ul><ul><ul><li>offers type-safe query support </li></ul></ul><ul><ul><li>compatible with GWT, Guice, Spring, and DI frameworks </li></ul></ul>
  12. 12. Creating a Morphia POJO <ul><li>import com.google.code.morphia.annotations.Entity; </li></ul><ul><li>@Entity(&quot;collectionName&quot;) </li></ul><ul><li>public class Contact { </li></ul><ul><li>     @Id </li></ul><ul><li>     private String id;    //generated by MongoDB </li></ul><ul><li>     private String firstName; </li></ul><ul><li>     private String lastName; </li></ul><ul><li>     @Embedded </li></ul><ul><li>     private List<PhoneNumber> phoneNumbers; </li></ul><ul><li>     // getters and setters </li></ul><ul><li>} </li></ul>
  13. 13. Mapping a POJO to a Mongo doc <ul><li>Morphia morphia = ...; </li></ul><ul><li>Mongo mongo = ...; </li></ul><ul><li>DB db = mongo.getDB(&quot;contacts&quot;); </li></ul><ul><li>Contact contact = ...; </li></ul><ul><li>// map the contact to a DBObject </li></ul><ul><li>DBObject contactObj = morphia.toDBObject(contact); </li></ul><ul><li>db.getCollection(&quot;personal&quot;).save(contactObj); </li></ul>
  14. 14. Getting a POJO from a Mongo doc <ul><li>Morphia morphia = ...; </li></ul><ul><li>Mongo mongo = ...; </li></ul><ul><li>DB db = mongo.getDB(&quot;contacts&quot;); </li></ul><ul><li>String contactId = ...; </li></ul><ul><li>//load the object from the collection </li></ul><ul><li>BasicDBObject idObj = new BasicDBObject( </li></ul><ul><li>     &quot;_id&quot;, new ObjectId(contactId) </li></ul><ul><li>); </li></ul><ul><li>BasicDBObject obj = (BasicDBObject) </li></ul><ul><li>   db.getCollection(&quot;personal&quot;).findOne(idObj); </li></ul><ul><li>Contact contact = morphia.fromDBObject(Contact.class, obj); </li></ul>
  15. 15. Beyond the Java Language
  16. 16. MongoDB with Groovy <ul><ul><li>Metaprogramming with MongoDB can reduce LOC </li></ul></ul><ul><ul><li>Dynamic finders </li></ul></ul><ul><ul><li>Fluent interface mirroring Ruby and Python </li></ul></ul>
  17. 17. Groovy + MongoDB <ul><li>J ava </li></ul><ul><li>BasicDBObject doc = new BasicDBObject(); </li></ul><ul><li>doc.put(&quot;name&quot;, &quot;MongoDB&quot;); </li></ul><ul><li>doc.put(&quot;type&quot;, &quot;database&quot;); </li></ul><ul><li>doc.put(&quot;count&quot;, 1); </li></ul><ul><li>coll.insert(doc); </li></ul><ul><li>Groovier </li></ul><ul><li>def doc = [name:&quot;MongoDB&quot;,type:&quot;database&quot;, count:1, info: </li></ul><ul><li>     [x:203, y:102] ] as BasicDBObject </li></ul><ul><li>coll.insert(doc) </li></ul><ul><li>Grooviest (using groovy-mongo) </li></ul><ul><li>coll.insert([name:&quot;MongoDB&quot;, type:&quot;database&quot;, info: [x:203, y:102]]) </li></ul>
  18. 18. Dynamic Finders <ul><ul><li>can build complex queries at runtime </li></ul></ul><ul><ul><li>can even reach into objects for addition query parameters </li></ul></ul><ul><li>Ex.  collection.findByAuthorAndPostCreatedGreaterThan(...) </li></ul><ul><li>     collection.findByComments_CreatedOn(...) </li></ul><ul><li>  </li></ul>
  19. 19. How dynamic finders work <ul><ul><li>Groovy receives the request for the method </li></ul></ul><ul><ul><li>The method is not found (invoking methodMissing) </li></ul></ul><ul><ul><li>The method name used to construct a query template </li></ul></ul><ul><ul><li>The method is cached </li></ul></ul><ul><ul><li>The method is invoked with the parameters </li></ul></ul><ul><ul><li>Future invocations use the cached method </li></ul></ul>
  20. 20. MongoDB Grails Plugin <ul><ul><li>Replaces JDBC layer in Grails applications </li></ul></ul><ul><ul><li>Can use dynamic finders </li></ul></ul><ul><ul><li>Requires only slight modifications to domain classes </li></ul></ul><ul><ul><li>http://github.com/mpriatel/mongodb-grails </li></ul></ul>
  21. 21. Links <ul><ul><li>Personal Blog: http://jameswilliams.be/blog </li></ul></ul><ul><ul><li>Twitter: http://twitter.com/ecspike </li></ul></ul><ul><ul><li>Morphia: http://code.google.com/p/morphia </li></ul></ul><ul><ul><li>Utilities for Groovy:  http://github.com/jwill/groovy-mongo </li></ul></ul><ul><ul><li>MongoDB Grails plugin:   http://github.com/mpriatel/mongodb-grails </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×