SlideShare une entreprise Scribd logo
1  sur  75
MongoDB Introduction
  for Java, Python and PHP Developers
                 Developers
  for Java, Python and PHP Developers
                 Developers
   Covers using MongoDB from a Java, PHP
       and Python developer’s perspective. Uses
       the Official MongoDB driver for Java,
       Python and PHP as well as command line
       tools for MongoDB to teach core concepts.


Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Outline
• Theory and Architecture of MongoDB (49%)
• Setup instructions (9%)
• Code examples in JavaScript, PHP, Python and Java
  (39%)
• 3% random

         © 2012 10gen. MongoDB®, Mongo®, and the
         leaf logo are registered trademarks of 10gen, Inc.

         10gen in no way endorses this slide deck or Mammatus
         Tehcnology Inc.




                                                                               2
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
MongoDB
• NoSQL landscape full of contenders tackling big
  data problems
• MongoDB very capable
• Document-oriented schema-less storage solution
• JSON-style documents to represent, query and
  modify data
• Supports many clients/languages Python, PHP,
  Java, Ruby, C++, etc.


    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Resources
• 10Gen site (http://www.mongodb.org)
  – Great documentation and presentations
• InfoQ articles and presentations
• Wikipedia
• http://mongly.com/




                                                                               4
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Feedback welcome
• Send any and all feedback to
  richardhightower@gmail.com



• Criticism welcome
  – Prefer constructive criticism, but will take any and all
  – Needed for continuous improvement of this slide deck and my
    knowledge




                                                                               5
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Creative Commons
• This slide deck and all material therein are covered
  under creative commons
• You can use all material in here as long as you don’t
  copy it word for word and then use it for commercial
  reasons
• Other material in here from other sources are
  covered under fair use
• http://creativecommons.org/



                                                                               6
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Why MongoDB?
• MongoDB great active community
Supports:
• High availability,
• Journaling
• Replication,
• Sharding,
                         MongoDB commercial
• Indexing,              http://www.10gen.com/what-is-mongodb

• Aggregation,
• Map/Reduce

      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
MongoDB is a top job trend




Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Leader in the NoSQL space?




MongoDB seems to be
the clear mind-share leader

Cassandra a close second




                                                                                 9
       Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Why do Developers pick Mongo?
• NoSQL, in general, can be more agile than full
  RDBMS/ SQL
  – problems with schema migration
  – a lot of upfront design needed for RDBMS
  – (or a lot schema migration later)
• MongoDB does not require a lot of ramp up time
  –   Easy to get started
  –   Many DevOps things come for free
  –   Easy on ramp for NoSQL
  –   Gateway drug?


                                                                                10
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Why PHP, Python and Java?




                Might add Ruby later to the mix or just focus on these



                                                                          11
Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Built for speed | Cache built in
• MongoDB was built with speed in mind
• Speed shaped architecture of MongoDB
• Uses binary protocol instead of HTTP text/
  (CouchDB)
• Pads disk space around document
  – faster updates
  – uses more disk
• Uses memory-mapped files as default storage
  engine, letting OS manage swapping
  – Linux/Windows/Solaris really good at virtual memory...
    MongoDB builds on top of this
                                                                               12
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Negatives of MongoDB
• Indexes are not as flexible as
  Oracle/MySQL/Postgres or other NoSQL solutions
  – Order of index matters, uses B-Trees, not very many options
    like more mature solutions
• Realtime queries might not be as fast as
  Oracle/MySQL and other NoSQL solutions
• Good enough if queries are simple
• Probably hits the sweet spot of 20/80 rule
• Not as mature as RDBMS
• Does not have full text search engine

                                                                             13
   Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Very useful still
• Every version seems to add more features
• Added journaling so they can have single server
  durability
• Improved Replica’s with Replica Sets
• Replica Sets and Autosharding required very little
  admin once running
• What it does, it does well...
  – Can be combined with Relational database
  – Can be combined with full text search (Solr)
  – Can be combined with Hadoop

                                                                               14
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Who uses MongoDB?

Big names, big data
• MTV                                                               •    SAP
• Craigslist                                                        •    Forbes
• Disney                                                            •    National Archives UK
• Shutterfly                                                        •    Intuit
• foursqaure                                                        •    github
• bit.ly                                                            •    LexisNexis
• The New York Times                                                •    many more
• Barclay’s
• The Guardian

                                                                                                15
   Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
MongoDB Concept
•   Oracle: Schema, Tables, Rows, Columns
•   MySQL: Database, Tables, Rows, Columns
•   MongoDB: Database, Collections, Document, Fields
•   MySQL/Oracle: Indexes
•   MongoDB: Indexes
•   MySQL/Oracle: Stored Procedures
•   MongoDB: Stored JavaScript
•   Oracle/MySQL: Database Schema
•   MongoDB: Schema free!


       Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
MongoDB Architecture




Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.   17
Additional Mongo Features
• Geo Indexing: How close am I to X?
• File Storage
   – Stores large files and file meta-data
• Capped Collection (like Ring Buffer)
   – Older documents auto-deleted
• Aggregation
• Auto sharding
• Load sharing for reads
• High availability
• Speed or durability (journaling)



                                                                                18
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
What do you need?
• Read scalability and high availability (HA)?
                                                                               HA
   – Use Replica Sets
• Write scalability?
   – Use Autosharding (also just called sharding)
• HA, Read Scalability, and Write Scalability?
   – Use Autosharding and Replica Sets
• You can start basic and add as your growth/needs
  change
   – Capacity planning, monitoring, determine needs



                                                                                    19
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Durability
• Journaling added in 1.8, and is now default for 64 bit
  OS for MongoDB 2.0
• Prior to that, you used replication to make sure copy
  of operation was on replica
   – MongoDB did not have single server durability, now it does
     with addition of journaling
• General thought was/is durability is overvalued
• You can also force an fsync
• See links in notes


                                                                               20
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Replica Sets
• Drivers know the primary
• If primary down, Drivers                                                      Replica 1                Replica 2
  know how to get new
  primary
• Data is replicated after
  writing
• Typical to have three in a
  replica set                                                                                Replica 0
• You can do more                                                                            PRIMARY
• Load sharing for reads                                                                                         Read
                                                                                               Read/Write


                                                                                            Client Driver




                                                                                                                     21
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Replica Sets Usage
•   Business Continuity
•   Data Redundancy
•   High Availability
•   Load sharing (reads)
•   “Just works / NoOps (low ops)”




                                                                                 22
       Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Replica Sets
•   Non-blocking master/slave replication
•   Auto failover
•   Two or more nodes (usually three)
•   No primary, master is nominated
•   Share nothing architecture
•   Brains in the client libraries
•   Client libraries (Drivers) are Replica Set aware
•   Client can block until data is replicated on all servers
    (for important data)

                                                                                 23
       Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Replica Sets
• You only write to the master, it then replicates to
  slaves
• Replication is by default async (non-blocking)
• Slave data and write data can be out of sync
   – There are workarounds
   – You can force master to sync to master before
     continuing (blocking, sync)
• Sync blocking is slower
• Async non-blocking is faster (eventual consistency)

                                                                               24
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Durability and Replica Sets
• Client libraries can do the following:

• Wait until write has happened on all replicas
• Wait until write is on two servers (primary and one
  other)
• Wait until write has occurred on majority of replicas
• Wait until write operation has been written to
  journal



                                                                               25
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Replica Sets
• Replica Sets and Autosharding go hand in hand for
  mass scale out
• Replica Sets are good for failover and speeding up
  reads, but...
• To speed up writes, you need autosharding




                                                                               26
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Sharding
• Sharding allows MongoDB to scale horizontally
• Sharding = partitioning
• Auto-shards
    – load balances
    – changes for data distribution
•   Elastic adding of new nodes
•   Supports automatic failover (along with replica sets)
•   No single point of failure
•   90% of deployments don’t need sharding according
    to Roger Bodamer
                                                                                 27
       Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Non-sharded client connection
• Client Driver talks directly
  to mongod process




                                                                                28
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Autosharded
• Three actors now:
  mongod, mongos, and
  Client Driver library
• Mongod is the process
• Mongos is a router, it
  routes writes to correct
  mongod instance
• Shares writing




                                                                                29
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Autoshard plus Replica Set
• Autosharding increases
  writes, helps with scale
  out
• Replica Sets are for high
  availability
• There is a whole lesson on
  sharding.




                                                                               30
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Sharding Topology
• Config Servers (mongod)
  –   contain versioned shard topology
  –   maps which shard has key
  –   used by mongos
  –   like DNS server for shards
• Mongos
  – Shard router clients drivers talk to Mongos instead of mongod
    directly
  – Mongos uses Config Servers to find shard where key lives
  – MongoD are shards that can be replicated



                                                                                31
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Large deployment




                                                                          32
Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
MapReduce
• Used for batch processing
• Similar to Hadoop,
• Massive aggregation possible through divide and
  conquer
• Used instead of Group/By in SQL
   – Also added simplified framework to MongoDB (aggregation
     framework)
• Map and Reduce functions are written in JavaScript
   – Executed on server, code next to data it is operating on
• Can copy results to results collections

                                                                                33
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
MapReduce Theory




                        Image from http://code.google.com/p/mapreduce-framework/wiki/MapReduce

                                                                                                 34
Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Incremental MapReduce
• Run MapReduce job over collections
• Run a second job but only over new documents in
  collection
• Use reduce output to merge new data into existing
  collection




                                                                               35
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Aggregation Framework
• Added in MongoDB 2.1
• Similar to SQL group by
• Before Aggregation framework, you had to use
  MapReduce for things like SQL group by
• Easier to use than MapReduce




                                                 Table from 10 Gen
                                                 http://www.mongodb.org/display/DOCS/SQL+to+Aggregation+Framework+M
                                                                                                               36
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Aggregation Framework




                                            Table from 10 Gen
                                            http://www.mongodb.org/display/DOCS/SQL+to+Aggregation+Framework+M
                                                                                                          37
Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
MongoDB versus SQL

                                    Compare contrast




Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.   38
SQL to Mongo




From http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart



                                                                              39
    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Mongo versus SQL




From http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

                                                                             40
   Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Mongo versus SQL




From http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

                                                                             41
   Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Mongo vs. SQL




From http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

                                                                             42
   Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Getting Started with Mongo

                       Installing and using Mongo




Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.   43
Install (1 of 2)
• http://www.mongodb.org/downloads
• Extract
  – ~/mongodb-platform-version/
• $ sudo mkdir /etc/mongodb/data
• Create file
  – /etc/mongodb/mongodb.config
• $ cat mongodb.config
  – dbpath=/etc/mongodb/data



    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Install (2 of 2)
• Link:
sudo ln -s ~/mongodb-platform-version/ /usr/local/mongodb
• Add to Path:
export PATH=$PATH:/usr/local/mongodb/bin
• Run the server:
mongod --config /etc/mongodb/mongodb.config




      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Run the client (type db.version())
$ mongo
MongoDB shell version: 2.0.4
connecting to: test
…
> db.version()
2.0.4
>



    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Client: mongo db.help()
> db.help()
DB methods:
         db.addUser(username, password[, readOnly=false])
         db.auth(username, password)
         db.cloneDatabase(fromhost)
         db.commandHelp(name) returns the help for the command
         db.copyDatabase(fromdb, todb, fromhost)
         db.createCollection(name, { size : ..., capped : ..., max : ... } )
         db.currentOp() displays the current operation in the db
         db.dropDatabase()
         db.eval(func, args) run code server-side
         db.getCollection(cname) same as db['cname'] or db.cname
         db.getCollectionNames()



     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
db.help()
db.getLastError() - just returns the err msg string                     db.printCollectionStats()
db.getLastErrorObj() - return full status object                        db.printReplicationInfo()
db.getMongo() get the server connection object                          db.printSlaveReplicationInfo()
                                                                        db.printShardingStatus()
db.getMongo().setSlaveOk() allow this connection
to read from the nonmaster member of a replica                          db.removeUser(username)
pair                                                                    db.repairDatabase()
                                                                        db.resetError()
db.getName()                                                            db.runCommand(cmdObj) run a database command. if
                                                                        cmdObj is a string, turns it into { cmdObj : 1 }
db.getPrevError()
db.getProfilingStatus() - returns if profiling is on                    db.serverStatus()
and slow threshold                                                      db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
db.getReplicationInfo()                                                 db.shutdownServer()
db.getSiblingDB(name) get the db at the same                            db.stats()
server as this one                                                      db.version() current version of the server
db.isMaster() check replica primary status                              db.getMongo().setSlaveOk() allow queries on a replication
db.killOp(opid) kills the current operation in the db                   slave server
db.listCommands() lists all the db commands                             db.fsyncLock() flush data to disk and lock server for
                                                                        backups
db.logout()                                                             db.fsyncUnock() unlocks server following a db.fsyncLock()




       Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Create Employee Collection
> use tutorial;
switched to db tutorial
> db.getCollectionNames();
[]
> db.employees.insert({name:'Rick Hightower',
gender:'m', gender:'m', phone:'520-555-1212', age:42});
Mon Apr 23 23:50:24 [FileAllocator] allocating new
datafile /etc/mongodb/data/tutorial.ns, filling with
zeroes…
..



     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Query DB (1 of 2)
> db.getCollectionNames();
[ "employees", "system.indexes" ]

> db.employees.find()
{ "_id" : ObjectId("4f964d3000b5874e7a163895"),
"name" : "Rick Hightower", "gender" : "m",
"phone" : "520-555-1212", "age" : 42 }




    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Query DB (2 of 2)
> db.employees.find({name:"Bob"})

> db.employees.find({name:"Rick Hightower"})
{ "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m",
"phone" : "520-555-1212", "age" : 42 }

> db.employees.find({age:{$lt:100}})
{ "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m",
"phone" : "520-555-1212", "age" : 42 }

> db.employees.find({age:{$lt:100}})[0].name
Rick Hightower

> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "tutorial.employees", "name" : "_id_" }

