Cedric Gatay - c.gatay@code-troopers.com
MongoDB
What’s up doc ?
Cedric Gatay - c.gatay@code-troopers.com
Cedric Gatay
• Code-Troopers	

• Packt	

• github.com/CedricGatay	

• @Cedric_Gatay
2
Cedric Gatay - c.gatay@code-troopers.com
NoSQL
3
Clef /Valeur Colonne Graphe Document
Cedric Gatay - c.gatay@code-troopers.com
Clef /Valeur
• Énormes volumes	

• Stockage basique	

• Pas de types de données	

• Opérations simples
4
Cedric Gatay - c.gatay@code-troopers.com
Colonne
• Stockage inverse du relationnel	

• Index pour retrouver une donnée	

• Opérations simples
5
Cedric Gatay - c.gatay@code-troopers.com
Graphe
• Stocke les noeuds et arcs	

• Requêtes évoluées	

• Opérations simples
6
Cedric Gatay - c.gatay@code-troopers.com
Document
• Ensemble de champs nommés	

• typage optionnel	

• Requêtes évoluées	

• Opérations plus complètes
7
Cedric Gatay - c.gatay@code-troopers.com
MongoDB
8
Cedric Gatay - c.gatay@code-troopers.com
MongoDB
9
Cedric Gatay - c.gatay@code-troopers.com
Caractéristiques
• Pas de schéma	

• Pas de transactions "longues"	

• Pas de jointures
10
Cedric Gatay - c.gatay@code-troopers.com
Caractéristiques
• Stockage en BSON	

• JSON	

• Typage	

• Compressé
11
Cedric Gatay - c.gatay@code-troopers.com
Caractéristiques
• Hautes performances	

• C++	

• Grosse utilisation mémoire	

• Lectures / Écritures rapides
12
Cedric Gatay - c.gatay@code-troopers.com
Caractéristiques
• Scalable	

• failover automatique	

• distribution des lectures	

• distribution des données
13
Cedric Gatay - c.gatay@code-troopers.com
Populaire
14
Cedric Gatay - c.gatay@code-troopers.com
Document
15
> cg = {name : 'Cedric Gatay', age : 30}
> db.troopers.save(cg)
• Enregistrer une entrée
Cedric Gatay - c.gatay@code-troopers.com
Requêtes
16
> db.troopers.find()
{
_id : ObjectId("ef43a2c954236e2d65a81c5e"),
name : 'Cedric Gatay',
age : 30
}
Cedric Gatay - c.gatay@code-troopers.com
Requêtes
17
> db.troopers.find({name: 'Darth Vader'})
{
_id : ObjectId("133713371337133713371337"),
name : 'Darth Vader',
age : 42
}
Cedric Gatay - c.gatay@code-troopers.com
Opérateurs
18
# $all, $exists, $mod, $ne, $in, $nin, $nor,
# $or, $size, $type, $lt, $lte, $gt, $gte
> db.troopers.find({age: {$gte : 30}}).count()
1
• Opérateurs
Cedric Gatay - c.gatay@code-troopers.com
Documents imbriqués
19
{
_id : ObjectId("ef43a2c954236e2d65a81c5e"),
name : 'Cedric Gatay',
age : 30,
projects : [{
name : 'Aucard de Tours',
stack : 'Android',
status : 'Done'
},{
name : 'Nowly',
stack : 'Android'
}]
}
Cedric Gatay - c.gatay@code-troopers.com
Map / Reduce
• Répartition de calcul	

• Permet la fusion de données	

• Fonctions JavaScript
20
Cedric Gatay - c.gatay@code-troopers.com
Utilisation depuis Java
• Morphia	

• mapping pseudo JPA	

• POJO pour collections	

• API pour les queries
21
Cedric Gatay - c.gatay@code-troopers.com
Morphia POJO
22
@Entity("employees")
class Employee {
@Id ObjectId id;
String firstName, lastName;
}
Cedric Gatay - c.gatay@code-troopers.com
Morphia Query
23
Datastore ds = new Morphia(/*...*/);
!
ds.save(new Employee("John", "Doe"));
!
Employee john =
ds.find(Employee.class)
.field("firstName")
.equal("John")
.get();
Cedric Gatay - c.gatay@code-troopers.com
Morphia : résumé
• JPA est adapté au relationnel	

