Webinar: MongoDB on the JVM
 

Webinar: MongoDB on the JVM

on

  • 2,129 views

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 ...

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.

Statistics

Views

Total Views
2,129
Views on SlideShare
1,458
Embed Views
671

Actions

Likes
3
Downloads
70
Comments
0

4 Embeds 671

http://www.10gen.com 617
http://www.mongodb.com 45
https://www.mongodb.com 7
http://drupal1.10gen.cc 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

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
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Webinar: MongoDB on the JVM Webinar: MongoDB on the JVM Presentation Transcript

  • 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
  • Today’s Webinar • Overview of MongoDB • Using MongoDB from Java • Expanding to other JVM languages • Other tools for Java developersThursday, November 29, 12
  • 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”: “Joe Armstrong”, “publicationYear”: 2007, “publisher”: “The Pragmatic Programmers, LLC”, }Thursday, November 29, 12
  • 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
  • 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
  • Scalability Database should grow and scale with our applicationThursday, November 29, 12
  • Scalability • Replica Sets: Robust replication model with automatic failover • Sharding: n-scalable horizontal partitioning with automatic managementThursday, November 29, 12
  • 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
  • Read MongoD Write ShardingThursday, November 29, 12
  • Read Secondary Secondary MongoD Primary Write ShardingThursday, November 29, 12
  • Read Shard1 Secondary Secondary MongoD Primary Write ShardingThursday, November 29, 12
  • Read Shard1 Shard2 Secondary Secondary Secondary Secondary MongoD Primary Primary Write ShardingThursday, November 29, 12
  • Read Shard1 Shard2 Shard3 Secondary Secondary Secondary Secondary Secondary Secondary MongoD Primary Primary Primary Write ShardingThursday, November 29, 12
  • Working with MongoDBThursday, November 29, 12
  • 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
  • Java and the JVM • Core Java driver • Python driver • JRuby driver • Scala driver • Clojure community driver - Monger • Higher Level Frameworks - ODMsThursday, November 29, 12
  • Using the Java DriverThursday, November 29, 12
  • ConnectingThursday, November 29, 12
  • Connecting com.mongodb.MongoClient mongoClient = new MongoClient( serverName, port);Thursday, November 29, 12
  • Connecting com.mongodb.MongoClient mongoClient = new MongoClient(); com.mongodb.DB db = mongoClient.getDB( "bookstore" );Thursday, November 29, 12
  • 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
  • InsertingThursday, November 29, 12
  • 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
  • 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
  • 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
  • QueryingThursday, November 29, 12
  • Querying com.mongodb.DBObject query = new BasicDBObject(); query.put( “tag”, “java” ); query.put( “price”, new BasicDBObject( “$lt”, 40.00 ) );Thursday, November 29, 12
  • Querying com.mongodb.DBObject query = new BasicDBObject(); query.put( “tag”, “java” ); query.put( “price”, new BasicDBObject( “$lt”, 40.00 ) );Thursday, November 29, 12
  • 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
  • Query Builder DBObject query = QueryBuilder.start( “price” ) .lessThan( 40.00 ) .and( “tag” ) .is( “java” ).get();Thursday, November 29, 12
  • 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
  • Other JVM LanguagesThursday, November 29, 12
  • 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
  • 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
  • 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: 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
  • 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
  • Scala val mongo: MongoCollection = MongoConnection()("bookstore")("books")Thursday, November 29, 12
  • 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
  • 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
  • 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
  • Higher Level AbstractionsThursday, November 29, 12
  • MorphiaThursday, November 29, 12
  • Morphia @Entity("books") // MongoDB collection to use class Book { public Book () { } // ... }Thursday, November 29, 12
  • Morphia @Entity("books") class Book { @Id private ObjectId id; public Book () { } // ... }Thursday, November 29, 12
  • Morphia @Entity("books") class Book { @Id private ObjectId id; private List<String> tags = new ArrayList<String>(); public Book () { } // ... }Thursday, November 29, 12
  • 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
  • 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
  • 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
  • Spring DataThursday, November 29, 12
  • Spring Data Document @Document(collection=“books”) public class Book { // ... }Thursday, November 29, 12
  • Spring Data Document @Document(collection=“books”) public class Book { @Id private String id; }Thursday, November 29, 12
  • 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
  • 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
  • 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
  • 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
  • Spring Data - Connecting ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring-context.xml"); MongoOperations ops = ctx.getBean("mongoTemplate", MongoOperations.class);Thursday, November 29, 12
  • 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
  • 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
  • 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
  • Spring Data - Querying ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring-context.xml"); BookRepository repo = ctx.getBean("bookRepository", BookRepository.class);Thursday, November 29, 12
  • 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
  • Spring Data - Querying public interface BookRepository extends CrudRepository<Book, String> { public Book findByAuthor(String author); }Thursday, November 29, 12
  • 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
  • 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
  • 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
  • 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