$30 off During Our Annual Pro Sale. View Details »

Webinar Introduction to MongoDB's Java Driver

mongodb
August 16, 2012
1.1k

Webinar Introduction to MongoDB's Java Driver

Bryan Reinero, Software Engineer, 10gen

A look into the internals of the Java driver for MongoDB. Learn how the driver supports replication, database connection pooling, and building queries. Also includes an introduction to the BSON (binary JSON) format for serialization of JSON objects, how the drivers handles write behavior with the use of write concerns and how the driver communicates to the database via the Mongo wire protocol. New features, fixes and improvements in the latest release will also be covered.

mongodb

August 16, 2012
Tweet

Transcript

  1. 1  
    MongoDB’s  Java  Driver  

    View Slide

  2. Topics we’ll cover
    •  Main  responsibilities  of  the  driver  
    •  New  features  for  version  2.9.0  

    View Slide

  3. Driver’s Main Responsibilities
    •  Manage  connections  
    •  BSON  serialization  /  deserialization  
    •  Manage  cursors  
    •  Maintain  replica  set  awareness  

    View Slide

  4. Connection Management
    /**  The  mechanism  managing  the  pool  */  
    public  class  DBPortPool  extends  SimplePool  
    /**  database  connection  with  internal  pooling  */  
    public  class  Mongo  
    /**  Various  settings  for  the  driver  */  
    public  class  MongoOptions  

    View Slide

  5. Wire Protocol
    •  TCP/IP  socket  based  request/response  
    •  Exchanges  BSON  objects  between  client  &  
    server  
    •  BSON  –  a  traversable,  binary  encoded  
    serialization  of  JSON  documents    
    •  BSON  is  the  primary  data  representation  in  
    MongoDB  
    http://bsonspec.org/  

    View Slide

  6. Encoding / Decoding BSON
     interface  DBEncoder  
     interface  DBDecoder  
    DBObject  decode(  InputStream  in,    
             DBCollection  collection  );  
    int  writeObject(  OutputBuffer  buf,    
                                   BSONObject  o  );  

    View Slide

  7. On to the good stuff…
    Reading, Writing & Arithmetic

    View Slide

  8. A  simple  example…  

    View Slide

  9. Basics of Replication
    Node 1

    Secondary

    Node 2

    Secondary

    Node 3

    Primary

    Replication  
    Replication  
    Heartbeat  

    View Slide

  10. Strong Consistency
    Primary

    Secondary

    Secondary

    Read  
    Write  
    Client  

    View Slide

  11. Eventual Consistency
    Primary

    Read  
    Write  
    Driver  
    Read  
    Secondary

    Secondary

    Client  Application  

    View Slide

  12. Read Preferences
    •  PRIMARY    
    •  PRIMARY  PREFERRED    
    •  SECONDARY    
    •  SECONDARY  PREFERRED    
    •  NEAREST    

    View Slide

  13. Using  Read  
    Preferences…  

    View Slide

  14. Writes & Durability
    •  Fire  and  forget    
    •  Wait  for  error  
    •  Wait  for  journal  commit  
    •  Wait  for  fsync  
    •  Wait  for  replication  to  secondary  

    View Slide

  15. Using  write  concerns…  

    View Slide

  16. Tagging
    •  Each  node  within  a  replica  set  can  be  
    marked  with  descriptors  called  tags    
    •  Tags  can  indicate  a  node’s  location,  
    membership  in  a  set,  or  other  
    characteristics    
    •  Use  tags  to  target  specific  nodes  for  read  
    or  write  operations    

    View Slide

  17. Using Tags
    Node 1
    Node 2

    Node 3

    tags"  :  {  
    "datacenter"  :  "Los  Angeles”,  
    "region"  :  "US_West”  
    }  
    tags"  :  {  
    "datacenter"  :”San  Jose”,  
    "region"  :  ”US_West”  
    }  
    tags"  :  {  
    "datacenter"  :”Richmond”,  
    "region"  :  "US_East”  
    }  

    View Slide

  18. Using  tags…  

    View Slide

  19. Aggregation Framework
    •  Calculate  aggregate  values  without  
    having  to  use  map-­‐reduce  
    •  Written  in  C++,  high  performance  
    •  Declarative,  no  javascript  
    •  Extensible,  new  operations  easily  added  

    View Slide

  20. Aggregation Operators
    •  $match
    •  $project
    •  $unwind
    •  $group
    •  $sort
    •  $skip
    •  $limit

    View Slide

  21. Aggregation Example
    sample document:
    {  
     "_id"  :  
    ObjectId("5029e745a0988a275aefd0c0"),  
     "name"  :  "quiz",  
     "score"  :  99,  
     "student"  :  7,  
     "year"  :  "junior"  
    }  

    View Slide

  22. Pipelining Operations
    $match
    $project
    $group
    $match  :  {  year  :  'junior'  }    
    $project  :  {  name  :  1,  score  :  1  }    
    $group:  {    
           _id:  '$name',    
         average:  {  $avg:  '$score'  }  
    }  

    View Slide

  23. What the command looks like…
    {    
           "aggregate"  :  "scores"  ,    
           "pipeline"  :  [    
                   {  "$match"  :  {  "year"  :  "junior"}}  ,  
                   {  "$project"  :  {  "name"  :  1  ,  "score"  :  1  ,  "_id"  :  0}}  ,    
                   {  "$group"  :  {    
                           "_id"  :  "$name"  ,    
                           "average"  :  {  "$avg"  :  "$score"}  
                           }  
                   }  
           ]  
    }  

    View Slide

  24. The  aggregation  helper…  

    View Slide

  25. There’s plenty more!...
    •  Tutorials  
    http://www.mongodb.org/display/DOCS/Java
    +Tutorial  
    •  Javadocs  http://api.mongodb.org/java/current/  
    •  Jira  
    https://jira.mongodb.org/secure/Dashboard.jspa  

    View Slide

  26. Join  us!  
    We’re  Hiring!  
    [email protected]    

    View Slide