• Le modèle document est assez éloigné	

• Certains concepts ne sont pas traduits
24
Cedric Gatay - c.gatay@code-troopers.com
Utilisation depuis Java
• Jongo	

• Jackson pour le marshalling	

• Driver Java attaqué directement
25
Cedric Gatay - c.gatay@code-troopers.com
Jongo POJO
26
class Employee {
ObjectId id;
String firstName, lastName;
}
Cedric Gatay - c.gatay@code-troopers.com
Jongo Query
27
MongoCollection employees =
jongo.getCollection("employees");
!
employees.insert("{firstName:’John’,
lastName:’Doe’}"));
!
Employee john =
employees
.findOne("{firstName:’John’}")
.as(Employee.class);
Cedric Gatay - c.gatay@code-troopers.com
Jongo : résumé
• Attention aux refactors	

• Utilise directement le Driver Java	

• Le plus rapide !
28
Cedric Gatay - c.gatay@code-troopers.com
MongoDB
Retour d’expérience
Cedric Gatay - c.gatay@code-troopers.com
SRMvision
30
• 10/2008	

• 2 développeurs	

!
• 01/2014	

• 7 développeurs
Cedric Gatay - c.gatay@code-troopers.com31
Gestion d’incidents
Cedric Gatay - c.gatay@code-troopers.com32
Gestion de projets
Cedric Gatay - c.gatay@code-troopers.com33
Gestion de parc
Cedric Gatay - c.gatay@code-troopers.com
Technos
34
Cedric Gatay - c.gatay@code-troopers.com35
Retour aux sources
Cedric Gatay - c.gatay@code-troopers.com
Besoin initial
• Restitution statistiques décideurs	

• Analyse de la qualité de prestations	

• Résolution incidents	

• Complétude parc
36
Cedric Gatay - c.gatay@code-troopers.com
Stats v1
• État de l'art	

• Business intelligence	

• Intégration Mondrian
37
Cedric Gatay - c.gatay@code-troopers.com
Architecture
• Mondrian rempli par un cron	

• Activité SQL lourde	

• Construction cube CPU bound	

• Schéma OLAP dans un XML	

• Requêtes SQL en base	

• Requêtes MDX en base
38
Cedric Gatay - c.gatay@code-troopers.com
Avantages
• Fonctionne bien	

• Mise en place rapide	

• OLAP est connu
39
Cedric Gatay - c.gatay@code-troopers.com
Inconvénients
!
• Complexe à comprendre et à maintenir	

• Apprentissage du MDX	

• Mises à jour difficiles	

• Synchronisation SQL > OLAP
40
Cedric Gatay - c.gatay@code-troopers.com41
Pendant ce temps…
Cedric Gatay - c.gatay@code-troopers.com
Analyse utilisation
plateforme
• Google Analytics like 	

• Collecte info browser / user	

• Prétexte d'essai NoSQL
42
Cedric Gatay - c.gatay@code-troopers.com
État de l'art NoSQL
!
• Etudes différents paradigmes	

• MongoDB 	

• approche document évolutive	

• communauté active	

• le plus proche de notre besoin
43
Cedric Gatay - c.gatay@code-troopers.com
Réalisation
• Collecte en JS vers un web service	

• Back-office de consultation	

!
!
• En 2 mois de stage...
44
Cedric Gatay - c.gatay@code-troopers.com
En temps réel
45
Cedric Gatay - c.gatay@code-troopers.com46
Statistiques
Cedric Gatay - c.gatay@code-troopers.com
Évolution des stats
• Construire d’autres stats	

• Permettre au client de construire ses stats	

• Alléger le système
47
Cedric Gatay - c.gatay@code-troopers.com
Une stat
• Un ensemble d'axes	

• Documents adaptés	

• Des mesures	

• Fonctions mathématiques d’agrégation
48
Cedric Gatay - c.gatay@code-troopers.com
Architecture
• Morphia pour un mapping pseudo JPA	

• QueryDSL pour des queries typesafe
49
Cedric Gatay - c.gatay@code-troopers.com
Problèmes
• Passage SQL vers NoSQL	