> db.employees.find({_id : ObjectId("4f964d3000b5874e7a163895")})
{ "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m",
"phone" : "520-555-1212", "age" : 42 }
          Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Java: Setup
• Download latest mongo driver
  – https://github.com/mongodb/mongo-java-driver/
    downloads


$ mkdir tools/mongodb/lib
$ cp mongo-2.7.3.jar tools/mongodb/lib

Create new Eclipse project in new Workspace


    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Java: Setup Eclipse (1 of 2)

• Right Click Project, Open Properties, Java Build Path->Libraries->Add Variable-
  >Configure Variable




     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Java: Setup Eclipse (2 of 2)

• From Properties->Java Build Path->Libraries
• Click Add Variable, Select MONGO, Click Extend…, select jar
  file you just downloaded




    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Java: Using Java Basics (1 of 2)




Out:
{ "_id" : { "$oid" : "4f964d3000b5874e7a163895"} , "name" : "Rick Hightower" , "gender" : "m" , "phone" : "520-555-1212" , "age" : 42.0}
{ "_id" : { "$oid" : "4f984cce72320612f8f432bb"} , "name" : "Diana Hightower" , "gender" : "f" , "phone" : "520-555-1212" , "age" : 30}
            Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Using Java Basics (2 of 2)




Output:
Rick?
{ "_id" : { "$oid" : "4f964d3000b5874e7a163895"} , "name" : "Rick Hightower" , "gender" : "m" , "phone" : "520-555-1212" , "age" : 42.0}
Diana?
{ "_id" : { "$oid" : "4f984cae72329d0ecd8716c8"} , "name" : "Diana Hightower" , "gender" : "m" , "phone" : "520-555-1212" , "age" : 30}
Diana by object id?
{ "_id" : { "$oid" : "4f984cce72320612f8f432bb"} , "name" : "Diana Hightower" , "gender" : "f" , "phone" : "520-555-1212" , "age" : 30}
             Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
All of the above but in Python
• Install mongodb lib for Python
MAC OSX
$ sudo env ARCHFLAGS='-arch i386 -arch x86_64'
  python -m easy_install pymongo
Linux
$ easy_install pymongo
or
$ pip install pymongo


    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Basic Python Operations (1 of 2)




Output:
{u'gender': u'm', u'age': 42.0, u'_id': ObjectId('4f964d3000b5874e7a163895'), u'name': u'Rick Hightower', u'phone':
u'520-555-1212'}
{u'gender': u'm', u'age': 30, u'_id': ObjectId('4f984cae72329d0ecd8716c8'), u'name': u'Diana Hightower', u'phone':
u'520-555-1212'}
{u'gender': u'm', u'age': 8, u'_id': ObjectId('4f9e111980cbd54eea000000'), u'name': u'Lucas Hightower', u'phone':
u'520-555-1212'}




           Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Basic Python Operations (2 of 2)




Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
All of the above but in PHP
• Install on PHP
$ sudo pecl install mongo

• Add to php.ini:
extension=mongo.so

• Restart apache
$ apachectl stop
$ apachectl start

     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Mongo PHP Basics (1 of 2)




Output:
array ( '_id' => MongoId::__set_state(array( '$id' => '4f964d3000b5874e7a163895', )), 'name' => 'Rick Hightower',
'gender' => 'm', 'phone' => '520-555-1212', 'age' => 42, )

array ( '_id' => MongoId::__set_state(array( '$id' => '4f984cae72329d0ecd8716c8', )), 'name' => 'Diana Hightower', 'gender' => ‘f',
'phone' => '520-555-1212', 'age' => 30, )

