Thursday Nov 29th - 10:00am PST / 1:00pm EST / 6:00pm GMT      MongoDB on the JVM       Trisha Gee, Java Developer, 10gen ...
Today’s Webinar       • Overview of MongoDB       • Using MongoDB from Java       • Expanding to other JVM languages      ...
What is MongoDB?Thursday, November 29, 12
Designed to fit your application       • Agile       • Scalable       • Highly AvailableThursday, November 29, 12
Document Oriented       {           “title”: “Programming Erlang: Software for a Concurrent World”,           “author”: “J...
Document Oriented       {           “title”: “Programming Erlang: Software for a Concurrent World”,           “author”: “J...
Document Oriented       {           “title”: “Programming Erlang: Software for a Concurrent World”,           “author”: “J...
Scalability       Database should grow and scale with our       applicationThursday, November 29, 12
Scalability       • Replica Sets: Robust replication model with          automatic failover       • Sharding: n-scalable h...
Insert Picture/Table/Chart Here (but if picture       doesn’t have a background, be sure to remove gray       background, ...
Read                  MongoD                            Write       ShardingThursday, November 29, 12
Read                 Secondary                 Secondary                  MongoD                  Primary                 ...
Read                   Shard1                 Secondary                 Secondary                  MongoD                 ...
Read                   Shard1     Shard2                 Secondary   Secondary                 Secondary   Secondary      ...
Read                   Shard1     Shard2      Shard3                 Secondary   Secondary   Secondary                 Sec...
Working with MongoDBThursday, November 29, 12
MongoDB Drivers       • Official support for 12 languages       • Community drivers for many more       • Drivers have two...
Java and the JVM       • Core Java driver       • Python driver       • JRuby driver       • Scala driver       • Clojure ...
Using the Java DriverThursday, November 29, 12
ConnectingThursday, November 29, 12
Connecting       com.mongodb.MongoClient mongoClient = new MongoClient(                            serverName, port);Thurs...
Connecting       com.mongodb.MongoClient mongoClient = new MongoClient();       com.mongodb.DB db = mongoClient.getDB( "bo...
Connecting       com.mongodb.MongoClient mongoClient = new MongoClient();       com.mongodb.DB db = mongoClient.getDB( "bo...
InsertingThursday, November 29, 12
Inserting       com.mongodb.DBCollection coll = db.getCollection( "books" );       ...       com.mongodb.DBObject newBook ...
Inserting       com.mongodb.DBCollection coll = db.getCollection( "books" );       ...       com.mongodb.DBObject newBook ...
Inserting       com.mongodb.DBCollection coll = db.getCollection( "books" );       ...       com.mongodb.DBObject newBook ...
QueryingThursday, November 29, 12
Querying       com.mongodb.DBObject query = new BasicDBObject();       query.put( “tag”, “java” );       query.put( “price...
Querying       com.mongodb.DBObject query = new BasicDBObject();       query.put( “tag”, “java” );       query.put( “price...
Querying       com.mongodb.DBObject query = new BasicDBObject();       query.put( “tag”, “java” );       query.put( “price...
Query Builder       DBObject query = QueryBuilder.start( “price” )                        .lessThan( 40.00 )              ...
Query Builder       DBObject query = QueryBuilder.start( “price” )                       .lessThan( 40.00 )               ...
Other JVM LanguagesThursday, November 29, 12
Jython       • Allows Python’s clear syntax on the JVM       • PyMongo uses native python code and has no          hooks i...
JRuby       • Support for JRuby 1.6.x and 1.7.x       • Native Java extensions are bundled in with the          BSON gem. ...
Clojure       • Clojure - http://clojuremongodb.info/Thursday, November 29, 12
Scala Driver       • Wraps the Java driver       • Provide idiomatic Scala interface to MongoDBThursday, November 29, 12
ScalaThursday, November 29, 12
Scala       case class Book(id: ObjectId, author: String,                 isbn: String, price: Price,                 year...
Scala       val builder = MongoDBObject.newBuilder       builder += "foo" -> "bar"       builder += "x" -> 5       builder...
Scala       val mongo: MongoCollection = MongoConnection()("bookstore")("books")Thursday, November 29, 12
Scala       val mongo: MongoCollection = MongoConnection()("bookstore")("books")       def findAll() = for ( book <- mongo...
Scala       val mongo: MongoCollection = MongoConnection()("bookstore")("books")       def findAll() = for ( book <- mongo...
Other Scala Projects       • Hammersmith - Netty + Akka.IO interfaces, strongly          functional and callback based    ...
Higher Level AbstractionsThursday, November 29, 12
MorphiaThursday, November 29, 12
Morphia       @Entity("books") // MongoDB collection to use       class Book {          public Book () {          }       ...
Morphia       @Entity("books")       class Book {          @Id private ObjectId id;           public Book () {           }...
Morphia       @Entity("books")       class Book {          @Id private ObjectId id;          private List<String> tags = n...
Morphia       @Entity("books")       class Book {          @Id private ObjectId id;          private List<String> tags = n...
Morphia       @Entity("books")       class Book {          @Id private ObjectId id;          private List<String> tags = n...
Morphia       @Entity("books")       class Book {          @Id private ObjectId id;          private List<String> tags = n...
Spring DataThursday, November 29, 12
Spring Data Document       @Document(collection=“books”)       public class Book {           // ...       }Thursday, Novem...
Spring Data Document       @Document(collection=“books”)       public class Book {         @Id private String id;       }T...
Spring Data Document       @Document(collection=“books”)       public class Book {         @Id private String id;         ...
Spring Data Document       @Document(collection=“books”)       public class Book {         @Id private String id;         ...
Spring Data Document       @Document(collection=“books”)       public class Book {         @Id private String id;         ...
Spring Data Configuration       <?xml version="1.0" encoding="UTF-8"?>       <beans         ...            xmlns:mongo="htt...
Spring Data - Connecting       ApplicationContext ctx = new ClassPathXmlApplicationContext(                             "s...
Spring Data - Inserting       ApplicationContext ctx = new ClassPathXmlApplicationContext(                             "sp...
Spring Data - Querying       MongoOperations ops = ctx.getBean("mongoTemplate",                              MongoOperatio...
Spring Data - Querying       MongoOperations ops = ctx.getBean("mongoTemplate",                              MongoOperatio...
Spring Data - Querying       ApplicationContext ctx = new ClassPathXmlApplicationContext(                             "spr...
Spring Data - Querying       ApplicationContext ctx = new ClassPathXmlApplicationContext(                             "spr...
Spring Data - Querying       public interface BookRepository extends                  CrudRepository<Book, String> {      ...
Spring Data Configuration       <?xml version="1.0" encoding="UTF-8"?>       <beans         ...            xmlns:mongo="htt...
In Summary       • MongoDB is an agile, scalable NoSQL database       • If you’re coding on the JVM you have many options ...
More Information           Resource                          Location           MongoDB Downloads                 www.mong...
More Information       • http://www.mongodb.org/display/DOCS/Drivers       • http://code.google.com/p/morphia/       • htt...
Upcoming SlideShare
Loading in …5
×

Webinar: MongoDB on the JVM

2,266 views
2,072 views

Published on

Trisha Gee explores the deeper relationship between the MongoDB database and various languages on the Java Virtual Machine such as Java, Scala, Clojure, JRuby and Python as well as the challenges posted getting MongoDB to play nice with these tools and their syntax. Also examined will be frameworks and integration points popular between MongoDB and the JVM such as Spring Data, Morphia and Lift’s MongoDB-Record component.

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,266
On SlideShare
0
From Embeds
0
Number of Embeds
719
Actions
Shares
0
Downloads
80
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Webinar: MongoDB on the JVM

  1. 1. Thursday Nov 29th - 10:00am PST / 1:00pm EST / 6:00pm GMT MongoDB on the JVM Trisha Gee, Java Developer, 10gen trisha.gee@10gen.com @trisha_geeThursday, November 29, 12
  2. 2. Today’s Webinar • Overview of MongoDB • Using MongoDB from Java • Expanding to other JVM languages • Other tools for Java developersThursday, November 29, 12
  3. 3. What is MongoDB?Thursday, November 29, 12
  4. 4. Designed to fit your application • Agile • Scalable • Highly AvailableThursday, November 29, 12
  5. 5. Document Oriented { “title”: “Programming Erlang: Software for a Concurrent World”, “author”: “Joe Armstrong”, “publicationYear”: 2007, “publisher”: “The Pragmatic Programmers, LLC”, }Thursday, November 29, 12
  6. 6. Document Oriented { “title”: “Programming Erlang: Software for a Concurrent World”, “author”: “Joe Armstrong”, “publicationYear”: 2007, “price”: { "currency": "USD", "discount": 24.14, "msrp": 36.95 }, “publisher”: “The Pragmatic Programmers, LLC”, }Thursday, November 29, 12
  7. 7. Document Oriented { “title”: “Programming Erlang: Software for a Concurrent World”, “author”: “Joe Armstrong”, “publicationYear”: 2007, “price”: { "currency": "USD", "discount": 24.14, "msrp": 36.95 }, “publisher”: “The Pragmatic Programmers, LLC”, “tags”: [ “erlang”, “concurrent programming”, “multicore”, “programming” ] }Thursday, November 29, 12
  8. 8. Scalability Database should grow and scale with our applicationThursday, November 29, 12
  9. 9. Scalability • Replica Sets: Robust replication model with automatic failover • Sharding: n-scalable horizontal partitioning with automatic managementThursday, November 29, 12
  10. 10. Insert Picture/Table/Chart Here (but if picture doesn’t have a background, be sure to remove gray background, border, and shadow!) Replica SetThursday, November 29, 12
  11. 11. Read MongoD Write ShardingThursday, November 29, 12
  12. 12. Read Secondary Secondary MongoD Primary Write ShardingThursday, November 29, 12
  13. 13. Read Shard1 Secondary Secondary MongoD Primary Write ShardingThursday, November 29, 12
  14. 14. Read Shard1 Shard2 Secondary Secondary Secondary Secondary MongoD Primary Primary Write ShardingThursday, November 29, 12
  15. 15. Read Shard1 Shard2 Shard3 Secondary Secondary Secondary Secondary Secondary Secondary MongoD Primary Primary Primary Write ShardingThursday, November 29, 12
  16. 16. Working with MongoDBThursday, November 29, 12
  17. 17. MongoDB Drivers • Official support for 12 languages • Community drivers for many more • Drivers have two main responsibilities: •Connecting to MongoDB servers •Translating BSON into native typesThursday, November 29, 12
  18. 18. Java and the JVM • Core Java driver • Python driver • JRuby driver • Scala driver • Clojure community driver - Monger • Higher Level Frameworks - ODMsThursday, November 29, 12
  19. 19. Using the Java DriverThursday, November 29, 12
  20. 20. ConnectingThursday, November 29, 12
  21. 21. Connecting com.mongodb.MongoClient mongoClient = new MongoClient( serverName, port);Thursday, November 29, 12
  22. 22. Connecting com.mongodb.MongoClient mongoClient = new MongoClient(); com.mongodb.DB db = mongoClient.getDB( "bookstore" );Thursday, November 29, 12
  23. 23. Connecting com.mongodb.MongoClient mongoClient = new MongoClient(); com.mongodb.DB db = mongoClient.getDB( "bookstore" ); com.mongodb.DBCollection coll = db.getCollection( "books" );Thursday, November 29, 12
  24. 24. InsertingThursday, November 29, 12
  25. 25. Inserting com.mongodb.DBCollection coll = db.getCollection( "books" ); ... com.mongodb.DBObject newBook = new BasicDBObject(); newBook.put( “title”, “Java Concurrency in Practice” ); newBook.put( “author”, “Brian Goetz” ); newBook.put( “price”, new BasicDBObject( “msrp”, 38.99 ) );Thursday, November 29, 12
  26. 26. Inserting com.mongodb.DBCollection coll = db.getCollection( "books" ); ... com.mongodb.DBObject newBook = new BasicDBObject(); newBook.put( “title”, “Java Concurrency in Practice” ); newBook.put( “author”, “Brian Goetz” ); newBook.put( “price”, new BasicDBObject( “msrp”, 38.99 ) );Thursday, November 29, 12
  27. 27. Inserting com.mongodb.DBCollection coll = db.getCollection( "books" ); ... com.mongodb.DBObject newBook = new BasicDBObject(); newBook.put( “title”, “Java Concurrency in Practice” ); newBook.put( “author”, “Brian Goetz” ); newBook.put( “price”, new BasicDBObject( “msrp”, 38.99 ) ); coll.insert(newBook);Thursday, November 29, 12
  28. 28. QueryingThursday, November 29, 12
  29. 29. Querying com.mongodb.DBObject query = new BasicDBObject(); query.put( “tag”, “java” ); query.put( “price”, new BasicDBObject( “$lt”, 40.00 ) );Thursday, November 29, 12
  30. 30. Querying com.mongodb.DBObject query = new BasicDBObject(); query.put( “tag”, “java” ); query.put( “price”, new BasicDBObject( “$lt”, 40.00 ) );Thursday, November 29, 12
  31. 31. Querying com.mongodb.DBObject query = new BasicDBObject(); query.put( “tag”, “java” ); query.put( “price”, new BasicDBObject( “$lt”, 40.00 ) ); for ( DBObject doc : coll.find( query ) ) { // ...for every document found, do something... }Thursday, November 29, 12
  32. 32. Query Builder DBObject query = QueryBuilder.start( “price” ) .lessThan( 40.00 ) .and( “tag” ) .is( “java” ).get();Thursday, November 29, 12
  33. 33. Query Builder DBObject query = QueryBuilder.start( “price” ) .lessThan( 40.00 ) .and( “tag” ) .is( “java” ).get(); DBObject query = new BasicDBObject(); query.put( “tag”, “java” ); query.put( “price”, new BasicDBObject( “$lt”, 40.00 ) );Thursday, November 29, 12
  34. 34. Other JVM LanguagesThursday, November 29, 12
  35. 35. Jython • Allows Python’s clear syntax on the JVM • PyMongo uses native python code and has no hooks into the Java driver • Some care needed to ensure cursors are closed due to different garbage collection models.Thursday, November 29, 12
  36. 36. JRuby • Support for JRuby 1.6.x and 1.7.x • Native Java extensions are bundled in with the BSON gem. • Utilises the Java driver for improved BSON decoding and performance.Thursday, November 29, 12
  37. 37. Clojure • Clojure - http://clojuremongodb.info/Thursday, November 29, 12
  38. 38. Scala Driver • Wraps the Java driver • Provide idiomatic Scala interface to MongoDBThursday, November 29, 12
  39. 39. ScalaThursday, November 29, 12
  40. 40. Scala case class Book(id: ObjectId, author: String, isbn: String, price: Price, year: Int, tags: Seq[String], title: String, publisher: String, edition: Option[String]) { def toDBObject = MongoDBObject( "author" -> author, "_id" -> id, "isbn" -> isbn, "price" -> price.toDBObject, "publicationYear" -> year, "tags" -> tags, "title" -> title, "publisher" -> publisher, "edition" -> edition ) }Thursday, November 29, 12
  41. 41. Scala val builder = MongoDBObject.newBuilder builder += "foo" -> "bar" builder += "x" -> 5 builder += "map" -> Map("spam" -> 8.2, "eggs" -> "bacon") val dbObj = builder.resultThursday, November 29, 12
  42. 42. Scala val mongo: MongoCollection = MongoConnection()("bookstore")("books")Thursday, November 29, 12
  43. 43. Scala val mongo: MongoCollection = MongoConnection()("bookstore")("books") def findAll() = for ( book <- mongo.find() ) yield new Book(book) findAll().foreach(b => println("<Book> " + b))Thursday, November 29, 12
  44. 44. Scala val mongo: MongoCollection = MongoConnection()("bookstore")("books") def findAll() = for ( book <- mongo.find() ) yield newBook(book) findAll().foreach(b => println("<Book> " + b)) val query: DBObject = ("price" $lt 40.00) ++ ("tag" -> "scala")Thursday, November 29, 12
  45. 45. Other Scala Projects • Hammersmith - Netty + Akka.IO interfaces, strongly functional and callback based • ReactiveMongo - from the amazing team @ Zenexity who brought us the Play! FrameworkThursday, November 29, 12
  46. 46. Higher Level AbstractionsThursday, November 29, 12
  47. 47. MorphiaThursday, November 29, 12
  48. 48. Morphia @Entity("books") // MongoDB collection to use class Book { public Book () { } // ... }Thursday, November 29, 12
  49. 49. Morphia @Entity("books") class Book { @Id private ObjectId id; public Book () { } // ... }Thursday, November 29, 12
  50. 50. Morphia @Entity("books") class Book { @Id private ObjectId id; private List<String> tags = new ArrayList<String>(); public Book () { } // ... }Thursday, November 29, 12
  51. 51. Morphia @Entity("books") class Book { @Id private ObjectId id; private List<String> tags = new ArrayList<String>(); @Embedded private Price price; public Book () { } // ... } @Embedded class Price { // ... }Thursday, November 29, 12
  52. 52. Morphia @Entity("books") class Book { @Id private ObjectId id; private List<String> tags = new ArrayList<String>(); @Embedded private Price price; @Reference private Author author; public Book () { } // ... } @Entity("authors") class Author { // ... }Thursday, November 29, 12
  53. 53. Morphia @Entity("books") class Book { @Id private ObjectId id; private List<String> tags = new ArrayList<String>(); @Embedded private Price price; @Reference private Author author; @Property("publicationYear") private int year; public Book () { } // ... }Thursday, November 29, 12
  54. 54. Spring DataThursday, November 29, 12
  55. 55. Spring Data Document @Document(collection=“books”) public class Book { // ... }Thursday, November 29, 12
  56. 56. Spring Data Document @Document(collection=“books”) public class Book { @Id private String id; }Thursday, November 29, 12
  57. 57. Spring Data Document @Document(collection=“books”) public class Book { @Id private String id; private List<String> tags = new ArrayList<String>(); private Price price; }Thursday, November 29, 12
  58. 58. Spring Data Document @Document(collection=“books”) public class Book { @Id private String id; private List<String> tags = new ArrayList<String>(); private Price price; @DBRef private Author author; }Thursday, November 29, 12
  59. 59. Spring Data Document @Document(collection=“books”) public class Book { @Id private String id; private List<String> tags = new ArrayList<String>(); private Price price; @DBRef private Author author; @Field("publicationYear") private int year; }Thursday, November 29, 12
  60. 60. Spring Data Configuration <?xml version="1.0" encoding="UTF-8"?> <beans ... xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" ... http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> <mongo:mongo id="mongo" > <mongo:options auto-connect-retry="true" /> </mongo:mongo> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg value="news" /> </bean> <mongo:repositories base-package="org.mongodb.news.repositories"/> </beans>Thursday, November 29, 12
  61. 61. Spring Data - Connecting ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring-context.xml"); MongoOperations ops = ctx.getBean("mongoTemplate", MongoOperations.class);Thursday, November 29, 12
  62. 62. Spring Data - Inserting ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring-context.xml"); MongoOperations ops = ctx.getBean("mongoTemplate", MongoOperations.class); Book book = new Book(); // set fields on the book // ... ops.insert(book);Thursday, November 29, 12
  63. 63. Spring Data - Querying MongoOperations ops = ctx.getBean("mongoTemplate", MongoOperations.class); Book book = ops.findOne(query(where("price").lt(40)), Book.class);Thursday, November 29, 12
  64. 64. Spring Data - Querying MongoOperations ops = ctx.getBean("mongoTemplate", MongoOperations.class); Book book = ops.findOne(query(where("price").lt(40)), Book.class); book = ops.findById(book.getId(), Book.class);Thursday, November 29, 12
  65. 65. Spring Data - Querying ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring-context.xml"); BookRepository repo = ctx.getBean("bookRepository", BookRepository.class);Thursday, November 29, 12
  66. 66. Spring Data - Querying ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring-context.xml"); BookRepository repo = ctx.getBean("bookRepository", BookRepository.class); Book book = repo.findByAuthor("Eric");Thursday, November 29, 12
  67. 67. Spring Data - Querying public interface BookRepository extends CrudRepository<Book, String> { public Book findByAuthor(String author); }Thursday, November 29, 12
  68. 68. Spring Data Configuration <?xml version="1.0" encoding="UTF-8"?> <beans ... xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" ... http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> <mongo:mongo id="mongo" > <mongo:options auto-connect-retry="true" /> </mongo:mongo> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg value="news" /> </bean> <mongo:repositories base-package="org.mongodb.news.repositories"/> </beans>Thursday, November 29, 12
  69. 69. In Summary • MongoDB is an agile, scalable NoSQL database • If you’re coding on the JVM you have many options for interacting with MongoDB •Multiple native drivers •Frameworks to simplify developmentThursday, November 29, 12
  70. 70. More Information Resource Location MongoDB Downloads www.mongodb.org/downloads Free Online Training education.10gen.com Webinars and Events www.10gen.com/events White Papers www.10gen.com/white-papers Customer Case Studies www.10gen.com/customers Presentations www.10gen.com/presentations Documentation docs.mongodb.org Additional Info info@10gen.com trisha.gee@10gen.com @trisha_geeThursday, November 29, 12
  71. 71. More Information • http://www.mongodb.org/display/DOCS/Drivers • http://code.google.com/p/morphia/ • http://www.springsource.org/spring-data/mongodb trisha.gee@10gen.com @trisha_geeThursday, November 29, 12

×