• Possibilité de resynchronisation	

• Not Only NoSQL
50
Cedric Gatay - c.gatay@code-troopers.com
Fonctionnement
• Écriture des documents	

• Transformation d’entités vers documents	

• Trigger JPA pour lancer la copie
51
Cedric Gatay - c.gatay@code-troopers.com
Youpi les données sont
dans MongoDB
Il ne reste "qu’à" les transformer
52
Cedric Gatay - c.gatay@code-troopers.com53
Sous le capot
Comment ça se passe pour les développeurs ?
Cedric Gatay - c.gatay@code-troopers.com
Décrire les documents
• Un POJO par document	

• Annotations	

• pour les axes	

• pour les mesures
54
Cedric Gatay - c.gatay@code-troopers.com
Faire les calculs
• Écrire les fonctions de calcul	

• Associer une annotation par fonction
55
Cedric Gatay - c.gatay@code-troopers.com
Stocker les stats
The easy part…
56
Cedric Gatay - c.gatay@code-troopers.com
Résumé
• Transformation des entités en documents	

• Manipulation avec le Map/Reduce
57
Cedric Gatay - c.gatay@code-troopers.com
IHM
• Complètement générique	

• Basée sur les annotations	

• Traductions à ajouter
58
Cedric Gatay - c.gatay@code-troopers.com59
Dynamique
Cedric Gatay - c.gatay@code-troopers.com60
Tableaux de bord
Cedric Gatay - c.gatay@code-troopers.com
Stats v2
90 jh
61
Cedric Gatay - c.gatay@code-troopers.com62
Difficultés
Cedric Gatay - c.gatay@code-troopers.com
MongoDB
• Appréhender le modèle document	

• Comprendre le map/reduce	

• Idempotence	

• JavaScript	

• Debug
63
Cedric Gatay - c.gatay@code-troopers.com
Faiblesses du système
• JavaScript dans le Java	

• tests importants (> 80% couverture)	

• IntelliJ IDEA @Language
64
Cedric Gatay - c.gatay@code-troopers.com
Faiblesses du système
• Another point of failure?	

• dépendance non critique	

• mongodump/mongorestore
65
Cedric Gatay - c.gatay@code-troopers.com
Faiblesses du système
• Pré-requis supplémentaire	

• Procédure de déploiement	

• Tests d’intégration
66
Cedric Gatay - c.gatay@code-troopers.com
Bilan
• Système compréhensible	

• Liberté laissée aux clients	

!
• On a mis en prod MongoDB !
67
Cedric Gatay - c.gatay@code-troopers.com68
Astuces
Cedric Gatay - c.gatay@code-troopers.com
Astuces
• GUI	

• MongoHub (Mac)	

• RoboMongo
69
Cedric Gatay - c.gatay@code-troopers.com
Astuces
• Montée en version	

• lire le changelog	

• tester dans une sandbox
70
Cedric Gatay - c.gatay@code-troopers.com
Astuces
• Debug Map/Reduce	

• substitution emit	

• print	

• tojson
71
Cedric Gatay - c.gatay@code-troopers.com
Merci
72
Cedric Gatay - c.gatay@code-troopers.com
Credits
• https://www.flickr.com/photos/mongodb/7416213536/sizes/o/	

• https://www.flickr.com/photos/jenny-pics/6110974997/sizes/o/	

• https://www.flickr.com/photos/artbystevejohnson/8479160301/sizes/o/	

• https://www.flickr.com/photos/wbaiv/6268300461/sizes/o/	

• https://www.flickr.com/photos/st3f4n/4360212268/sizes/o/	

• https://www.flickr.com/photos/afagen/8297146248/sizes/o	

• https://www.flickr.com/photos/anubisabyss/9639217548/sizes/o	

• https://www.flickr.com/photos/garrettheath/9371876094/sizes/o	

• Trademarks belong to their respective owners
73

