Your SlideShare is downloading. ×
Webinar: MongoDB Persistence with Java and Morphia
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Webinar: MongoDB Persistence with Java and Morphia

4,574

Published on

MongoDB is the leading noSQL database and as with any new technology, some of the biggest questions are about tools and integration. This webinar will cover how to use MongoDB from a Java application …

MongoDB is the leading noSQL database and as with any new technology, some of the biggest questions are about tools and integration. This webinar will cover how to use MongoDB from a Java application and introduce Morphia, the object document mapping library. It will cover the essential features of the framework with examples before moving on to some of the more advanced and upcoming features such as text search and aggregation framework support.

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

No Downloads
Views
Total Views
4,574
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
116
Comments
0
Likes
7
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. MongoDB Persistence with Java and Morphia Justin Lee Software Engineer, MongoDB
  • 2. Agenda Introduction Getting Started Modeling Querying Updating Indexing Road Map
  • 3. What is Morphia? • Object document mapper — https://github.com/mongodb/morphia • Provides mapping to/from Java objects to mongodb • Annotation-based • Embedded objects/documents • References (lazy or eager) • Fluent query/update APIs • Runtime validation
  • 4. Why Morphia? • Offload object marshaling • automatically keeps up as objects evolve: new, removed, renamed • Higher abstraction • Simplified management of indexes • Unify code with schema management • Object/Document Versioning
  • 5. A Brief History • Created by Scott Hernandez • Hired by 10Gen —> started working on the kernel • Morphia left alone for a couple of years • Lots of uncertainty • James Green forked morphia • I was hired in June, 2013 in part to pick up Morphia • 5 releases since with another pending
  • 6. Getting Started
  • 7. Adding morphia — Maven <dependency> <groupId>org.mongodb.morphia</groupId> <artifactId>morphia</artifactId> <version>0.105</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.11.3</version> </dependency>
  • 8. Adding morphia — Gradle compile "org.mongodb.morphia:morphia:0.105" compile "org.mongodb:mongo-java-driver:2.11.3" ! !
  • 9. Initializing morphia Morphia morphia = new Morphia(); Datastore datastore = morphia.createDatastore( new MongoClient(), “morphia-demo"); morphia.mapPackage("org.mongodb.morphia.demo"); datastore.ensureIndexes(); !
  • 10. Entity Modeling
  • 11. Basic Entity @Entity public class GithubUser { private String userName; private String fullName; ! private Date memberSince; }
  • 12. Basic Entity @Entity (value = “users") public class GithubUser { private String userName; private String fullName; ! private Date memberSince; }
  • 13. Basic Entity @Entity (value = "users", noClassnameStored = true) public class GithubUser { private String userName; private String fullName; ! private Date memberSince; }
  • 14. Basic Entity @Entity (value = "users", noClassnameStored = true) public class GithubUser { @Id private String userName; private String fullName; ! private Date memberSince; }
  • 15. Basic Entity @Entity (value = "users", noClassnameStored = true) public class GithubUser { @Id private String userName; private String fullName; ! } @Property("since") private Date memberSince;
  • 16. Basic Entity — Saving GithubUser user = new GithubUser("evanchooly"); user.fullName = "Justin Lee"; user.memberSince = sdf.parse("6-15-1987"); datastore.save(user);
  • 17. Basic Entity — Shell repl0:PRIMARY> db.users.findOne() { "_id" : "evanchooly", "fullName" : "Justin Lee", "since" : ISODate("1987-06-15T04:00:00Z") }
  • 18. Complex Entities @Entity("orgs") public class Organization { @Id public String name; ! } public Organization(final String name) { this.name = name; }
  • 19. Complex Entities @Embedded public class Settings { public String defaultBranch = "master"; public Boolean allowWiki = false; public Boolean allowIssues = true; }
  • 20. Complex Entities @Entity("repos") public class Repository { @Id public String name; @Reference public Organization organization; @Reference public GithubUser owner; public Settings settings = new Settings(); }
  • 21. Complex Entities @Entity(value = "users", noClassnameStored = true) public class GithubUser { @Id public final String userName; public String fullName; @Property("since") public Date memberSince; @Reference(lazy = true) public List<Repository> repositories = new ArrayList<>(); }
  • 22. Complex Entities Organization org = new Organization("mongodb"); ! GithubUser user = new GithubUser("evanchooly"); user.fullName = "Justin Lee"; user.memberSince = sdf.parse("6-15-1987"); ! datastore.save(org); datastore.save(user); ! datastore.save(new Repository(org, "morphia")); datastore.save(new Repository(user, "morphia"));
  • 23. Complex Entities — Shell repl0:PRIMARY> show collections orgs repos system.indexes users
  • 24. Complex Entities — Shell repl0:PRIMARY> db.orgs.findOne(); { "_id" : "mongodb", "className" : "org.mongodb.morphia.demo.Organization" }
  • 25. Complex Entities — Shell repl0:PRIMARY> db.repos.find().pretty(); { "_id" : "mongodb/morphia", "className" : "org.mongodb.morphia.demo.Repository", "organization" : DBRef("orgs", "mongodb"), "settings" : { "defaultBranch" : "master", "allowWiki" : false, "allowIssues" : true } } { "_id" : "evanchooly/morphia", "className" : "org.mongodb.morphia.demo.Repository", "owner" : DBRef("users", "evanchooly"), "settings" : { … } }
  • 26. Complex Entities — Shell repl0:PRIMARY> db.users.findOne(); { "_id" : "evanchooly", "fullName" : "Justin Lee", "since" : ISODate("1987-06-15T04:00:00Z"), "repositories" : [ DBRef("repos", "mongodb/morphia"), DBRef("repos", "evanchooly/morphia") ] }
  • 27. Queries
  • 28. Basic Query Query<Repository> query = datastore.createQuery(Repository.class); Repository repository = query.get(); ! runQuery called morphia-demo.repos {} ! List<Repository> repositories = query.asList(); ! runQuery called morphia-demo.repos {} ! Iterable<Repository> fetch = query.fetch(); ! … ?
  • 29. Basic Query Query<Repository> query = datastore.createQuery(Repository.class); query.field("owner").equal(evanchooly).get(); ! runQuery called morphia-demo.repos { owner: { $ref: "users", $id: "evanchooly" } }
  • 30. Complex Entities @Entity("repos") public class Repository { @Id public String name; @Reference public Organization organization; @Reference public GithubUser owner; public Settings settings = new Settings(); }
  • 31. Basic Query — @Property @Entity(value = "users", noClassnameStored = true) public class GithubUser { @Id private String userName; private String fullName; @Property("since") private Date memberSince; }
  • 32. Basic Query — @Property datastore.createQuery(GithubUser.class) .field("memberSince").equal(date).get(); ! GithubUser{userName='evanchooly', fullName='Justin Lee', memberSince=Mon Jun 15 00:00:00 EDT 1987} ! datastore.createQuery(GithubUser.class) .field("since").equal(date).get(); ! GithubUser{userName='evanchooly', fullName='Justin Lee', memberSince=Mon Jun 15 00:00:00 EDT 1987}
  • 33. Updates
  • 34. Updating Entities evanchooly.followers = 12; datastore.save(evanchooly);
  • 35. Multiple Updates UpdateOperations<GithubUser> update = datastore.createUpdateOperations( GithubUser.class) .inc("followers").set("following", 42); Query<GithubUser> query = datastore.createQuery(GithubUser.class) .field(“followers”) .equal(0); datastore.update(query, update); ! update morphia-demo.users query: { followers: 0 } update: { $set: { following: 42 }, $inc: { followers: 1 } }
  • 36. Versioned Updates @Entity("orgs") public class Organization { @Id public String name; @Indexed public Date created; @Version(“v”) private long version; ! … ! }
  • 37. Versioned Updates Organization organization = datastore.createQuery(Organization.class).get(); Organization organization2 = datastore.createQuery(Organization.class).get(); ! datastore.save(organization); // fine datastore.save(organization); // fine datastore.save(organization2); ! java.util.ConcurrentModificationException: Entity of class org.mongodb.morphia.demo.Organization (id='mongodb',version='1') was concurrently updated.
  • 38. Indexes
  • 39. Indexes — Fields @Entity("orgs") public class Organization { @Id public String name; @Indexed(value = IndexDirection.ASC, unique = false, name = "", dropDups = false, background = false, sparse = false, expireAfterSeconds = -1) public Date created; ! … }
  • 40. Indexes — Fields repl0:PRIMARY> db.orgs.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "morphia-demo.orgs" }, { "v" : 1, "key" : { "created" : 1 }, "name" : "created_1", "ns" : "morphia-demo.orgs" } ]
  • 41. Indexes — Classes @Entity(value = "users", noClassnameStored = true) @Indexes({ @Index(value ="userName, -followers", name = "popular") }) public class GithubUser { @Id public String userName; public String fullName; @Property("since") public Date memberSince; public Date lastActive; @Reference(lazy = true) public List<Repository> repositories = new ArrayList<>(); public int followers = 0; public int following = 0; ! …
  • 42. What’s Next
  • 43. The Road to 1.0 • It’s short • Aggregation support • Text Searching • Improved geo support • Handful of bug fixes
  • 44. Resources • Morphia Homepage https://github.com/mongodb/morphia • Discussions Forum https://groups.google.com/forum/#!forum/ morphia • This presentation and code https://github.com/evanchooly/morphiademo
  • 45. Questions?
  • 46. MongoDB Persistence with Java and Morphia Justin Lee Software Engineer, MongoDB

×