array ( '_id' => MongoId::__set_state(array( '$id' => '4f9e170580cbd54f27000000', )), 'gender' => 'm', 'age' => 8, 'name' => 'Lucas Hightower',
'phone' => '520-555-1212', )

               Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Mongo PHP (2 of 2)




Output
Rick?
array ( '_id' => MongoId..., 'name' => 'Rick Hightower', 'gender' => 'm',
'phone' => '520-555-1212', 'age' => 42, )
Diana?
array ( '_id' => MongoId::..., 'name' => 'Diana Hightower', 'gender' => ‘f',
'phone' => '520-555-1212', 'age' => 30, )
Diana by id?
array ( '_id' => MongoId::..., 'name' => 'Diana Hightower', 'gender' => 'f',
'phone' => '520-555-1212', 'age' => 30, )

        Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Shell commands in action
> show dbs
local          (empty)
tutorial       0.203125GB

> show collections
employees
system.indexes

> show users

> show profile
db.system.profile is empty
Use db.setProfilingLevel(2) will enable profiling
..

> show logs
global

> show log global
Mon Apr 23 23:33:14 [initandlisten] MongoDB starting : pid=11773 port=27017 dbpath=/etc/mongodb/data 64-bit…
…
Mon Apr 23 23:33:14 [initandlisten] options: { config: "/etc/mongodb/mongodb.config", dbpath: "/etc/mongodb/data" }




        Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Collection Methods (1 of 2)
> db.employees.help()
DBCollection help

db.employees.find().help() - show DBCursor help
db.employees.count()
db.employees.dataSize()
db.employees.distinct( key ) - eg. db.employees.distinct( 'x' )
db.employees.drop() drop the collection
db.employees.dropIndex(name)
db.employees.dropIndexes()
db.employees.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name,
unique, dropDups
db.employees.reIndex()
db.employees.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
e.g. db.employees.find( {x:77} , {name:1, x:1} )
db.employees.find(...).count()
db.employees.find(...).limit(n)
db.employees.find(...).skip(n)
db.employees.find(...).sort(...)


           Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Collection Methods (2 of 2)

db.employees.findOne([query])                                          db.employees.save(obj)
db.employees.findAndModify( { update : ... ,                           db.employees.stats()
remove : bool [, query: {}, sort: {}, 'new': false] } )                db.employees.storageSize() - includes free
db.employees.getDB() get DB object associated                          space allocated to this collection
with collection                                                        db.employees.totalIndexSize() - size in
db.employees.getIndexes()                                              bytes of all the indexes
db.employees.group( { key : ..., initial: ...,                         db.employees.totalSize() - storage allocated
reduce : ...[, cond: ...] } )                                          for all data and indexes
db.employees.mapReduce( mapFunction ,                                  db.employees.update(query, object[,
reduceFunction , <optional params> )
                                                                       upsert_bool, multi_bool])
db.employees.remove(query)
                                                                       db.employees.validate( <full> ) – SLOW
db.employees.renameCollection( newName ,
<dropTarget> ) renames the collection.                                 db.employees.getShardVersion() - only for
db.employees.runCommand( name , <options> )                            use with sharding
runs a db command with the given name where                            db.employees.getShardDistribution() -
the first param is the collection name                                 prints statistics about data distribution in
                                                                       the cluster




         Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Basic shell commands (1 of 2)
> help
      db.help()                                             help on db methods
      db.mycoll.help()                                      help on collection methods
      rs.help()                                             help on replica set methods
      help admin                                            administrative help
      help connect                                          connecting to a db help
      help keys                                             key shortcuts
      help misc                                              misc things to know
      help mr                                               mapreduce

     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Basic shell commands (2 of 2)
> help
…
show dbs                          show database names
show collections                  show collections in current database
show users                        show users in current database
show profile                      show most recent system.profile entries time>= 1ms

show logs                         show the accessible logger names
show log [name]                   prints out the last segment of log in memory,
use <db_name>                      set current database

DBQuery.shellBatchSize = x set default number of items to display on shell
exit                quit the mongo shell



       Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Tips for scaling mongo
•   Roger Bodamer
•   http://www.infoq.com/presentations/Scaling-with-Mon
•   Good ideas on EC2 shortcoming
•   RAID configuration (RAID 10 for speed and scaling)
•   Config Servers know where keys are, has key to
    shard mapping, mongos refer to config servers




                                                                                68
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Tips for scaling mongo
• Don’t use sharding unless needed
• 90% of deployments don’t need sharding
  according to Roger Bodamer
      – Are you Twitter, Linkedin, Facebook, Foursquare? No
      – You probably not going to need it
• Replica Sets are more needed
      – Why? HA, read scalability
• Mongos can live on primary box
• ConfigServer can live on a primary box
•   http://www.infoq.com/presentations/Scaling-with-MongoDB




                                                                                 69
       Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Tips for scaling mongo
• Replicas should be on separate boxes




                                                                               70
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Backups
• You can use tools with mongodb
• Or, if your hardware supports shutdowns
• Sync to disk, shutdown cleanly, take a snapshot
•   http://www.infoq.com/presentations/Scaling-with-MongoDB




                                                                                       71
             Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
Coming up
• Basic CRUD and queries: Slide deck showing a simple CRUD listing
  website in Python, Java and PHP using MySQL and MongoDB
   – Justing showing general operations
• Queries: Slide deck improving demo app to do more complex
  queries
• Replica Set: Slide deck setting up simple Replica Set in Amazon
  EC2 with boto scripts
   – Durability modes with examples in Python, PHP and Java
• Sharding: Slide deck setting up Sharding in MongoDB
   – Python, PHP and Java
• Map Reduce: Slide deck using sample app to do map reduce in
  Java, PHP and Python

                                                                                72
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
More to come
• This is an early version of this.... expect updates
• Looking for some feedback
• Want to grow it out, put it on github, etc.
• Do some compare and contrast between MongoDB,
  MySQL, Cassandra, etc.
• Things you won’t get from a vendor
    – complaining
    – criticism
    – shortcomings



                                                                                73
      Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
About Author
• Rick Hightower, founder of ArcMind (RIP) and Mammatus,
  father of five
• Former CTO of LearningPatterns and Trivera Technologies
  (global training and consulting firms)
• Director of Development at 3 different places
• Author of five technical books
• Editor at InfoQ
• 20 years hacking code (C, Python, C++, Java, etc.)
• https://twitter.com/#!/RickHigh




                                                                              74
    Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
About Mammatus, ArcMind
• ArcMind focused on Spring framework, JSF, Java EE,
  Spring MVC, Grails, Groovy, Development, Tiles, etc.
  – Started in 2003 ended in 2009
  – Lot of clients (Boeing, Qualcomm, Bank of America, et.c), lots
    of traveling
• Mammatus Tech (bumpy clouds) focuses on Cloud
  computing, NoSQL, BigData, Map Reduce, Java, PHP,
  Python, EC2, etc.
  – Started in 2009
  – http://cloud.mammatustech.com/
  – http://nosql.mammatustech.com/
  – http://www.mammatustech.com/

                                                                               75
     Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.

Contenu connexe

Tendances

«NoSQL Databases and Polyglot Persistence»
«NoSQL Databases and Polyglot Persistence»«NoSQL Databases and Polyglot Persistence»
«NoSQL Databases and Polyglot Persistence»Olga Lavrentieva
 
Introduction to mongo db
Introduction to mongo dbIntroduction to mongo db
Introduction to mongo dbRohit Bishnoi
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational DatabasesChris Baglieri
 
MongoDB World 2016: Poster Sessions eBook
MongoDB World 2016: Poster Sessions eBookMongoDB World 2016: Poster Sessions eBook
MongoDB World 2016: Poster Sessions eBookMongoDB
 
MongoDB: An Introduction - june-2011
MongoDB:  An Introduction - june-2011MongoDB:  An Introduction - june-2011
MongoDB: An Introduction - june-2011Chris Westin
 
SQL or NoSQL, that is the question!
SQL or NoSQL, that is the question!SQL or NoSQL, that is the question!
SQL or NoSQL, that is the question!Andraz Tori
 
An Introduction to Big Data, NoSQL and MongoDB
An Introduction to Big Data, NoSQL and MongoDBAn Introduction to Big Data, NoSQL and MongoDB
An Introduction to Big Data, NoSQL and MongoDBWilliam LaForest
 
Non relational databases-no sql
Non relational databases-no sqlNon relational databases-no sql
Non relational databases-no sqlRam kumar
 
Intro to NoSQL and MongoDB
Intro to NoSQL and MongoDBIntro to NoSQL and MongoDB
Intro to NoSQL and MongoDBDATAVERSITY
 
introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Databasenehabsairam
 
NoSQL in the context of Social Web
NoSQL in the context of Social WebNoSQL in the context of Social Web
NoSQL in the context of Social WebBogdan Gaza
 
