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.
Dev Dives: Streamline document processing with UiPath Studio Web
Webinar: MongoDB on the JVM
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_gee
Thursday, November 29, 12
2. Today’s Webinar
• Overview of MongoDB
• Using MongoDB from Java
• Expanding to other JVM languages
• Other tools for Java developers
Thursday, November 29, 12
8. Scalability
Database should grow and scale with our
application
Thursday, November 29, 12
9. Scalability
• Replica Sets: Robust replication model with
automatic failover
• Sharding: n-scalable horizontal partitioning with
automatic management
Thursday, November 29, 12
10. Insert Picture/Table/Chart Here (but if picture
doesn’t have a background, be sure to remove gray
background, border, and shadow!)
Replica Set
Thursday, November 29, 12
11. Read
MongoD
Write
Sharding
Thursday, November 29, 12
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 types
Thursday, November 29, 12
18. Java and the JVM
• Core Java driver
• Python driver
• JRuby driver
• Scala driver
• Clojure community driver - Monger
• Higher Level Frameworks - ODMs
Thursday, November 29, 12
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. 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. Clojure
• Clojure - http://clojuremongodb.info/
Thursday, November 29, 12
38. Scala Driver
• Wraps the Java driver
• Provide idiomatic Scala interface to MongoDB
Thursday, November 29, 12
41. Scala
val builder = MongoDBObject.newBuilder
builder += "foo" -> "bar"
builder += "x" -> 5
builder += "map" -> Map("spam" -> 8.2, "eggs" -> "bacon")
val dbObj = builder.result
Thursday, November 29, 12
42. Scala
val mongo: MongoCollection = MongoConnection()("bookstore")("books")
Thursday, November 29, 12
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. 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. Other Scala Projects
• Hammersmith - Netty + Akka.IO interfaces, strongly
functional and callback based
• ReactiveMongo - from the amazing team @
Zenexity who brought us the Play! Framework
Thursday, November 29, 12
48. Morphia
@Entity("books") // MongoDB collection to use
class Book {
public Book () {
}
// ...
}
Thursday, November 29, 12
49. Morphia
@Entity("books")
class Book {
@Id private ObjectId id;
public Book () {
}
// ...
}
Thursday, November 29, 12
50. Morphia
@Entity("books")
class Book {
@Id private ObjectId id;
private List<String> tags = new ArrayList<String>();
public Book () {
}
// ...
}
Thursday, November 29, 12
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. 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. 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
55. Spring Data Document
@Document(collection=“books”)
public class Book {
// ...
}
Thursday, November 29, 12
56. Spring Data Document
@Document(collection=“books”)
public class Book {
@Id private String id;
}
Thursday, November 29, 12
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. 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. 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
61. Spring Data - Connecting
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring-context.xml");
MongoOperations ops = ctx.getBean("mongoTemplate",
MongoOperations.class);
Thursday, November 29, 12
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. 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. 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. Spring Data - Querying
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring-context.xml");
BookRepository repo = ctx.getBean("bookRepository",
BookRepository.class);
Thursday, November 29, 12
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. Spring Data - Querying
public interface BookRepository extends
CrudRepository<Book, String> {
public Book findByAuthor(String author);
}
Thursday, November 29, 12
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 development
Thursday, November 29, 12
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_gee
Thursday, November 29, 12
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_gee
Thursday, November 29, 12