Java Development with MongoDB (James Williams)
Upcoming SlideShare
Loading in...5
×
 

Java Development with MongoDB (James Williams)

on

  • 8,541 views

 

Statistics

Views

Total Views
8,541
Views on SlideShare
8,100
Embed Views
441

Actions

Likes
17
Downloads
221
Comments
1

10 Embeds 441

http://www.slideshare.net 153
http://www.nosqldatabases.com 148
http://www.mongodb.org 98
http://www.10gen.com 18
http://mongospanish.blogspot.com 17
http://translate.googleusercontent.com 2
http://static.slidesharecdn.com 2
http://pt.wiki.mongodb.org 1
http://slideclip.b-prep.com 1
http://192.168.6.52 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Java Development with MongoDB (James Williams) Java Development with MongoDB (James Williams) Presentation Transcript

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