NOSQL Databases types and Uses
NOSQL Databases types and UsesNOSQL Databases types and Uses
NOSQL Databases types and UsesSuvradeep Rudra
 
An Intro to NoSQL Databases
An Intro to NoSQL DatabasesAn Intro to NoSQL Databases
An Intro to NoSQL DatabasesRajith Pemabandu
 

Tendances (20)

«NoSQL Databases and Polyglot Persistence»
«NoSQL Databases and Polyglot Persistence»«NoSQL Databases and Polyglot Persistence»
«NoSQL Databases and Polyglot Persistence»
 
Mongo db intro.pptx
Mongo db intro.pptxMongo db intro.pptx
Mongo db intro.pptx
 
Introduction to mongo db
Introduction to mongo dbIntroduction to mongo db
Introduction to mongo db
 
Introduction to NoSQL
Introduction to NoSQLIntroduction to NoSQL
Introduction to NoSQL
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational Databases
 
Mongo db
Mongo dbMongo db
Mongo db
 
MongoDB World 2016: Poster Sessions eBook
MongoDB World 2016: Poster Sessions eBookMongoDB World 2016: Poster Sessions eBook
MongoDB World 2016: Poster Sessions eBook
 
Data
DataData
Data
 
MongoDB: An Introduction - june-2011
MongoDB:  An Introduction - june-2011MongoDB:  An Introduction - june-2011
MongoDB: An Introduction - june-2011
 
Introduction to mongodb
Introduction to mongodbIntroduction to mongodb
Introduction to mongodb
 
SQL or NoSQL, that is the question!
SQL or NoSQL, that is the question!SQL or NoSQL, that is the question!
SQL or NoSQL, that is the question!
 
An Introduction to Big Data, NoSQL and MongoDB
An Introduction to Big Data, NoSQL and MongoDBAn Introduction to Big Data, NoSQL and MongoDB
An Introduction to Big Data, NoSQL and MongoDB
 
Non relational databases-no sql
Non relational databases-no sqlNon relational databases-no sql
Non relational databases-no sql
 
Intro to NoSQL and MongoDB
Intro to NoSQL and MongoDBIntro to NoSQL and MongoDB
Intro to NoSQL and MongoDB
 
introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Database
 
NoSQL in the context of Social Web
NoSQL in the context of Social WebNoSQL in the context of Social Web
NoSQL in the context of Social Web
 
Hdfs Dhruba
Hdfs DhrubaHdfs Dhruba
Hdfs Dhruba
 
MongoDB
MongoDBMongoDB
MongoDB
 
NOSQL Databases types and Uses
NOSQL Databases types and UsesNOSQL Databases types and Uses
NOSQL Databases types and Uses
 
An Intro to NoSQL Databases
An Intro to NoSQL DatabasesAn Intro to NoSQL Databases
An Intro to NoSQL Databases
 

En vedette

Administration (Eliot Horowitz)
Administration (Eliot Horowitz)Administration (Eliot Horowitz)
Administration (Eliot Horowitz)MongoSF
 
Shankar's mongo db presentation
Shankar's mongo db presentationShankar's mongo db presentation
Shankar's mongo db presentationShankar Kamble
 
Connecting NodeJS & MongoDB
Connecting NodeJS & MongoDBConnecting NodeJS & MongoDB
Connecting NodeJS & MongoDBEnoch Joshua
 
Mongo DB on the JVM - Brendan McAdams
Mongo DB on the JVM - Brendan McAdamsMongo DB on the JVM - Brendan McAdams
Mongo DB on the JVM - Brendan McAdamsJAX London
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsSpringPeople
 
MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for BeginnersEnoch Joshua
 
Intro To MongoDB
Intro To MongoDBIntro To MongoDB
Intro To MongoDBAlex Sharp
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBMike Dirolf
 
An Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBAn Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBLee Theobald
 

En vedette (12)

Administration (Eliot Horowitz)
Administration (Eliot Horowitz)Administration (Eliot Horowitz)
Administration (Eliot Horowitz)
 
Shankar's mongo db presentation
Shankar's mongo db presentationShankar's mongo db presentation
Shankar's mongo db presentation
 
Connecting NodeJS & MongoDB
Connecting NodeJS & MongoDBConnecting NodeJS & MongoDB
Connecting NodeJS & MongoDB
 
Mongo DB on the JVM - Brendan McAdams
Mongo DB on the JVM - Brendan McAdamsMongo DB on the JVM - Brendan McAdams
Mongo DB on the JVM - Brendan McAdams
 
Mongo DB
Mongo DB Mongo DB
Mongo DB
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
 
MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for Beginners
 
Mongo DB
Mongo DBMongo DB
Mongo DB
 
Mongo DB
Mongo DBMongo DB
Mongo DB
 
Intro To MongoDB
Intro To MongoDBIntro To MongoDB
Intro To MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
An Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBAn Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDB
 

Similaire à Mongo DB for Java, Python and PHP Developers

MongoDB quickstart for Java, PHP, and Python developers
MongoDB quickstart for Java, PHP, and Python developersMongoDB quickstart for Java, PHP, and Python developers
MongoDB quickstart for Java, PHP, and Python developersRick Hightower
 
Introducing MongoDB into your Organization
Introducing MongoDB into your OrganizationIntroducing MongoDB into your Organization
Introducing MongoDB into your OrganizationMongoDB
 
11 Live Node.js CMS Frameworks
11 Live Node.js CMS Frameworks11 Live Node.js CMS Frameworks
11 Live Node.js CMS FrameworksiScripts
 
One drupal to rule them all - Drupalcamp Caceres
One drupal to rule them all - Drupalcamp CaceresOne drupal to rule them all - Drupalcamp Caceres
One drupal to rule them all - Drupalcamp Cacereshernanibf
 
WCM-9 WCM Solutions with Drupal and Alfresco
WCM-9 WCM Solutions with Drupal and AlfrescoWCM-9 WCM Solutions with Drupal and Alfresco
WCM-9 WCM Solutions with Drupal and AlfrescoAlfresco Software
 
ROMA User-Customizable NoSQL Database in Ruby
ROMA User-Customizable NoSQL Database in RubyROMA User-Customizable NoSQL Database in Ruby
ROMA User-Customizable NoSQL Database in RubyRakuten Group, Inc.
 
Content Management Systems and Refactoring - Drupal, WordPress and eZ Publish
Content Management Systems and Refactoring - Drupal, WordPress and eZ PublishContent Management Systems and Refactoring - Drupal, WordPress and eZ Publish
Content Management Systems and Refactoring - Drupal, WordPress and eZ PublishJani Tarvainen
 
How to Get Started with Your MongoDB Pilot Project
How to Get Started with Your MongoDB Pilot ProjectHow to Get Started with Your MongoDB Pilot Project
How to Get Started with Your MongoDB Pilot ProjectDATAVERSITY
 
Getting Started with MongoDB at Oracle Open World 2012
Getting Started with MongoDB at Oracle Open World 2012Getting Started with MongoDB at Oracle Open World 2012
Getting Started with MongoDB at Oracle Open World 2012MongoDB
 
Mongo db first steps with csharp
Mongo db first steps with csharpMongo db first steps with csharp
Mongo db first steps with csharpSerdar Buyuktemiz
 
Petabyte scale on commodity infrastructure
Petabyte scale on commodity infrastructurePetabyte scale on commodity infrastructure
Petabyte scale on commodity infrastructureelliando dias
 
Big Data Strategy for the Relational World
Big Data Strategy for the Relational World Big Data Strategy for the Relational World
Big Data Strategy for the Relational World Andrew Brust
 
Introduction to hadoop and hdfs
Introduction to hadoop and hdfsIntroduction to hadoop and hdfs
Introduction to hadoop and hdfsTrendProgContest13
 
Getting Started with Big Data in the Cloud
Getting Started with Big Data in the CloudGetting Started with Big Data in the Cloud
Getting Started with Big Data in the CloudRightScale
 
01/2009 - Portral development with liferay
01/2009 - Portral development with liferay01/2009 - Portral development with liferay
01/2009 - Portral development with liferaydaveayan
 
After the LAMP, it's time to get MEAN
After the LAMP, it's time to get MEANAfter the LAMP, it's time to get MEAN
After the LAMP, it's time to get MEANJeff Fox
 
Introduction to BIg Data and Hadoop
Introduction to BIg Data and HadoopIntroduction to BIg Data and Hadoop
Introduction to BIg Data and HadoopAmir Shaikh
 
Intro to Big Data and NoSQL
Intro to Big Data and NoSQLIntro to Big Data and NoSQL
Intro to Big Data and NoSQLDon Demcsak
 
EDB Postgres with Containers
EDB Postgres with ContainersEDB Postgres with Containers
EDB Postgres with ContainersEDB
 

