MongoDB + Java - Everything you need to know

11,191 views

Published on

Learn everything you need to know to get started building a MongoDB-based app in Java. We'll explore the relationship between MongoDB and various languages on the Java Virtual Machine such as Java, Scala, and Clojure. From there, we'll examine the popular frameworks and integration points between MongoDB and the JVM including Spring Data and object-document mappers like Morphia.

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

No Downloads
Views
Total views
11,191
On SlideShare
0
From Embeds
0
Number of Embeds
78
Actions
Shares
0
Downloads
186
Comments
0
Likes
14
Embeds 0
No embeds

No notes for slide
  • Replacing objects highly inefficient and should be avoided.
  • MongoDB + Java - Everything you need to know

    1. 1. MongoDB + Java Everything you need to know
    2. 2. Agenda • MongoDB + Java • Driver • ODM's • JVM Languages • Hadoop
    3. 3. Ola, I'm Norberto! Norberto Leite Technical Evangelist Madrid, Spain @nleite norberto@mongodb.com http://www.mongodb.com/norberto
    4. 4. A few announcements…
    5. 5. Announcements
    6. 6. Announcements • Java 3.0.0-beta2 available for testing – Generic MongoCollection – New Codec infrastructure – Asynchronous Support – New core driver • Better support for JVM languages – https://github.com/mongodb/mongo-java- driver/releases/tag/r3.0.0-beta2 • RX Streams – testing only! – https://github.com/rozza/mongo-java-driver- reactivestreams
    7. 7. http://www.humanandnatural.com/data/media/178/badan_jaran_desert_oasis_china.jpg Let's go and test this! http://www.tinypm.com/blog/wp-content/uploads/2015/01/hammer.jpg
    8. 8. Java Driver
    9. 9. Getting Started • Review our documentation – http://docs.mongodb.org/ecosystem/drivers/java/
    10. 10. Connecting http://std3.ru/d3/09/1379154681-d309dacac47a71fb92480157f1d9a0ea.jpg
    11. 11. Connecting public void connect() throws UnknownHostException{ String host = "mongodb://localhost:27017"; //connect to host MongoClient mongoClient = new MongoClient(host); … }
    12. 12. Connecting public void connect() throws UnknownHostException{ String host = "mongodb://localhost:27017"; //connect to host MongoClient mongoClient = new MongoClient(host); … } Client Instance Host Machine
    13. 13. Connecting public void connectServerAddress() throws UnknownHostException{ String host = "mongodb://localhost:27017"; //using ServerAddress ServerAddress serverAddress = new ServerAddress(host); //connect to host MongoClient mongoClient = new MongoClient(serverAddress); … }
    14. 14. Connecting public void connectServerAddress() throws UnknownHostException{ String host = "mongodb://localhost:27017"; //using ServerAddress ServerAddress serverAddress = new ServerAddress(host); //connect to host MongoClient mongoClient = new MongoClient(serverAddress); … } Server Address Instance
    15. 15. Connecting public boolean connectReplicas() throws UnknownHostException{ //replica set members String []hosts = { "mongodb://localhost:30000", "mongodb://localhost:30001", "mongodb://localhost:30000", }; //list of ServerAdress List<ServerAddress> seeds = new ArrayList<ServerAddress>(); for (String h: hosts){ seeds.add(new ServerAddress(h)); } //connect to host MongoClient mongoClient = new MongoClient(seeds); … }
    16. 16. Connecting public boolean connectReplicas() throws UnknownHostException{ //replica set members String []hosts = { "mongodb://localhost:30000", "mongodb://localhost:30001", "mongodb://localhost:30000", }; //list of ServerAdress List<ServerAddress> seeds = new ArrayList<ServerAddress>(); for (String h: hosts){ seeds.add(new ServerAddress(h)); } //connect to host MongoClient mongoClient = new MongoClient(seeds); … } Seed List
    17. 17. Database & Collections public void connectDatabase(final String dbname, final String collname){ //database instance DB database = mongoClient.getDB(dbname); //collection instance DBCollection collection = database.getCollection(collname); https://api.mongodb.org/java/current/com/mongodb/DB.html https://api.mongodb.org/java/current/com/mongodb/DBCollection.html
    18. 18. Security public void connectServerAddress() throws UnknownHostException{ String host = "localhost:27017"; ServerAddress serverAddress = new ServerAddress(host); String dbname = "test"; String userName = "norberto"; char[] password = {'a', 'b', 'c'}; MongoCredential credential = MongoCredential .createMongoCRCredential(userName, dbname, password); MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential)); http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java- driver/#authentication-optional
    19. 19. Writing http://std3.ru/d3/09/1379154681-d309dacac47a71fb92480157f1d9a0ea.jpg
    20. 20. Inserting public boolean insertObject(SimplePojo p){ //lets insert on database `test` String dbname = "test"; //on collection `simple` String collname = "simple"; DBCollection collection = mc.getDB(dbname).getCollection(collname); DBObject document = new BasicDBObject(); document.put("name", p.getName()); document.put("age", p.getAge()); document.put("address", p.getAddress()); //db.simple.insert( { "name": XYZ, "age": 45, "address": "49 Rue de Rivoli, 75001 Paris"}) collection.insert(document);… }
    21. 21. Inserting public boolean insertObject(SimplePojo p){ //lets insert on database `test` String dbname = "test"; //on collection `simple` String collname = "simple"; DBCollection collection = mc.getDB(dbname).getCollection(collname); DBObject document = new BasicDBObject(); document.put("name", p.getName()); document.put("age", p.getAge()); document.put("address", p.getAddress()); //db.simple.insert( { "name": XYZ, "age": 45, "address": "49 Rue de Rivoli, 75001 Paris"}) collection.insert(document); … } Collection instance BSON wrapper
    22. 22. Inserting public boolean insertObject(SimplePojo p){ … WriteResult result = collection.insert(document); //log number of inserted documents log.debug("Number of inserted results " + result.getN() ); … } https://api.mongodb.org/java/current/com/mongodb/DBObject.html https://api.mongodb.org/java/current/com/mongodb/WriteResult.html
    23. 23. Update public long savePojo(SimplePojo p) { DBObject document = BasicDBObjectBuilder.start() .add("name", p.getName()) .add("age", p.getAge()) .add("address", p.getAddress()).get(); //method replaces the existing database document by this new one WriteResult res = this.collection.save(document); //if it does not exist will create one (upsert) return res.getN(); }
    24. 24. Update public long savePojo(SimplePojo p) { DBObject document = BasicDBObjectBuilder.start() .add("name", p.getName()) .add("age", p.getAge()) .add("address", p.getAddress()).get(); //method replaces the existing database document by this new one WriteResult res = this.collection.save(document); //if it does not exist creates one (upsert) return res.getN(); } Save method
    25. 25. Update public long updateAddress( SimplePojo p, Address a ){ //{ 'name': XYZ} DBObject query = QueryBuilder.start("name").is(p.getName()).get(); DBObject address = BasicDBObjectBuilder.start() .add("street", a.getStreet()) .add("city", a.getCity()) .add("number", a.getNumber()) .add("district", a.getDistrict()).get(); DBObject update = BasicDBObjectBuilder.start().add("name", p.getName()).add("age", p.getAge()).add("address", address).get(); return this.collection.update(query, update).getN(); }
    26. 26. Update public long updateAddress( SimplePojo p, Address a ){ //{ 'name': XYZ} DBObject query = QueryBuilder.start("name").is(p.getName()).get(); DBObject address = BasicDBObjectBuilder.start() .add("street", a.getStreet()) .add("city", a.getCity()) .add("number", a.getNumber()) .add("district", a.getDistrict()).get(); DBObject update = BasicDBObjectBuilder.start().add("name", p.getName()).add("age", p.getAge()).add("address", address).get(); return this.collection.update(query, update).getN(); } Query Object Update Object
    27. 27. Update public long updateSetAddress( SimplePojo p, Address a ){ //{ 'name': XYZ} DBObject query = QueryBuilder.start("name").is(p.getName()).get(); DBObject address = BasicDBObjectBuilder.start() .add("street", a.getStreet()) .add("city", a.getCity()) .add("number", a.getNumber()) .add("district", a.getDistrict()).get(); //db.simple.update( {"name": XYZ}, {"$set":{ "address": ...} } ) DBObject update = BasicDBObjectBuilder.start() .append("$set", new BasicDBObject("address", address) ).get(); return this.collection.update(query, update).getN(); }
    28. 28. Update public long updateSetAddress( SimplePojo p, Address a ){ //{ 'name': XYZ} DBObject query = QueryBuilder.start("name").is(p.getName()).get(); DBObject address = BasicDBObjectBuilder.start() .add("street", a.getStreet()) .add("city", a.getCity()) .add("number", a.getNumber()) .add("district", a.getDistrict()).get(); //db.simple.update( {"name": XYZ}, {"$set":{ "address": ...} } ) DBObject update = BasicDBObjectBuilder.start() .append("$set", new BasicDBObject("address", address) ).get(); return this.collection.update(query, update).getN(); } Using $set operator http://docs.mongodb.org/manual/reference/operator/update/
    29. 29. Remove public long removePojo( SimplePojo p){ //query object to match documents to be removed DBObject query = BasicDBObjectBuilder.start().add("name", p.getName()).get(); return this.collection.remove(query).getN(); }
    30. 30. Remove public long removePojo( SimplePojo p){ //query object to match documents to be removed DBObject query = BasicDBObjectBuilder.start() .add("name", p.getName()).get(); return this.collection.remove(query).getN(); } Matching query
    31. 31. Remove All public long removeAll( ){ //empty object removes all documents > db.simple.remove({}) return this.collection.remove(new BasicDBObject()).getN(); } Empty document http://docs.mongodb.org/manual/reference/method/db.collection.remove/
    32. 32. WriteConcern http://std3.ru/d3/09/1379154681-d309dacac47a71fb92480157f1d9a0ea.jpghttp://www.theworldeffect.com/images/6a00e54fa8abf78833011570eedcf1970b-800wi.jpg
    33. 33. WriteConcern //Default WriteConcern w1 = WriteConcern.ACKNOWLEDGED; WriteConcern w0 = WriteConcern.UNACKNOWLEDGED; WriteConcern j1 = WriteConcern.JOURNALED; WriteConcern wx = WriteConcern.REPLICA_ACKNOWLEDGED; WriteConcern majority = WriteConcern.MAJORITY; https://api.mongodb.org/java/current/com/mongodb/WriteConcern.html
    34. 34. WriteConcern.ACKNOWLEDGED
    35. 35. WriteConcern.UNACKNOWLEDGED
    36. 36. WriteConcern.JOURNALED
    37. 37. WriteConcern.REPLICA_ACKNOWLEDGED
    38. 38. WriteConcern public boolean insertObjectWriteConcern(SimplePojo p){ … WriteResult result = collection.insert(document, WriteConcern.REPLICA_ACKNOWLEDGED); //log number of inserted documents log.debug("Number of inserted results " + result.getN() ); //log the last write concern used log.info( "Last write concern used "+ result.getLastConcern() );… }
    39. 39. WriteConcern public boolean insertObjectWriteConcern(SimplePojo p){ … WriteResult result = collection.insert(document, WriteConcern.REPLICA_ACKNOWLEDGED); //log number of inserted documents log.debug("Number of inserted results " + result.getN() ); //log the last write concern used log.info( "Last write concern used "+ result.getLastConcern() ); … } Confirm on Replicas
    40. 40. WriteConcern public void setWriteConcernLevels(WriteConcern wc){ //connection level this.mc.setWriteConcern(wc); //database level this.mc.getDB("test").setWriteConcern(wc); //collection level this.mc.getDB("test").getCollection("simple").setWriteConcern(wc); //instruction level this.mc.getDB("test").getCollection("simple").insert( new BasicDBObject() , wc); }
    41. 41. Bulk Write http://images.fineartamerica.com/images-medium-large/2-my-old-bucket-curt-simpson.jpg
    42. 42. Write Bulk public boolean bulkInsert(List<SimplePojo> ps){ … //initialize a bulk write operation BulkWriteOperation bulkOp = coll.initializeUnorderedBulkOperation(); for (SimplePojo p : ps){ DBObject document = BasicDBObjectBuilder.start() .add("name", p.getName()) .add("age", p.getAge()) .add("address", p.getAddress()).get(); bulkOp.insert(document); } //call the set of inserts bulkOp.execute(); … }
    43. 43. Inserting Bulk public void writeSeveral( SimplePojo p, ComplexPojo c){ //initialize ordered bulk BulkWriteOperation bulk = this.collection.initializeOrderedBulkOperation(); DBObject q1 = BasicDBObjectBuilder.start() .add("name", p.getName()).get(); //remove the previous document bulk.find(q1).removeOne(); //insert the new document bulk.insert( c.encodeDBObject() ); BulkWriteResult result = bulk.execute(); //do something with the result result.getClass(); }
    44. 44. Reading http://std3.ru/d3/09/1379154681-d309dacac47a71fb92480157f1d9a0ea.jpghttp://1.bp.blogspot.com/_uYSlZ_2-VwI/SK0TrNUBVBI/AAAAAAAAASs/5334Tlv0IIY/s1600-h/lady_reading_book.jpg
    45. 45. Read public SimplePojo get(){ //basic findOne operation DBObject doc = this.collection.findOne(); SimplePojo pojo = new SimplePojo(); //casting to destination type pojo.setAddress((String) doc.get("address")); pojo.setAge( (int) doc.get("age") ); pojo.setAddress( (String) doc.get("address") ); return pojo; }
    46. 46. Read public List<SimplePojo> getSeveral(){ List<SimplePojo> pojos = new ArrayList<SimplePojo>(); //returns a cursor DBCursor cursor = this.collection.find(); //iterates over the cursor for (DBObject document : cursor){ //calls factory or transformation method pojos.add( this.transform(document) ); } return pojos; }
    47. 47. Read public List<SimplePojo> getSeveral(){ List<SimplePojo> pojos = new ArrayList<SimplePojo>(); //returns a cursor DBCursor cursor = this.collection.find(); //iterates over the cursor for (DBObject document : cursor){ //calls factory or transformation method pojos.add( this.transform(document) ); } return pojos; } Returns Cursor
    48. 48. Read public List<SimplePojo> getAtAge(int age) { List<SimplePojo> pojos = new ArrayList<SimplePojo>(); DBObject criteria = new BasicDBObject("age", age); // matching operator { "age": age} DBCursor cursor = this.collection.find(criteria); // iterates over the cursor for (DBObject document : cursor) { // calls factory or transformation method pojos.add(this.transform(document)); } return pojos; } Matching operator
    49. 49. Read public List<SimplePojo> getOlderThan( int minAge){ … DBObject criteria = new BasicDBObject( "$gt", new BasicDBObject("age", minAge)); // matching operator { "$gt": { "age": minAge} } DBCursor cursor = this.collection.find(criteria); … }
    50. 50. Read public List<SimplePojo> getOlderThan( int minAge){ … DBObject criteria = new BasicDBObject( "$gt", new BasicDBObject("age", minAge)); // matching operator { "$gt": { "age": minAge} } DBCursor cursor = this.collection.find(criteria); … } Range operator http://docs.mongodb.org/manual/reference/operator/query/
    51. 51. Read & Filtering public String getAddress(final String name) { DBObject criteria = new BasicDBObject("name", name); //we want the address field DBObject fields = new BasicDBObject( "address", 1 ); //and exclude _id too fields.put("_id", 0); // db.simple.find( { "name": name}, {"_id:0", "address":1} ) DBObject document = this.collection.findOne(criteria, fields); //we can check if this is a partial document document.isPartialObject(); return (String) document.get("address"); }
    52. 52. Read & Filtering public String getAddress(final String name) { DBObject criteria = new BasicDBObject("name", name); //we want the address field DBObject fields = new BasicDBObject( "address", 1 ); //and exclude _id too fields.put("_id", 0); // db.simple.find( { "name": name}, {"_id:0", "address":1} ) DBObject document = this.collection.findOne(criteria, fields); //we can check if this is a partial document document.isPartialObject(); return (String) document.get("address"); } Select fields
    53. 53. Read Preference http://2.bp.blogspot.com/-CW3UtTpWoqg/UHhJ0gzjxcI/AAAAAAAAQz0/l_ozSnrwkvo/s1600/Grigori+Semenovich+Sedov+-+Choosing+the+bride+of+Czar+Alexis.jpg
    54. 54. Read Preference //default ReadPreference.primary(); ReadPreference.primaryPreferred(); ReadPreference.secondary(); ReadPreference.secondaryPreferred(); ReadPreference.nearest(); https://api.mongodb.org/java/current/com/mongodb/ReadPreference.html
    55. 55. Read Preference – Tag Aware //read from specific tag DBObject tag = new BasicDBObject( "datacenter", "Porto" ); ReadPreference readPref = ReadPreference.secondary(tag); http://docs.mongodb.org/manual/tutorial/configure-replica-set-tag-sets/
    56. 56. Read Preference public String getName( final int age ){ DBObject criteria = new BasicDBObject("age", age); DBObject fields = new BasicDBObject( "name", 1 ); //set to read from nearest node DBObject document = this.collection.findOne(criteria, fields, ReadPreference.nearest() ); //return the element return (String) document.get("name"); } Read Preference
    57. 57. Aggregation // create our pipeline operations, first with the $match DBObject match = new BasicDBObject("$match", new BasicDBObject("type", "airfare")); // build the $projection operation DBObject fields = new BasicDBObject("department", 1); fields.put("amount", 1); fields.put("_id", 0); DBObject project = new BasicDBObject("$project", fields ); // Now the $group operation DBObject groupFields = new BasicDBObject( "_id", "$department"); groupFields.put("average", new BasicDBObject( "$avg", "$amount")); DBObject group = new BasicDBObject("$group", groupFields); …
    58. 58. Aggregation // Finally the $sort operation DBObject sort = new BasicDBObject("$sort", new BasicDBObject("amount", -1)); // run aggregation List<DBObject> pipeline = Arrays.asList(match, project, group, sort); AggregationOutput output = coll.aggregate(pipeline);
    59. 59. Java ODM's
    60. 60. Morphia
    61. 61. Morphia
    62. 62. Morphia public class Employee { // auto-generated, if not set (see ObjectId) @Id ObjectId id; // value types are automatically persisted String firstName, lastName; // only non-null values are stored Long salary = null; // by default fields are @Embedded Address address; … }
    63. 63. Morphia public class Employee { … //references can be saved without automatic loading Key<Employee> manager; //refs are stored**, and loaded automatically @Reference List<Employee> underlings = new ArrayList<Employee>(); // stored in one binary field @Serialized EncryptedReviews encryptedReviews; //fields can be renamed @Property("started") Date startDate; @Property("left") Date endDate; … }
    64. 64. SpringData
    65. 65. Spring Data http://projects.spring.io/spring-data-mongodb/
    66. 66. Jongo
    67. 67. Jongo http://jongo.org/
    68. 68. Others • DataNucleus JPA/JDO • lib-mongomapper • Kundera • Hibernate OGM
    69. 69. JVM Languages
    70. 70. Scala • http://docs.mongodb.org/ecosystem/drivers/scala/ • Cashbah – Java Driver Wrapper – Idiomatic Scala Interface for MongoDB • Community – Hammersmith – Salat – Reactive Mongo – Lift Web Framework – BlueEyes – MSSD
    71. 71. Clojure http://clojuremongodb.info/
    72. 72. Groovy https://github.com/poiati/gmongo
    73. 73. Hadoop
    74. 74. MongoDB Hadoop Connector • MongoDB and BSON – Input and Output formats • Computes splits to read data • Support for – Filtering data with MongoDB queries – Authentication (and separate for configdb) – Reading from shard Primary directly – ReadPreferences and Replica Set tags (via MongoDB URI) – Appending to existing collections (MapReduce, Pig, Spark)
    75. 75. For More Information Resource Location Case Studies mongodb.com/customers Presentations mongodb.com/presentations Free Online Training education.mongodb.com Webinars and Events mongodb.com/events Documentation docs.mongodb.org MongoDB Downloads mongodb.com/download Additional Info info@mongodb.com
    76. 76. http://cl.jroo.me/z3/v/D/C/e/a.baa-Too-many-bicycles-on-the-van.jpg Questions? @nleite norberto@mongodb.com http://www.mongodb.com/norberto

    ×