ToursJUG mongoDB

  • 1.
    Cedric Gatay -c.gatay@code-troopers.com MongoDB What’s up doc ?
  • 2.
    Cedric Gatay -c.gatay@code-troopers.com Cedric Gatay • Code-Troopers • Packt • github.com/CedricGatay • @Cedric_Gatay 2
  • 3.
    Cedric Gatay -c.gatay@code-troopers.com NoSQL 3 Clef /Valeur Colonne Graphe Document
  • 4.
    Cedric Gatay -c.gatay@code-troopers.com Clef /Valeur • Énormes volumes • Stockage basique • Pas de types de données • Opérations simples 4
  • 5.
    Cedric Gatay -c.gatay@code-troopers.com Colonne • Stockage inverse du relationnel • Index pour retrouver une donnée • Opérations simples 5
  • 6.
    Cedric Gatay -c.gatay@code-troopers.com Graphe • Stocke les noeuds et arcs • Requêtes évoluées • Opérations simples 6
  • 7.
    Cedric Gatay -c.gatay@code-troopers.com Document • Ensemble de champs nommés • typage optionnel • Requêtes évoluées • Opérations plus complètes 7
  • 8.
    Cedric Gatay -c.gatay@code-troopers.com MongoDB 8
  • 9.
    Cedric Gatay -c.gatay@code-troopers.com MongoDB 9
  • 10.
    Cedric Gatay -c.gatay@code-troopers.com Caractéristiques • Pas de schéma • Pas de transactions "longues" • Pas de jointures 10
  • 11.
    Cedric Gatay -c.gatay@code-troopers.com Caractéristiques • Stockage en BSON • JSON • Typage • Compressé 11
  • 12.
    Cedric Gatay -c.gatay@code-troopers.com Caractéristiques • Hautes performances • C++ • Grosse utilisation mémoire • Lectures / Écritures rapides 12
  • 13.
    Cedric Gatay -c.gatay@code-troopers.com Caractéristiques • Scalable • failover automatique • distribution des lectures • distribution des données 13
  • 14.
    Cedric Gatay -c.gatay@code-troopers.com Populaire 14
  • 15.
    Cedric Gatay -c.gatay@code-troopers.com Document 15 > cg = {name : 'Cedric Gatay', age : 30} > db.troopers.save(cg) • Enregistrer une entrée
  • 16.
    Cedric Gatay -c.gatay@code-troopers.com Requêtes 16 > db.troopers.find() { _id : ObjectId("ef43a2c954236e2d65a81c5e"), name : 'Cedric Gatay', age : 30 }
  • 17.
    Cedric Gatay -c.gatay@code-troopers.com Requêtes 17 > db.troopers.find({name: 'Darth Vader'}) { _id : ObjectId("133713371337133713371337"), name : 'Darth Vader', age : 42 }
  • 18.
    Cedric Gatay -c.gatay@code-troopers.com Opérateurs 18 # $all, $exists, $mod, $ne, $in, $nin, $nor, # $or, $size, $type, $lt, $lte, $gt, $gte > db.troopers.find({age: {$gte : 30}}).count() 1 • Opérateurs
  • 19.
    Cedric Gatay -c.gatay@code-troopers.com Documents imbriqués 19 { _id : ObjectId("ef43a2c954236e2d65a81c5e"), name : 'Cedric Gatay', age : 30, projects : [{ name : 'Aucard de Tours', stack : 'Android', status : 'Done' },{ name : 'Nowly', stack : 'Android' }] }
  • 20.
    Cedric Gatay -c.gatay@code-troopers.com Map / Reduce • Répartition de calcul • Permet la fusion de données • Fonctions JavaScript 20
  • 21.
    Cedric Gatay -c.gatay@code-troopers.com Utilisation depuis Java • Morphia • mapping pseudo JPA • POJO pour collections • API pour les queries 21
  • 22.
    Cedric Gatay -c.gatay@code-troopers.com Morphia POJO 22 @Entity("employees") class Employee { @Id ObjectId id; String firstName, lastName; }
  • 23.
    Cedric Gatay -c.gatay@code-troopers.com Morphia Query 23 Datastore ds = new Morphia(/*...*/); ! ds.save(new Employee("John", "Doe")); ! Employee john = ds.find(Employee.class) .field("firstName") .equal("John") .get();
  • 24.
    Cedric Gatay -c.gatay@code-troopers.com Morphia : résumé • JPA est adapté au relationnel • Le modèle document est assez éloigné • Certains concepts ne sont pas traduits 24
  • 25.
    Cedric Gatay -c.gatay@code-troopers.com Utilisation depuis Java • Jongo • Jackson pour le marshalling • Driver Java attaqué directement 25
  • 26.
    Cedric Gatay -c.gatay@code-troopers.com Jongo POJO 26 class Employee { ObjectId id; String firstName, lastName; }
  • 27.
    Cedric Gatay -c.gatay@code-troopers.com Jongo Query 27 MongoCollection employees = jongo.getCollection("employees"); ! employees.insert("{firstName:’John’, lastName:’Doe’}")); ! Employee john = employees .findOne("{firstName:’John’}") .as(Employee.class);
  • 28.
    Cedric Gatay -c.gatay@code-troopers.com Jongo : résumé • Attention aux refactors • Utilise directement le Driver Java • Le plus rapide ! 28
  • 29.
    Cedric Gatay -c.gatay@code-troopers.com MongoDB Retour d’expérience
  • 30.
    Cedric Gatay -c.gatay@code-troopers.com SRMvision 30 • 10/2008 • 2 développeurs ! • 01/2014 • 7 développeurs
  • 31.
    Cedric Gatay -c.gatay@code-troopers.com31 Gestion d’incidents
  • 32.
    Cedric Gatay -c.gatay@code-troopers.com32 Gestion de projets
  • 33.
    Cedric Gatay -c.gatay@code-troopers.com33 Gestion de parc
  • 34.
    Cedric Gatay -c.gatay@code-troopers.com Technos 34
  • 35.
    Cedric Gatay -c.gatay@code-troopers.com35 Retour aux sources
  • 36.
    Cedric Gatay -c.gatay@code-troopers.com Besoin initial • Restitution statistiques décideurs • Analyse de la qualité de prestations • Résolution incidents • Complétude parc 36
  • 37.
    Cedric Gatay -c.gatay@code-troopers.com Stats v1 • État de l'art • Business intelligence • Intégration Mondrian 37
  • 38.
    Cedric Gatay -c.gatay@code-troopers.com Architecture • Mondrian rempli par un cron • Activité SQL lourde • Construction cube CPU bound • Schéma OLAP dans un XML • Requêtes SQL en base • Requêtes MDX en base 38
  • 39.
    Cedric Gatay -c.gatay@code-troopers.com Avantages • Fonctionne bien • Mise en place rapide • OLAP est connu 39
  • 40.
    Cedric Gatay -c.gatay@code-troopers.com Inconvénients ! • Complexe à comprendre et à maintenir • Apprentissage du MDX • Mises à jour difficiles • Synchronisation SQL > OLAP 40
  • 41.
    Cedric Gatay -c.gatay@code-troopers.com41 Pendant ce temps…
  • 42.
    Cedric Gatay -c.gatay@code-troopers.com Analyse utilisation plateforme • Google Analytics like • Collecte info browser / user • Prétexte d'essai NoSQL 42
  • 43.
    Cedric Gatay -c.gatay@code-troopers.com État de l'art NoSQL ! • Etudes différents paradigmes • MongoDB • approche document évolutive • communauté active • le plus proche de notre besoin 43
  • 44.
    Cedric Gatay -c.gatay@code-troopers.com Réalisation • Collecte en JS vers un web service • Back-office de consultation ! ! • En 2 mois de stage... 44
  • 45.
    Cedric Gatay -c.gatay@code-troopers.com En temps réel 45
  • 46.
    Cedric Gatay -c.gatay@code-troopers.com46 Statistiques
  • 47.
    Cedric Gatay -c.gatay@code-troopers.com Évolution des stats • Construire d’autres stats • Permettre au client de construire ses stats • Alléger le système 47
  • 48.
    Cedric Gatay -c.gatay@code-troopers.com Une stat • Un ensemble d'axes • Documents adaptés • Des mesures • Fonctions mathématiques d’agrégation 48
  • 49.
    Cedric Gatay -c.gatay@code-troopers.com Architecture • Morphia pour un mapping pseudo JPA • QueryDSL pour des queries typesafe 49
  • 50.
    Cedric Gatay -c.gatay@code-troopers.com Problèmes • Passage SQL vers NoSQL • Possibilité de resynchronisation • Not Only NoSQL 50
  • 51.
    Cedric Gatay -c.gatay@code-troopers.com Fonctionnement • Écriture des documents • Transformation d’entités vers documents • Trigger JPA pour lancer la copie 51
  • 52.
    Cedric Gatay -c.gatay@code-troopers.com Youpi les données sont dans MongoDB Il ne reste "qu’à" les transformer 52
  • 53.
    Cedric Gatay -c.gatay@code-troopers.com53 Sous le capot Comment ça se passe pour les développeurs ?
  • 54.
    Cedric Gatay -c.gatay@code-troopers.com Décrire les documents • Un POJO par document • Annotations • pour les axes • pour les mesures 54
  • 55.
    Cedric Gatay -c.gatay@code-troopers.com Faire les calculs • Écrire les fonctions de calcul • Associer une annotation par fonction 55
  • 56.
    Cedric Gatay -c.gatay@code-troopers.com Stocker les stats The easy part… 56
  • 57.
    Cedric Gatay -c.gatay@code-troopers.com Résumé • Transformation des entités en documents • Manipulation avec le Map/Reduce 57
  • 58.
    Cedric Gatay -c.gatay@code-troopers.com IHM • Complètement générique • Basée sur les annotations • Traductions à ajouter 58
  • 59.
    Cedric Gatay -c.gatay@code-troopers.com59 Dynamique
  • 60.
    Cedric Gatay -c.gatay@code-troopers.com60 Tableaux de bord
  • 61.
    Cedric Gatay -c.gatay@code-troopers.com Stats v2 90 jh 61
  • 62.
    Cedric Gatay -c.gatay@code-troopers.com62 Difficultés
  • 63.
    Cedric Gatay -c.gatay@code-troopers.com MongoDB • Appréhender le modèle document • Comprendre le map/reduce • Idempotence • JavaScript • Debug 63
  • 64.
    Cedric Gatay -c.gatay@code-troopers.com Faiblesses du système • JavaScript dans le Java • tests importants (> 80% couverture) • IntelliJ IDEA @Language 64
  • 65.
    Cedric Gatay -c.gatay@code-troopers.com Faiblesses du système • Another point of failure? • dépendance non critique • mongodump/mongorestore 65
  • 66.
    Cedric Gatay -c.gatay@code-troopers.com Faiblesses du système • Pré-requis supplémentaire • Procédure de déploiement • Tests d’intégration 66
  • 67.
    Cedric Gatay -c.gatay@code-troopers.com Bilan • Système compréhensible • Liberté laissée aux clients ! • On a mis en prod MongoDB ! 67
  • 68.
    Cedric Gatay -c.gatay@code-troopers.com68 Astuces
  • 69.
    Cedric Gatay -c.gatay@code-troopers.com Astuces • GUI • MongoHub (Mac) • RoboMongo 69
  • 70.
    Cedric Gatay -c.gatay@code-troopers.com Astuces • Montée en version • lire le changelog • tester dans une sandbox 70
  • 71.
    Cedric Gatay -c.gatay@code-troopers.com Astuces • Debug Map/Reduce • substitution emit • print • tojson 71
  • 72.
    Cedric Gatay -c.gatay@code-troopers.com Merci 72
  • 73.
    Cedric Gatay -c.gatay@code-troopers.com Credits • https://www.flickr.com/photos/mongodb/7416213536/sizes/o/ • https://www.flickr.com/photos/jenny-pics/6110974997/sizes/o/ • https://www.flickr.com/photos/artbystevejohnson/8479160301/sizes/o/ • https://www.flickr.com/photos/wbaiv/6268300461/sizes/o/ • https://www.flickr.com/photos/st3f4n/4360212268/sizes/o/ • https://www.flickr.com/photos/afagen/8297146248/sizes/o • https://www.flickr.com/photos/anubisabyss/9639217548/sizes/o • https://www.flickr.com/photos/garrettheath/9371876094/sizes/o • Trademarks belong to their respective owners 73