Similaire à Mongo DB for Java, Python and PHP Developers (20)

MongoDB quickstart for Java, PHP, and Python developers
MongoDB quickstart for Java, PHP, and Python developersMongoDB quickstart for Java, PHP, and Python developers
MongoDB quickstart for Java, PHP, and Python developers
 
Introducing MongoDB into your Organization
Introducing MongoDB into your OrganizationIntroducing MongoDB into your Organization
Introducing MongoDB into your Organization
 
11 Live Node.js CMS Frameworks
11 Live Node.js CMS Frameworks11 Live Node.js CMS Frameworks
11 Live Node.js CMS Frameworks
 
One drupal to rule them all - Drupalcamp Caceres
One drupal to rule them all - Drupalcamp CaceresOne drupal to rule them all - Drupalcamp Caceres
One drupal to rule them all - Drupalcamp Caceres
 
WCM-9 WCM Solutions with Drupal and Alfresco
WCM-9 WCM Solutions with Drupal and AlfrescoWCM-9 WCM Solutions with Drupal and Alfresco
WCM-9 WCM Solutions with Drupal and Alfresco
 
ROMA User-Customizable NoSQL Database in Ruby
ROMA User-Customizable NoSQL Database in RubyROMA User-Customizable NoSQL Database in Ruby
ROMA User-Customizable NoSQL Database in Ruby
 
Content Management Systems and Refactoring - Drupal, WordPress and eZ Publish
Content Management Systems and Refactoring - Drupal, WordPress and eZ PublishContent Management Systems and Refactoring - Drupal, WordPress and eZ Publish
Content Management Systems and Refactoring - Drupal, WordPress and eZ Publish
 
How to Get Started with Your MongoDB Pilot Project
How to Get Started with Your MongoDB Pilot ProjectHow to Get Started with Your MongoDB Pilot Project
How to Get Started with Your MongoDB Pilot Project
 
Be faster then rabbits
Be faster then rabbitsBe faster then rabbits
Be faster then rabbits
 
Getting Started with MongoDB at Oracle Open World 2012
Getting Started with MongoDB at Oracle Open World 2012Getting Started with MongoDB at Oracle Open World 2012
Getting Started with MongoDB at Oracle Open World 2012
 
Mongo db first steps with csharp
Mongo db first steps with csharpMongo db first steps with csharp
Mongo db first steps with csharp
 
Petabyte scale on commodity infrastructure
Petabyte scale on commodity infrastructurePetabyte scale on commodity infrastructure
Petabyte scale on commodity infrastructure
 
Big Data Strategy for the Relational World
Big Data Strategy for the Relational World Big Data Strategy for the Relational World
Big Data Strategy for the Relational World
 
Introduction to hadoop and hdfs
Introduction to hadoop and hdfsIntroduction to hadoop and hdfs
Introduction to hadoop and hdfs
 
Getting Started with Big Data in the Cloud
Getting Started with Big Data in the CloudGetting Started with Big Data in the Cloud
Getting Started with Big Data in the Cloud
 
01/2009 - Portral development with liferay
01/2009 - Portral development with liferay01/2009 - Portral development with liferay
01/2009 - Portral development with liferay
 
After the LAMP, it's time to get MEAN
After the LAMP, it's time to get MEANAfter the LAMP, it's time to get MEAN
After the LAMP, it's time to get MEAN
 
Introduction to BIg Data and Hadoop
Introduction to BIg Data and HadoopIntroduction to BIg Data and Hadoop
Introduction to BIg Data and Hadoop
 
Intro to Big Data and NoSQL
Intro to Big Data and NoSQLIntro to Big Data and NoSQL
Intro to Big Data and NoSQL
 
EDB Postgres with Containers
EDB Postgres with ContainersEDB Postgres with Containers
EDB Postgres with Containers
 

Plus de Rick Hightower

JParse Fast JSON Parser
JParse Fast JSON ParserJParse Fast JSON Parser
JParse Fast JSON ParserRick Hightower
 
Service Mesh Talk for CTO Forum
Service Mesh Talk for CTO ForumService Mesh Talk for CTO Forum
Service Mesh Talk for CTO ForumRick Hightower
 
Service Mesh CTO Forum (Draft 3)
Service Mesh CTO Forum (Draft 3)Service Mesh CTO Forum (Draft 3)
Service Mesh CTO Forum (Draft 3)Rick Hightower
 
Accelerate Delivery: Business Case for Agile DevOps, CI/CD and Microservices
Accelerate Delivery: Business Case for Agile DevOps, CI/CD and MicroservicesAccelerate Delivery: Business Case for Agile DevOps, CI/CD and Microservices
Accelerate Delivery: Business Case for Agile DevOps, CI/CD and MicroservicesRick Hightower
 
Accelerate Delivery: Business case for Agile DevOps, CI/CD and Microservices
Accelerate Delivery: Business case for Agile DevOps, CI/CD and MicroservicesAccelerate Delivery: Business case for Agile DevOps, CI/CD and Microservices
Accelerate Delivery: Business case for Agile DevOps, CI/CD and MicroservicesRick Hightower
 
Accelerate DevOps/Microservices and Kubernetes
Accelerate DevOps/Microservices and KubernetesAccelerate DevOps/Microservices and Kubernetes
Accelerate DevOps/Microservices and KubernetesRick Hightower
 
Accelerate using DevOps and CI/CD.
Accelerate using DevOps and CI/CD.Accelerate using DevOps and CI/CD.
Accelerate using DevOps and CI/CD.Rick Hightower
 
High-speed, Reactive Microservices 2017
High-speed, Reactive Microservices 2017High-speed, Reactive Microservices 2017
High-speed, Reactive Microservices 2017Rick Hightower
 
Reactive Java: Promises and Streams with Reakt (JavaOne Talk 2016)
Reactive Java:  Promises and Streams with Reakt (JavaOne Talk 2016)Reactive Java:  Promises and Streams with Reakt (JavaOne Talk 2016)
Reactive Java: Promises and Streams with Reakt (JavaOne Talk 2016)Rick Hightower
 
Reactive Java: Promises and Streams with Reakt (JavaOne talk 2016)
Reactive Java: Promises and Streams with Reakt  (JavaOne talk 2016)Reactive Java: Promises and Streams with Reakt  (JavaOne talk 2016)
Reactive Java: Promises and Streams with Reakt (JavaOne talk 2016)Rick Hightower
 
High-Speed Reactive Microservices - trials and tribulations
High-Speed Reactive Microservices - trials and tribulationsHigh-Speed Reactive Microservices - trials and tribulations
High-Speed Reactive Microservices - trials and tribulationsRick Hightower
 
High-Speed Reactive Microservices
High-Speed Reactive MicroservicesHigh-Speed Reactive Microservices
High-Speed Reactive MicroservicesRick Hightower
 
Netty Notes Part 3 - Channel Pipeline and EventLoops
Netty Notes Part 3 - Channel Pipeline and EventLoopsNetty Notes Part 3 - Channel Pipeline and EventLoops
Netty Notes Part 3 - Channel Pipeline and EventLoopsRick Hightower
 
Netty Notes Part 2 - Transports and Buffers
Netty Notes Part 2 - Transports and BuffersNetty Notes Part 2 - Transports and Buffers
Netty Notes Part 2 - Transports and BuffersRick Hightower
 
WebSocket MicroService vs. REST Microservice
WebSocket MicroService vs. REST MicroserviceWebSocket MicroService vs. REST Microservice
WebSocket MicroService vs. REST MicroserviceRick Hightower
 
Consul: Microservice Enabling Microservices and Reactive Programming
Consul: Microservice Enabling Microservices and Reactive ProgrammingConsul: Microservice Enabling Microservices and Reactive Programming
Consul: Microservice Enabling Microservices and Reactive ProgrammingRick Hightower
 
The Java Microservice Library
The Java Microservice LibraryThe Java Microservice Library
The Java Microservice LibraryRick Hightower
 

Plus de Rick Hightower (19)

JParse Fast JSON Parser
JParse Fast JSON ParserJParse Fast JSON Parser
JParse Fast JSON Parser
 
Service Mesh Talk for CTO Forum
Service Mesh Talk for CTO ForumService Mesh Talk for CTO Forum
Service Mesh Talk for CTO Forum
 
Service Mesh CTO Forum (Draft 3)
Service Mesh CTO Forum (Draft 3)Service Mesh CTO Forum (Draft 3)
Service Mesh CTO Forum (Draft 3)
 
Accelerate Delivery: Business Case for Agile DevOps, CI/CD and Microservices
Accelerate Delivery: Business Case for Agile DevOps, CI/CD and MicroservicesAccelerate Delivery: Business Case for Agile DevOps, CI/CD and Microservices
Accelerate Delivery: Business Case for Agile DevOps, CI/CD and Microservices
 
