Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Mongo db et java en pratique

5.787 Aufrufe

Veröffentlicht am

MongoFR 23 March 2011 Paris.
MongoDB and Java, example.
Morphia 0.99 and Java driver 2.5

Veröffentlicht in: Technologie
  • DOWNLOAD THAT BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book that can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer that is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story That Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money That the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths that Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • DOWNLOAD THAT BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... Download Full EPUB Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... Download Full doc Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... Download PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... Download doc Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book that can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer that is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story That Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money That the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths that Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • DOWNLOAD THAT BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download Full EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download Full doc Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download doc Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book that can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer that is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story That Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money That the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths that Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • DOWNLOAD THAT BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download Full EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download Full doc Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download doc Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book that can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer that is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story That Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money That the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths that Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • ACCESS that WEBSITE Over for All Ebooks (Unlimited) ......................................................................................................................... DOWNLOAD FULL PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... DOWNLOAD FULL EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m6jJ5M }
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

Mongo db et java en pratique

  1. 1. MongoDB et Java en pratique MongoFR 23/03/2011
  2. 2. Speaker Katia Aresti, Xebia Développeur Java : 5 ans dexpérience MongoDB : 6 mois @karesti blog.xebia.fr jduchess.org/duchess-france 2
  3. 3. Agenda 3
  4. 4. Agenda Pourquoi Mongo DB 3
  5. 5. Agenda Pourquoi Mongo DB Configuration et connexion 3
  6. 6. Agenda Pourquoi Mongo DB Configuration et connexion Objet-Mongo Mapper 3
  7. 7. Agenda Pourquoi Mongo DB Configuration et connexion Objet-Mongo Mapper Recherche textuelle 3
  8. 8. Agenda Pourquoi Mongo DB Configuration et connexion Objet-Mongo Mapper Recherche textuelle Recherche spatiale 3
  9. 9. Agenda Pourquoi Mongo DB Configuration et connexion Objet-Mongo Mapper Recherche textuelle Recherche spatiale Indexation 3
  10. 10. Agenda Pourquoi Mongo DB Configuration et connexion Objet-Mongo Mapper Recherche textuelle Recherche spatiale Indexation Conclusions 3
  11. 11. Agenda Pourquoi Mongo DB Configuration et connexion Objet-Mongo Mapper Recherche textuelle Recherche spatiale Indexation Conclusions Questions 3
  12. 12. Notre système (I) Recherche Paris http://www.parisnet.com/images/parismap_new.gif 4
  13. 13. Notre système (I) Recherche Paris http://www.parisnet.com/images/parismap_new.gif 4
  14. 14. Notre système (I) Recherche Bistro Paris http://www.parisnet.com/images/parismap_new.gif 4
  15. 15. Notre système (I) Recherche Bistro Bistro du Bastille Bistro de Mer Mon grand Bistro MongoDB bistro Boulanger Bistro Jean Bistro votre Plombier ... Paris http://www.parisnet.com/images/parismap_new.gif 4
  16. 16. Notre système (II) POI : Point of Interest TOUTE LA FRANCE !!! http://lh3.ggpht.com/_ViFLVzjIPSk/TR7lvqmu8lI/AAAAAAAAEAg/dHEuf1Dvz6o/Map_France_French-cities.gif 5
  17. 17. Notre système (II) POI : Point of Interest Monuments TOUTE LA FRANCE !!! http://lh3.ggpht.com/_ViFLVzjIPSk/TR7lvqmu8lI/AAAAAAAAEAg/dHEuf1Dvz6o/Map_France_French-cities.gif 5
  18. 18. Notre système (II) POI : Point of Interest Monuments Mairies TOUTE LA FRANCE !!! http://lh3.ggpht.com/_ViFLVzjIPSk/TR7lvqmu8lI/AAAAAAAAEAg/dHEuf1Dvz6o/Map_France_French-cities.gif 5
  19. 19. Notre système (II) POI : Point of Interest Monuments Mairies Médecins TOUTE LA FRANCE !!! http://lh3.ggpht.com/_ViFLVzjIPSk/TR7lvqmu8lI/AAAAAAAAEAg/dHEuf1Dvz6o/Map_France_French-cities.gif 5
  20. 20. Notre système (II) POI : Point of Interest Monuments Mairies Médecins Restaurants TOUTE LA FRANCE !!! http://lh3.ggpht.com/_ViFLVzjIPSk/TR7lvqmu8lI/AAAAAAAAEAg/dHEuf1Dvz6o/Map_France_French-cities.gif 5
  21. 21. Notre système (II) POI : Point of Interest Monuments Mairies Médecins Restaurants ... TOUTE LA FRANCE !!! http://lh3.ggpht.com/_ViFLVzjIPSk/TR7lvqmu8lI/AAAAAAAAEAg/dHEuf1Dvz6o/Map_France_French-cities.gif 5
  22. 22. Notre système (II) POI : Point of Interest Monuments Mairies Médecins Restaurants ... mais aussi le mode contributif pour ajouter mes lieux personnels ... TOUTE LA FRANCE !!! http://lh3.ggpht.com/_ViFLVzjIPSk/TR7lvqmu8lI/AAAAAAAAEAg/dHEuf1Dvz6o/Map_France_French-cities.gif 5
  23. 23. Notre système (III)En bref : 6
  24. 24. Notre système (III) En bref : Modèle de données très hétérogène 6
  25. 25. Notre système (III) En bref : Modèle de données très hétérogène Relations simples (1 - 1) et absence de transactions complexes 6
  26. 26. Notre système (III) En bref : Modèle de données très hétérogène Relations simples (1 - 1) et absence de transactions complexes Recherche par mot-clé (full-text à venir) 6
  27. 27. Notre système (III) En bref : Modèle de données très hétérogène Relations simples (1 - 1) et absence de transactions complexes Recherche par mot-clé (full-text à venir) Recherche spatiale 6
  28. 28. Notre système (III) En bref : Modèle de données très hétérogène Relations simples (1 - 1) et absence de transactions complexes Recherche par mot-clé (full-text à venir) Recherche spatiale Fort trafic et performance en temps réel 6
  29. 29. Notre système (III) En bref : Modèle de données très hétérogène Relations simples (1 - 1) et absence de transactions complexes Recherche par mot-clé (full-text à venir) Recherche spatiale Fort trafic et performance en temps réel Scalabilité horizontale 6
  30. 30. Notre système (III) En bref : Modèle de données très hétérogène Relations simples (1 - 1) et absence de transactions complexes Recherche par mot-clé (full-text à venir) Recherche spatiale Fort trafic et performance en temps réel Scalabilité horizontale Délais très courts de livraison 6
  31. 31. Notre système (III) En bref : Modèle de données très hétérogène Relations simples (1 - 1) et absence de transactions complexes Recherche par mot-clé (full-text à venir) Recherche spatiale Fort trafic et performance en temps réel Scalabilité horizontale Délais très courts de livraison Equipe Java 6
  32. 32. Notre système (IV) HTTP Java Driver ... REST BD de POI Services XML CSV SOAP REST Sources ... http://www.openclipart.org/ 7
  33. 33. Geoentity Java public class Geoentity { public class Message { private String id; private String id; private long updated; private String text; private String name; 1 .. N private String userId; private String userId; private long created; } private Set keywords; private List<Message> messages; public class Location { 1 .. 1 private Location location; private double lat; } private double lng; } 8
  34. 34. Geoentity Java public class Geoentity { public class Message { private String id; private String id; private long updated; private String text; private String name; 1 .. N private String userId; private String userId; private long created; } private Set keywords; private List<Message> messages; public class Location { 1 .. 1 private Location location; private double lat; } private double lng; } Une collection : «places» 8
  35. 35. Configurer la connexion (I) Maven <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.5</version> </dependency> 9
  36. 36. Configurer la connexion (I) Maven <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.5</version> </dependency> Suivez le projet sur GitHub ! 9
  37. 37. Configurer la connexion (II) IoC : Guice @Singleton public class MongoDB { private Mongo mongo; private DB db; @Inject public MongoDB(String host, int port, int nb) { MongoOptions mongoOpts = new MongoOptions(); mongoOptions.connectionsPerHost = nb; try { mongo = new Mongo(new ServerAddress(host, port), mongoOpts); } catch (Exception e) { throw new RuntimeException(e); } db = mongo.getDB(DATABASE_NAME); } } 10
  38. 38. Configurer la connexion (II) IoC : Guice @Singleton public class MongoDB { private Mongo mongo; private DB db; @Inject Attention ! public MongoDB(String host, int port, int nb) { MongoOptions mongoOpts = new MongoOptions(); mongoOptions.connectionsPerHost = nb; try { mongo = new Mongo(new ServerAddress(host, port), mongoOpts); } catch (Exception e) { throw new RuntimeException(e); } db = mongo.getDB(DATABASE_NAME); } } 10
  39. 39. Objet - BSON (I) API driver Java (notre choix initial) public DBObject toBasicDBObject() { BasicDBObject dbObject = new BasicDBObject(); if (this.id != null && ObjectId.isValid(this.id)) { dbObject.put("_id", new ObjectId(this.id)); } dbObject.put("name", this.name); dbObject.put("keywords", new BasicDBObject(this.hobbies)); ... dbObject.put("messages", messages); return dbObject; } 11
  40. 40. Objet - BSON (II) Morphia (notre choix actuel) @Entity(noClassnameStored = true) @Embedded public class Geoentity { public class Message { private String id; private String id; ... private long updated; ... // get - set private Set keywords; } @Embedded @Embedded public class Location { private List<Message> messages; private double lat; @Embedded private Location location; private double lng; //get-set //get - set } } 12
  41. 41. Objet - BSON (III) Morphia Object Wrapper@Singletonpublic class MapperMorphia { private Morphia morphia; @Inject public MapperMorphia() { this.morphia = new Morphia(); this.morphia.mapPackage("com.myproject.model"); } public <T extends Object> T from(Class<T> entityClass, DBObject dbObject) { return this.morphia.fromDBObject(entityClass, dbObject); } public <T extends Object> DBObject toDBObject(T modelObject) { return this.morphia.toDBObject(modelObject); }} 13
  42. 42. Objet - BSON (III) Morphia Object Wrapper@Singletonpublic class MapperMorphia { private Morphia morphia; @Inject public MapperMorphia() { this.morphia = new Morphia(); this.morphia.mapPackage("com.myproject.model"); Attention ! } public <T extends Object> T from(Class<T> entityClass, DBObject dbObject) { return this.morphia.fromDBObject(entityClass, dbObject); } public <T extends Object> DBObject toDBObject(T modelObject) { return this.morphia.toDBObject(modelObject); }} 13
  43. 43. Objet - BSON (IV) Morphia Wrapper ... Re-Wrapper !public class GeoentityMapper { private MapperMorphia mapperMorphia; @Inject public GeoentityMapper(MapperMorphia mapperMorphia) { this.mapperMorphia = mapperMorphia; } public BasicDBObject map(Geoentity geoentity) { BasicDBObject dbObject = (BasicDBObject) mapperMorphia.toDBObject(geoentity); if (!Strings.isNullOrEmpty(geoentity.getId())) { dbObject.put("_id", new ObjectId(geoentity.getId())); } return dbObject; } public Geoentity map(BasicDBObject rootObject) { return mapperMorphia.fromDBObject(Geoentity.class, rootObject); }} 14
  44. 44. Objet - BSON (IV) Morphia Wrapper ... Re-Wrapper !public class GeoentityMapper { private MapperMorphia mapperMorphia; @Inject public GeoentityMapper(MapperMorphia mapperMorphia) { this.mapperMorphia = mapperMorphia; } Pour ObjectId -> String ! public BasicDBObject map(Geoentity geoentity) { BasicDBObject dbObject = (BasicDBObject) mapperMorphia.toDBObject(geoentity); if (!Strings.isNullOrEmpty(geoentity.getId())) { dbObject.put("_id", new ObjectId(geoentity.getId())); } return dbObject; } public Geoentity map(BasicDBObject rootObject) { return mapperMorphia.fromDBObject(Geoentity.class, rootObject); }} 14
  45. 45. CRUD (I) Créer un repository public class GeoentityRepository { private final MongoDB mongoDB; private final GeoentityMapper geoentityMapper; @Inject public GeoentityRepository(MongoDB mongoDB, GeoentityMapper geoentityMapper) { this.mongoDB = mongoDB; this.geoentityMapper = geoentityMapper; } protected DBCollection getCollection() { DBCollection collection = mongoDB.getDB().getCollection(NAME); return collection; } // autres méthodes ici } 15
  46. 46. CRUD (II) insert/save> db.places.insert({"name" : "my place" , "location" :{ "lat": 48.0130, "lng" : 21.050505}})DBObject dbObject = mapper.map(geoentity);getCollection("places").insert(dbObject);ObjectId id = (ObjectId) dbObject.get("_id");geoentity.setId(id.toString()); 16
  47. 47. CRUD (II) insert/save> db.places.insert({"name" : "my place" , "location" :{ "lat": 48.0130, "lng" : 21.050505}})DBObject dbObject = mapper.map(geoentity);getCollection("places").insert(dbObject);ObjectId id = (ObjectId) dbObject.get("_id");geoentity.setId(id.toString()); Si l’écriture échoue, Mongo ne dira rien ... 16
  48. 48. CRUD (II) insert/save> db.places.insert({"name" : "my place" , "location" :{ "lat": 48.0130, "lng" : 21.050505}})DBObject dbObject = mapper.map(geoentity);getCollection("places").insert(dbObject);ObjectId id = (ObjectId) dbObject.get("_id");geoentity.setId(id.toString()); Si l’écriture échoue, Mongo ne dira rien ... ... sauf si vous activez le «Write Concern» à SAFE ! collection.setWriteConcern(WriteConcern.SAFE); 16
  49. 49. CRUD (III) find by Id> db.places.find({"_id" : ObjectId("4d6d42abf22737543cc01c0b")})DBObject query = QueryBuilder.start("_id").is(new ObjectId(id))BasicDBObject dbObject = getCollection("places").findOne(query.get());if (null == dbObject) { return null;} 17
  50. 50. CRUD (IV) Remove> db.places.remove({"_id" : ObjectId("4d6d42abf22737543cc01c0b")})QueryBuilder builder = QueryBuilder.start("_id").is(new ObjectId(id));getCollection().remove(builder.get()); 18
  51. 51. CRUD (V) Modifier un message et la date de mise à jour > db.places.update({"_id" : ObjectId("foo") , "messages.id" : "bar" }, {$set : { "message.$.text" : "my new text", "updated" : "dateFoo"} }) DBObject searchQuery = QueryBuilder.start("_id").is(new ObjectId(id)) .and("messages.id").is(messageId) .get(); BasicDBObject updateObject = new BasicDBObject("messages.$.text", "my new text"); updateObject.put("updated", Calendar.getInstance().getTimeInMillis()); DBObject modificatorQuery = new BasicDBObject("$set", updateObject); getCollection().update(searchQuery, modificatorQuery); 19
  52. 52. Recherche textuelle Recherche la valeur dans «name» OR «keywords» > db.places.find({"name" : "foo" , $or : [{"keywords" : "bar"}]}) List<Geoentity> result = new ArrayList<Geoentity>(); QueryBuilder.start().queryBuilder.put("name").is("foo"); queryBuilder.or(new BasicDBObject("keywords", "bar")); DBCursor dbCursor = queryBuilder.get().find(); while (dbCursor.hasNext()) { result.add(geoentityMapper.map(dbCursor.next())); } 20
  53. 53. Recherche Spatiale (II) Near Search public List<Geoentity> circleSearch(Coordinate coordinate, Double radius) { QueryBuilder queryBuilder = QueryBuilder.start().and(LOCATION) .near(coordinate.getLatitude(), coordinate.getLongitude(), radius); ... getCollection().find( queryBuilder.get()) ) .limit(100) .sort("importance"); 21
  54. 54. Recherche Spatiale (II) Near Search 35.000 éléments public List<Geoentity> circleSearch(Coordinate coordinate, Double radius) { QueryBuilder queryBuilder = QueryBuilder.start().and(LOCATION) .near(coordinate.getLatitude(), coordinate.getLongitude(), radius); ... getCollection().find( queryBuilder.get()) ) .limit(100) .sort("importance"); 21
  55. 55. Recherche Spatiale (II) Near Search limit(100) chargera 100 35.000 éléments et sort(«champ») triera les résultats public List<Geoentity> circleSearch(Coordinate coordinate, Double radius) { QueryBuilder queryBuilder = QueryBuilder.start().and(LOCATION) .near(coordinate.getLatitude(), coordinate.getLongitude(), radius); ... getCollection().find( queryBuilder.get()) ) .limit(100) .sort("importance"); 21
  56. 56. Recherche Spatiale (I) BOX ou Circle Search public List<Geoentity> boxSearch (Coordinate bottomLeft, Coordinate topRight) { QueryBuilder queryBuilder = QueryBuilder.start().and(LOCATION) .withinBox(bottomLeft.getLatitude(), bottomLeft.getLongitude(), topRight.getLatitude(), topRight.getLongitude()); getCollection().find(queryBuilder.get())) .limit(100) 22
  57. 57. Recherche Spatiale (I) BOX ou Circle Search public List<Geoentity> boxSearch (Coordinate bottomLeft, Coordinate topRight) { QueryBuilder queryBuilder = QueryBuilder.start().and(LOCATION) .withinBox(bottomLeft.getLatitude(), bottomLeft.getLongitude(), topRight.getLatitude(), topRight.getLongitude()); getCollection().find(queryBuilder.get())) .limit(100) .sort("importance"); 22
  58. 58. Recherche Spatiale (I) BOX ou Circle Search 35.000 éléments public List<Geoentity> boxSearch (Coordinate bottomLeft, Coordinate topRight) { QueryBuilder queryBuilder = QueryBuilder.start().and(LOCATION) .withinBox(bottomLeft.getLatitude(), bottomLeft.getLongitude(), topRight.getLatitude(), topRight.getLongitude()); getCollection().find(queryBuilder.get())) .limit(100) .sort("importance"); 22
  59. 59. Recherche Spatiale (I) BOX ou Circle Search sort() charge les 35.000 éléments. 35.000 éléments Index 2D trie uniquement par distance public List<Geoentity> boxSearch (Coordinate bottomLeft, Coordinate topRight) { QueryBuilder queryBuilder = QueryBuilder.start().and(LOCATION) .withinBox(bottomLeft.getLatitude(), bottomLeft.getLongitude(), topRight.getLatitude(), topRight.getLongitude()); getCollection().find(queryBuilder.get())) .limit(100) .sort("importance"); 22
  60. 60. Indexation (I) Avant de chercher, les indexes doivent exister Index mot-clé DBObject indexObject = BasicDBObjectBuilder.start() .add("name", 1) .add("updated", -1).get(); getCollection().ensureIndex(indexObject); 23
  61. 61. Indexation (II) Index 2D ▶ valeur «2D» ▶ toujours le premier public class Geoentity { public class Location { private Location location; private double lat; } private double lng; } DBObject indexObject2d = BasicDBObjectBuilder.start() .add(LOCATION, "2d") .add("keywords", 1).get(); getCollection().createIndex(indexObject2d); 24
  62. 62. Indexation (II) Index 2D ▶ valeur «2D» ▶ toujours le premier public class Geoentity { public class Location { private Location location; private double lat; } private double lng; private double alt; } DBObject indexObject2d = BasicDBObjectBuilder.start() .add(LOCATION, "2d") .add("keywords", 1).get(); getCollection().createIndex(indexObject2d); 24
  63. 63. Indexation (II) Index 2D ▶ valeur «2D» ▶ toujours le premier L’indexation n’aura pas d’effet public class Geoentity { public class Location { private Location location; private double lat; } private double lng; private double alt; } DBObject indexObject2d = BasicDBObjectBuilder.start() .add(LOCATION, "2d") .add("keywords", 1).get(); getCollection().createIndex(indexObject2d); 24
  64. 64. Conclusions 25
  65. 65. Conclusions Prise en main de MongoDB rapide et ludique 25
  66. 66. Conclusions Prise en main de MongoDB rapide et ludique Montée en compétence rapide 25
  67. 67. Conclusions Prise en main de MongoDB rapide et ludique Montée en compétence rapide Adaptation naturelle à la philosophie 25
  68. 68. Conclusions Prise en main de MongoDB rapide et ludique Montée en compétence rapide Adaptation naturelle à la philosophie Test unitaires / d’intégration sont plus faciles à maintenir 25
  69. 69. Conclusions Prise en main de MongoDB rapide et ludique Montée en compétence rapide Adaptation naturelle à la philosophie Test unitaires / d’intégration sont plus faciles à maintenir Fonctionne bien avec Java 25
  70. 70. Conclusions Prise en main de MongoDB rapide et ludique Montée en compétence rapide Adaptation naturelle à la philosophie Test unitaires / d’intégration sont plus faciles à maintenir Fonctionne bien avec Java Le driver (avec et sans Morphia) reste un peu verbeux mais il s’améliore en continu 25
  71. 71. Conclusions Prise en main de MongoDB rapide et ludique Montée en compétence rapide Adaptation naturelle à la philosophie Test unitaires / d’intégration sont plus faciles à maintenir Fonctionne bien avec Java Le driver (avec et sans Morphia) reste un peu verbeux mais il s’améliore en continu Il est important de mener des tests de performance au plus tôt dans le cycle de développement (JMeter) 25
  72. 72. Conclusions Prise en main de MongoDB rapide et ludique Montée en compétence rapide Adaptation naturelle à la philosophie Test unitaires / d’intégration sont plus faciles à maintenir Fonctionne bien avec Java Le driver (avec et sans Morphia) reste un peu verbeux mais il s’améliore en continu Il est important de mener des tests de performance au plus tôt dans le cycle de développement (JMeter) Il faut bien connaître les options et la configuration 25
  73. 73. Questions / Réponses 26
  74. 74. @karestiblog.xebia.frjduchess.org/duchess-france http://www.toutcaen.com/images/merci.jpg 27

×