Accelerate Delivery: Business case for Agile DevOps, CI/CD and Microservices
Accelerate Delivery: Business case for Agile DevOps, CI/CD and MicroservicesAccelerate Delivery: Business case for Agile DevOps, CI/CD and Microservices
Accelerate Delivery: Business case for Agile DevOps, CI/CD and Microservices
 
Accelerate DevOps/Microservices and Kubernetes
Accelerate DevOps/Microservices and KubernetesAccelerate DevOps/Microservices and Kubernetes
Accelerate DevOps/Microservices and Kubernetes
 
Accelerate using DevOps and CI/CD.
Accelerate using DevOps and CI/CD.Accelerate using DevOps and CI/CD.
Accelerate using DevOps and CI/CD.
 
High-speed, Reactive Microservices 2017
High-speed, Reactive Microservices 2017High-speed, Reactive Microservices 2017
High-speed, Reactive Microservices 2017
 
Reactive Java: Promises and Streams with Reakt (JavaOne Talk 2016)
Reactive Java:  Promises and Streams with Reakt (JavaOne Talk 2016)Reactive Java:  Promises and Streams with Reakt (JavaOne Talk 2016)
Reactive Java: Promises and Streams with Reakt (JavaOne Talk 2016)
 
Reactive Java: Promises and Streams with Reakt (JavaOne talk 2016)
Reactive Java: Promises and Streams with Reakt  (JavaOne talk 2016)Reactive Java: Promises and Streams with Reakt  (JavaOne talk 2016)
Reactive Java: Promises and Streams with Reakt (JavaOne talk 2016)
 
High-Speed Reactive Microservices - trials and tribulations
High-Speed Reactive Microservices - trials and tribulationsHigh-Speed Reactive Microservices - trials and tribulations
High-Speed Reactive Microservices - trials and tribulations
 
High-Speed Reactive Microservices
High-Speed Reactive MicroservicesHigh-Speed Reactive Microservices
High-Speed Reactive Microservices
 
Netty Notes Part 3 - Channel Pipeline and EventLoops
Netty Notes Part 3 - Channel Pipeline and EventLoopsNetty Notes Part 3 - Channel Pipeline and EventLoops
Netty Notes Part 3 - Channel Pipeline and EventLoops
 
Netty Notes Part 2 - Transports and Buffers
Netty Notes Part 2 - Transports and BuffersNetty Notes Part 2 - Transports and Buffers
Netty Notes Part 2 - Transports and Buffers
 
Notes on Netty baics
Notes on Netty baicsNotes on Netty baics
Notes on Netty baics
 
WebSocket MicroService vs. REST Microservice
WebSocket MicroService vs. REST MicroserviceWebSocket MicroService vs. REST Microservice
WebSocket MicroService vs. REST Microservice
 
Consul: Microservice Enabling Microservices and Reactive Programming
Consul: Microservice Enabling Microservices and Reactive ProgrammingConsul: Microservice Enabling Microservices and Reactive Programming
Consul: Microservice Enabling Microservices and Reactive Programming
 
The Java Microservice Library
The Java Microservice LibraryThe Java Microservice Library
The Java Microservice Library
 
Java JSON Benchmark
Java JSON BenchmarkJava JSON Benchmark
Java JSON Benchmark
 

Dernier

Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Dernier (20)

Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

Mongo DB for Java, Python and PHP Developers

  • 1. MongoDB Introduction for Java, Python and PHP Developers Developers for Java, Python and PHP Developers Developers Covers using MongoDB from a Java, PHP and Python developer’s perspective. Uses the Official MongoDB driver for Java, Python and PHP as well as command line tools for MongoDB to teach core concepts. Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 2. Outline • Theory and Architecture of MongoDB (49%) • Setup instructions (9%) • Code examples in JavaScript, PHP, Python and Java (39%) • 3% random © 2012 10gen. MongoDB®, Mongo®, and the leaf logo are registered trademarks of 10gen, Inc. 10gen in no way endorses this slide deck or Mammatus Tehcnology Inc. 2 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 3. MongoDB • NoSQL landscape full of contenders tackling big data problems • MongoDB very capable • Document-oriented schema-less storage solution • JSON-style documents to represent, query and modify data • Supports many clients/languages Python, PHP, Java, Ruby, C++, etc. Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 4. Resources • 10Gen site (http://www.mongodb.org) – Great documentation and presentations • InfoQ articles and presentations • Wikipedia • http://mongly.com/ 4 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 5. Feedback welcome • Send any and all feedback to richardhightower@gmail.com • Criticism welcome – Prefer constructive criticism, but will take any and all – Needed for continuous improvement of this slide deck and my knowledge 5 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 6. Creative Commons • This slide deck and all material therein are covered under creative commons • You can use all material in here as long as you don’t copy it word for word and then use it for commercial reasons • Other material in here from other sources are covered under fair use • http://creativecommons.org/ 6 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 7. Why MongoDB? • MongoDB great active community Supports: • High availability, • Journaling • Replication, • Sharding, MongoDB commercial • Indexing, http://www.10gen.com/what-is-mongodb • Aggregation, • Map/Reduce Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 8. MongoDB is a top job trend Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 9. Leader in the NoSQL space? MongoDB seems to be the clear mind-share leader Cassandra a close second 9 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 10. Why do Developers pick Mongo? • NoSQL, in general, can be more agile than full RDBMS/ SQL – problems with schema migration – a lot of upfront design needed for RDBMS – (or a lot schema migration later) • MongoDB does not require a lot of ramp up time – Easy to get started – Many DevOps things come for free – Easy on ramp for NoSQL – Gateway drug? 10 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 11. Why PHP, Python and Java? Might add Ruby later to the mix or just focus on these 11 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 12. Built for speed | Cache built in • MongoDB was built with speed in mind • Speed shaped architecture of MongoDB • Uses binary protocol instead of HTTP text/ (CouchDB) • Pads disk space around document – faster updates – uses more disk • Uses memory-mapped files as default storage engine, letting OS manage swapping – Linux/Windows/Solaris really good at virtual memory... MongoDB builds on top of this 12 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 13. Negatives of MongoDB • Indexes are not as flexible as Oracle/MySQL/Postgres or other NoSQL solutions – Order of index matters, uses B-Trees, not very many options like more mature solutions • Realtime queries might not be as fast as Oracle/MySQL and other NoSQL solutions • Good enough if queries are simple • Probably hits the sweet spot of 20/80 rule • Not as mature as RDBMS • Does not have full text search engine 13 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 14. Very useful still • Every version seems to add more features • Added journaling so they can have single server durability • Improved Replica’s with Replica Sets • Replica Sets and Autosharding required very little admin once running • What it does, it does well... – Can be combined with Relational database – Can be combined with full text search (Solr) – Can be combined with Hadoop 14 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 15. Who uses MongoDB? Big names, big data • MTV • SAP • Craigslist • Forbes • Disney • National Archives UK • Shutterfly • Intuit • foursqaure • github • bit.ly • LexisNexis • The New York Times • many more • Barclay’s • The Guardian 15 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 16. MongoDB Concept • Oracle: Schema, Tables, Rows, Columns • MySQL: Database, Tables, Rows, Columns • MongoDB: Database, Collections, Document, Fields • MySQL/Oracle: Indexes • MongoDB: Indexes • MySQL/Oracle: Stored Procedures • MongoDB: Stored JavaScript • Oracle/MySQL: Database Schema • MongoDB: Schema free! Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 17. MongoDB Architecture Copyright © Mammatus Technology Inc. | Licensed under Creative Commons. 17
  • 18. Additional Mongo Features • Geo Indexing: How close am I to X? • File Storage – Stores large files and file meta-data • Capped Collection (like Ring Buffer) – Older documents auto-deleted • Aggregation • Auto sharding • Load sharing for reads • High availability • Speed or durability (journaling) 18 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 19. What do you need? • Read scalability and high availability (HA)? HA – Use Replica Sets • Write scalability? – Use Autosharding (also just called sharding) • HA, Read Scalability, and Write Scalability? – Use Autosharding and Replica Sets • You can start basic and add as your growth/needs change – Capacity planning, monitoring, determine needs 19 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 20. Durability • Journaling added in 1.8, and is now default for 64 bit OS for MongoDB 2.0 • Prior to that, you used replication to make sure copy of operation was on replica – MongoDB did not have single server durability, now it does with addition of journaling • General thought was/is durability is overvalued • You can also force an fsync • See links in notes 20 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 21. Replica Sets • Drivers know the primary • If primary down, Drivers Replica 1 Replica 2 know how to get new primary • Data is replicated after writing • Typical to have three in a replica set Replica 0 • You can do more PRIMARY • Load sharing for reads Read Read/Write Client Driver 21 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 22. Replica Sets Usage • Business Continuity • Data Redundancy • High Availability • Load sharing (reads) • “Just works / NoOps (low ops)” 22 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 23. Replica Sets • Non-blocking master/slave replication • Auto failover • Two or more nodes (usually three) • No primary, master is nominated • Share nothing architecture • Brains in the client libraries • Client libraries (Drivers) are Replica Set aware • Client can block until data is replicated on all servers (for important data) 23 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 24. Replica Sets • You only write to the master, it then replicates to slaves • Replication is by default async (non-blocking) • Slave data and write data can be out of sync – There are workarounds – You can force master to sync to master before continuing (blocking, sync) • Sync blocking is slower • Async non-blocking is faster (eventual consistency) 24 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 25. Durability and Replica Sets • Client libraries can do the following: • Wait until write has happened on all replicas • Wait until write is on two servers (primary and one other) • Wait until write has occurred on majority of replicas • Wait until write operation has been written to journal 25 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 26. Replica Sets • Replica Sets and Autosharding go hand in hand for mass scale out • Replica Sets are good for failover and speeding up reads, but... • To speed up writes, you need autosharding 26 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 27. Sharding • Sharding allows MongoDB to scale horizontally • Sharding = partitioning • Auto-shards – load balances – changes for data distribution • Elastic adding of new nodes • Supports automatic failover (along with replica sets) • No single point of failure • 90% of deployments don’t need sharding according to Roger Bodamer 27 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 28. Non-sharded client connection • Client Driver talks directly to mongod process 28 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 29. Autosharded • Three actors now: mongod, mongos, and Client Driver library • Mongod is the process • Mongos is a router, it routes writes to correct mongod instance • Shares writing 29 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 30. Autoshard plus Replica Set • Autosharding increases writes, helps with scale out • Replica Sets are for high availability • There is a whole lesson on sharding. 30 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 31. Sharding Topology • Config Servers (mongod) – contain versioned shard topology – maps which shard has key – used by mongos – like DNS server for shards • Mongos – Shard router clients drivers talk to Mongos instead of mongod directly – Mongos uses Config Servers to find shard where key lives – MongoD are shards that can be replicated 31 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 32. Large deployment 32 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 33. MapReduce • Used for batch processing • Similar to Hadoop, • Massive aggregation possible through divide and conquer • Used instead of Group/By in SQL – Also added simplified framework to MongoDB (aggregation framework) • Map and Reduce functions are written in JavaScript – Executed on server, code next to data it is operating on • Can copy results to results collections 33 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 34. MapReduce Theory Image from http://code.google.com/p/mapreduce-framework/wiki/MapReduce 34 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 35. Incremental MapReduce • Run MapReduce job over collections • Run a second job but only over new documents in collection • Use reduce output to merge new data into existing collection 35 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 36. Aggregation Framework • Added in MongoDB 2.1 • Similar to SQL group by • Before Aggregation framework, you had to use MapReduce for things like SQL group by • Easier to use than MapReduce Table from 10 Gen http://www.mongodb.org/display/DOCS/SQL+to+Aggregation+Framework+M 36 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 37. Aggregation Framework Table from 10 Gen http://www.mongodb.org/display/DOCS/SQL+to+Aggregation+Framework+M 37 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 38. MongoDB versus SQL Compare contrast Copyright © Mammatus Technology Inc. | Licensed under Creative Commons. 38
  • 39. SQL to Mongo From http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart 39 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 40. Mongo versus SQL From http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart 40 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 41. Mongo versus SQL From http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart 41 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 42. Mongo vs. SQL From http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart 42 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 43. Getting Started with Mongo Installing and using Mongo Copyright © Mammatus Technology Inc. | Licensed under Creative Commons. 43
  • 44. Install (1 of 2) • http://www.mongodb.org/downloads • Extract – ~/mongodb-platform-version/ • $ sudo mkdir /etc/mongodb/data • Create file – /etc/mongodb/mongodb.config • $ cat mongodb.config – dbpath=/etc/mongodb/data Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 45. Install (2 of 2) • Link: sudo ln -s ~/mongodb-platform-version/ /usr/local/mongodb • Add to Path: export PATH=$PATH:/usr/local/mongodb/bin • Run the server: mongod --config /etc/mongodb/mongodb.config Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 46. Run the client (type db.version()) $ mongo MongoDB shell version: 2.0.4 connecting to: test … > db.version() 2.0.4 > Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 47. Client: mongo db.help() > db.help() DB methods: db.addUser(username, password[, readOnly=false]) db.auth(username, password) db.cloneDatabase(fromhost) db.commandHelp(name) returns the help for the command db.copyDatabase(fromdb, todb, fromhost) db.createCollection(name, { size : ..., capped : ..., max : ... } ) db.currentOp() displays the current operation in the db db.dropDatabase() db.eval(func, args) run code server-side db.getCollection(cname) same as db['cname'] or db.cname db.getCollectionNames() Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 48. db.help() db.getLastError() - just returns the err msg string db.printCollectionStats() db.getLastErrorObj() - return full status object db.printReplicationInfo() db.getMongo() get the server connection object db.printSlaveReplicationInfo() db.printShardingStatus() db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica db.removeUser(username) pair db.repairDatabase() db.resetError() db.getName() db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 } db.getPrevError() db.getProfilingStatus() - returns if profiling is on db.serverStatus() and slow threshold db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all db.getReplicationInfo() db.shutdownServer() db.getSiblingDB(name) get the db at the same db.stats() server as this one db.version() current version of the server db.isMaster() check replica primary status db.getMongo().setSlaveOk() allow queries on a replication db.killOp(opid) kills the current operation in the db slave server db.listCommands() lists all the db commands db.fsyncLock() flush data to disk and lock server for backups db.logout() db.fsyncUnock() unlocks server following a db.fsyncLock() Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 49. Create Employee Collection > use tutorial; switched to db tutorial > db.getCollectionNames(); [] > db.employees.insert({name:'Rick Hightower', gender:'m', gender:'m', phone:'520-555-1212', age:42}); Mon Apr 23 23:50:24 [FileAllocator] allocating new datafile /etc/mongodb/data/tutorial.ns, filling with zeroes… .. Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 50. Query DB (1 of 2) > db.getCollectionNames(); [ "employees", "system.indexes" ] > db.employees.find() { "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m", "phone" : "520-555-1212", "age" : 42 } Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 51. Query DB (2 of 2) > db.employees.find({name:"Bob"}) > db.employees.find({name:"Rick Hightower"}) { "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m", "phone" : "520-555-1212", "age" : 42 } > db.employees.find({age:{$lt:100}}) { "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m", "phone" : "520-555-1212", "age" : 42 } > db.employees.find({age:{$lt:100}})[0].name Rick Hightower > db.system.indexes.find() { "v" : 1, "key" : { "_id" : 1 }, "ns" : "tutorial.employees", "name" : "_id_" } > db.employees.find({_id : ObjectId("4f964d3000b5874e7a163895")}) { "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m", "phone" : "520-555-1212", "age" : 42 } Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 52. Java: Setup • Download latest mongo driver – https://github.com/mongodb/mongo-java-driver/ downloads $ mkdir tools/mongodb/lib $ cp mongo-2.7.3.jar tools/mongodb/lib Create new Eclipse project in new Workspace Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 53. Java: Setup Eclipse (1 of 2) • Right Click Project, Open Properties, Java Build Path->Libraries->Add Variable- >Configure Variable Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 54. Java: Setup Eclipse (2 of 2) • From Properties->Java Build Path->Libraries • Click Add Variable, Select MONGO, Click Extend…, select jar file you just downloaded Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 55. Java: Using Java Basics (1 of 2) Out: { "_id" : { "$oid" : "4f964d3000b5874e7a163895"} , "name" : "Rick Hightower" , "gender" : "m" , "phone" : "520-555-1212" , "age" : 42.0} { "_id" : { "$oid" : "4f984cce72320612f8f432bb"} , "name" : "Diana Hightower" , "gender" : "f" , "phone" : "520-555-1212" , "age" : 30} Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 56. Using Java Basics (2 of 2) Output: Rick? { "_id" : { "$oid" : "4f964d3000b5874e7a163895"} , "name" : "Rick Hightower" , "gender" : "m" , "phone" : "520-555-1212" , "age" : 42.0} Diana? { "_id" : { "$oid" : "4f984cae72329d0ecd8716c8"} , "name" : "Diana Hightower" , "gender" : "m" , "phone" : "520-555-1212" , "age" : 30} Diana by object id? { "_id" : { "$oid" : "4f984cce72320612f8f432bb"} , "name" : "Diana Hightower" , "gender" : "f" , "phone" : "520-555-1212" , "age" : 30} Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 57. All of the above but in Python • Install mongodb lib for Python MAC OSX $ sudo env ARCHFLAGS='-arch i386 -arch x86_64' python -m easy_install pymongo Linux $ easy_install pymongo or $ pip install pymongo Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 58. Basic Python Operations (1 of 2) Output: {u'gender': u'm', u'age': 42.0, u'_id': ObjectId('4f964d3000b5874e7a163895'), u'name': u'Rick Hightower', u'phone': u'520-555-1212'} {u'gender': u'm', u'age': 30, u'_id': ObjectId('4f984cae72329d0ecd8716c8'), u'name': u'Diana Hightower', u'phone': u'520-555-1212'} {u'gender': u'm', u'age': 8, u'_id': ObjectId('4f9e111980cbd54eea000000'), u'name': u'Lucas Hightower', u'phone': u'520-555-1212'} Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 59. Basic Python Operations (2 of 2) Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 60. All of the above but in PHP • Install on PHP $ sudo pecl install mongo • Add to php.ini: extension=mongo.so • Restart apache $ apachectl stop $ apachectl start Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 61. Mongo PHP Basics (1 of 2) Output: array ( '_id' => MongoId::__set_state(array( '$id' => '4f964d3000b5874e7a163895', )), 'name' => 'Rick Hightower', 'gender' => 'm', 'phone' => '520-555-1212', 'age' => 42, ) array ( '_id' => MongoId::__set_state(array( '$id' => '4f984cae72329d0ecd8716c8', )), 'name' => 'Diana Hightower', 'gender' => ‘f', 'phone' => '520-555-1212', 'age' => 30, ) array ( '_id' => MongoId::__set_state(array( '$id' => '4f9e170580cbd54f27000000', )), 'gender' => 'm', 'age' => 8, 'name' => 'Lucas Hightower', 'phone' => '520-555-1212', ) Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 62. Mongo PHP (2 of 2) Output Rick? array ( '_id' => MongoId..., 'name' => 'Rick Hightower', 'gender' => 'm', 'phone' => '520-555-1212', 'age' => 42, ) Diana? array ( '_id' => MongoId::..., 'name' => 'Diana Hightower', 'gender' => ‘f', 'phone' => '520-555-1212', 'age' => 30, ) Diana by id? array ( '_id' => MongoId::..., 'name' => 'Diana Hightower', 'gender' => 'f', 'phone' => '520-555-1212', 'age' => 30, ) Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 63. Shell commands in action > show dbs local (empty) tutorial 0.203125GB > show collections employees system.indexes > show users > show profile db.system.profile is empty Use db.setProfilingLevel(2) will enable profiling .. > show logs global > show log global Mon Apr 23 23:33:14 [initandlisten] MongoDB starting : pid=11773 port=27017 dbpath=/etc/mongodb/data 64-bit… … Mon Apr 23 23:33:14 [initandlisten] options: { config: "/etc/mongodb/mongodb.config", dbpath: "/etc/mongodb/data" } Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 64. Collection Methods (1 of 2) > db.employees.help() DBCollection help db.employees.find().help() - show DBCursor help db.employees.count() db.employees.dataSize() db.employees.distinct( key ) - eg. db.employees.distinct( 'x' ) db.employees.drop() drop the collection db.employees.dropIndex(name) db.employees.dropIndexes() db.employees.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups db.employees.reIndex() db.employees.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return. e.g. db.employees.find( {x:77} , {name:1, x:1} ) db.employees.find(...).count() db.employees.find(...).limit(n) db.employees.find(...).skip(n) db.employees.find(...).sort(...) Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 65. Collection Methods (2 of 2) db.employees.findOne([query]) db.employees.save(obj) db.employees.findAndModify( { update : ... , db.employees.stats() remove : bool [, query: {}, sort: {}, 'new': false] } ) db.employees.storageSize() - includes free db.employees.getDB() get DB object associated space allocated to this collection with collection db.employees.totalIndexSize() - size in db.employees.getIndexes() bytes of all the indexes db.employees.group( { key : ..., initial: ..., db.employees.totalSize() - storage allocated reduce : ...[, cond: ...] } ) for all data and indexes db.employees.mapReduce( mapFunction , db.employees.update(query, object[, reduceFunction , <optional params> ) upsert_bool, multi_bool]) db.employees.remove(query) db.employees.validate( <full> ) – SLOW db.employees.renameCollection( newName , <dropTarget> ) renames the collection. db.employees.getShardVersion() - only for db.employees.runCommand( name , <options> ) use with sharding runs a db command with the given name where db.employees.getShardDistribution() - the first param is the collection name prints statistics about data distribution in the cluster Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 66. Basic shell commands (1 of 2) > help db.help() help on db methods db.mycoll.help() help on collection methods rs.help() help on replica set methods help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 67. Basic shell commands (2 of 2) > help … show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries time>= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, use <db_name> set current database DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 68. Tips for scaling mongo • Roger Bodamer • http://www.infoq.com/presentations/Scaling-with-Mon • Good ideas on EC2 shortcoming • RAID configuration (RAID 10 for speed and scaling) • Config Servers know where keys are, has key to shard mapping, mongos refer to config servers 68 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 69. Tips for scaling mongo • Don’t use sharding unless needed • 90% of deployments don’t need sharding according to Roger Bodamer – Are you Twitter, Linkedin, Facebook, Foursquare? No – You probably not going to need it • Replica Sets are more needed – Why? HA, read scalability • Mongos can live on primary box • ConfigServer can live on a primary box • http://www.infoq.com/presentations/Scaling-with-MongoDB 69 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 70. Tips for scaling mongo • Replicas should be on separate boxes 70 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 71. Backups • You can use tools with mongodb • Or, if your hardware supports shutdowns • Sync to disk, shutdown cleanly, take a snapshot • http://www.infoq.com/presentations/Scaling-with-MongoDB 71 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 72. Coming up • Basic CRUD and queries: Slide deck showing a simple CRUD listing website in Python, Java and PHP using MySQL and MongoDB – Justing showing general operations • Queries: Slide deck improving demo app to do more complex queries • Replica Set: Slide deck setting up simple Replica Set in Amazon EC2 with boto scripts – Durability modes with examples in Python, PHP and Java • Sharding: Slide deck setting up Sharding in MongoDB – Python, PHP and Java • Map Reduce: Slide deck using sample app to do map reduce in Java, PHP and Python 72 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 73. More to come • This is an early version of this.... expect updates • Looking for some feedback • Want to grow it out, put it on github, etc. • Do some compare and contrast between MongoDB, MySQL, Cassandra, etc. • Things you won’t get from a vendor – complaining – criticism – shortcomings 73 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 74. About Author • Rick Hightower, founder of ArcMind (RIP) and Mammatus, father of five • Former CTO of LearningPatterns and Trivera Technologies (global training and consulting firms) • Director of Development at 3 different places • Author of five technical books • Editor at InfoQ • 20 years hacking code (C, Python, C++, Java, etc.) • https://twitter.com/#!/RickHigh 74 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.
  • 75. About Mammatus, ArcMind • ArcMind focused on Spring framework, JSF, Java EE, Spring MVC, Grails, Groovy, Development, Tiles, etc. – Started in 2003 ended in 2009 – Lot of clients (Boeing, Qualcomm, Bank of America, et.c), lots of traveling • Mammatus Tech (bumpy clouds) focuses on Cloud computing, NoSQL, BigData, Map Reduce, Java, PHP, Python, EC2, etc. – Started in 2009 – http://cloud.mammatustech.com/ – http://nosql.mammatustech.com/ – http://www.mammatustech.com/ 75 Copyright © Mammatus Technology Inc. | Licensed under Creative Commons.

Notes de l'éditeur

  1. http://openmymind.net/2011/3/28/The-Little-MongoDB-Book/ http://www.10gen.com/what-is-mongodb
  2. http://www.infoq.com/articles/mongodb-java-orm-bcd
  3. http://en.wikipedia.org/wiki/MongoDB#Use_cases_.26_production_deployments http://www.mongodb.org/display/DOCS/Production+Deployments
  4. http://www.thebuzzmedia.com/mongodb-single-server-data-durability-guide/ http://blog.mongodb.org/post/381927266/what-about-durability
  5. http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial http://www.mongodb.org/display/DOCS/Replica+Sets http:// www.mongodb.org/display/DOCS/Replication http:// www.mongodb.org/display/DOCS/Verifying+Propagatio n+of+Writes+with+getLastError
  6. http://www.mongodb.org/display/DOCS/Verifying+Propagation+of+Writes+with+getLastError
  7. http://www.mongodb.org/display/DOCS/Sharding http://www.mongodb.org/display/DOCS/Sharding +Introduction
  8. http://code.google.com/p/mapreduce-framework/wiki/MapReduce
  9. http://www.mongodb.org/display/DOCS/Aggregation+Framework http://www.mongodb.org/display/DOCS/SQL+to+Aggregation+Fr amework+Mapping+Chart