Python andMongoDBThe perfect Match<br />Andreas Jung, www.zopyx.com<br />
Trainer Andreas Jung<br />Python developersince 1993<br />Python, Zope & Plonedevelopment<br />Specialized in Electronic P...
Agenda (45 minutes per slot)<br />IntroductiontoMongoDB<br />UsingMongoDB<br />UsingMongoDBfrom Python withPyMongo<br />(P...
Things not coveredin thistutorial<br />Geospatialindexing<br />Map-reduce<br />Details on scaling (Sharding, Replicasets)<...
Part I/4<br />IntroductiontoMongoDB:<br />ConceptsofMongoDB<br />Architecture<br />HowMongoDBcompareswith relational datab...
MongoDBis...<br />an open-source,<br />high-performance,<br />schema-less,<br />document-oriented<br />database<br />
Let‘sagree on thefollowingorleave...<br />MongoDBis cool<br />MongoDBis not the multi-purpose-one-size-fits-all database<b...
And.....<br />Don‘taskmeabouthowto do JOINs in MongoDB<br />
Oh, SQL – let‘shavesomefunfirst<br />A SQL statementwalksinto a bar andseestwotables. He walksandsays: „Hello, may I joiny...
The historyofMongoDB<br />10gen founded in 2007<br />Startedascloud-alternative GAE<br />App-engineed<br />Database p<br /...
Major MongoDBdeployments<br />
MongoDBis schema-less<br />JSON-style datastore<br />Eachdocumentcanhaveitsownschema<br />Documentsinside a collectionusua...
Terminology: RDBMS vs. MongoDB<br />
CharacteristicsofMongoDB (I)<br />High-performance<br />Rich querylanguage (similarto SQL)<br />Map-Reduce (ifyoureallynee...
CharacteristicsofMongoDB (II)<br />Notransactionsupport, onlyatomicoperations<br />Default: „fire-and-forget“ modefor high...
Typicalperformancecharacteristics<br />Decentcommoditiyhardware:<br />Upto 100.000 read/writes per second (fire-and-forget...
Functionality vs. Scability<br />
MongoDB: Pros & Cons<br />
Durability<br />Default: fire-and-forget (usesafe-mode)<br />Changesarekept in RAM (!)<br />Fsynctodiskevery 60 seconds (d...
Differences from Typical RDBMS<br />Memory mapped data<br />All data in memory (if it fits), synced to disk periodically<b...
Replica Sets<br />Cluster of N servers<br />Only one node is ‘primary’ at a time<br />This is equivalent to master<br />Th...
Replica Sets - Writes<br />A write is only ‘committed’ once it has been replicated to a majority of nodes in the set<br />...
Replica Sets - Nodes<br />Nodes monitor each other’s heartbeats<br />If primary can’t see a majority of nodes, it relinqui...
Replica Sets - Nodes<br />Member 1<br />Member 2<br />Member 3<br />
Replica Sets - Nodes<br />{a:1}<br />Member 1<br />SECONDARY<br />{a:1}<br />{b:2}<br />Member 2<br />SECONDARY<br />{a:1}...
Replica Sets - Nodes<br />{a:1}<br />Member 1<br />SECONDARY<br />{a:1}<br />{b:2}<br />Member 2<br />PRIMARY<br />{a:1}<b...
Replica Sets - Nodes<br />{a:1}<br />{b:2}<br />Member 1<br />SECONDARY<br />{a:1}<br />{b:2}<br />Member 2<br />PRIMARY<b...
Replica Sets - Nodes<br />{a:1}<br />{b:2}<br />Member 1<br />SECONDARY<br />{a:1}<br />{b:2}<br />Member 2<br />PRIMARY<b...
Replica Sets – Node Types<br />Standard – can be primary or secondary<br />Passive – will be secondary but never primary<b...
SlaveOk<br />db.getMongo().setSlaveOk();<br />Syntax varies by driver<br />Writes to master, reads to slave<br />Slave wil...
Sharding Architecture<br />
Shard<br />A replica set<br />Manages a well defined range of shard keys<br />
Shard<br />Distribute data across machines<br />Reduce data per machine<br />Better able to fit in RAM<br />Distribute wri...
Shard Key<br />{ user_id: 1 }<br />{ lastname: 1, firstname: 1 }<br />{ tag: 1, timestamp: -1 }<br />{ _id: 1 }<br />This ...
Mongos<br />Routes data to/from shards<br />db.users.find( { user_id: 5000 } )<br />db.users.find( { user_id: { $gt: 4000,...
Differences from Typical RDBMS<br />Memory mapped data<br />All data in memory (if it fits), synced to disk periodically<b...
Part 2/4<br />UsingMongoDB<br />StartingMongoDB<br />Usingtheinteractive Mongo console<br />Basic databaseoperations<br />
Gettingstarted...theserver<br />wget http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.8.1.tgz<br />tarxfzmongodb-osx-x86...
Gettingstarted...theconsole<br />bin/mongod<br />mongodlistenstoport 27017 bydefault<br />HTTP interface on port 28017<br ...
Datatypes...<br />Remember: MongoDBis schema-less<br />MongoDBsupports JSON + some extra types<br />
A smalladdressdatabase<br />Person:<br />firstname<br />lastname<br />birthday<br />city<br />phone<br />
Inserting<br />> db.foo.insert(document)<br />> db.foo.insert({‚firstname‘ : ‚Ben‘})<br />everydocumenthas an „_id“ field<...
Querying<br />> db.foo.find(query_expression)<br />> db.foo.find({‚firstname‘ : ‚Ben‘})<br />Queriesareexpressedusing JSON...
Queryingwithsorting<br />> db.foo.find({}).sort({‚firstname‘ :1, ‚age‘: -1})<br />sortingspecification in JSON notation<br...
Advancedquerying<br />$all<br />$exists<br />$mod<br />$ne<br />$in<br />$nin<br />$nor<br />$or<br />$size<br />$type<br ...
Updating<br />> db.foo.update(criteria, obj, multi, upsert)<br />update() updatesonlyonedocumentbydefault (specifymulti=1)...
Updating – modifieroperations<br />$inc<br />$set<br />$unset<br />$push<br />$pushAll<br />$addToSet<br />$pop<br />$pull...
Updating<br />> db.foo.update(criteria, obj, multi, upsert)<br />update() updatesonlyonedocumentbydefault (specifymulti=1)...
Removing<br />db.foo.remove({})                             // remove all<br />db.foo.remove({‚firstname‘ : ‚Ben‘})  // re...
Indexes<br />workingsimilartoindex in relational databases<br />db.foo.ensureIndex({age: 1}, {background: true})<br />oneq...
Embedded documents<br />MongoDBdocs = JSON/BSON-like<br />Embeededdocumentssimilarnesteddicts in Python<br />db.foo.insert...
Arrays (1/2)<br />Like (nested) lists in Python<br />db.foo.insert({colors: [‚green‘, ‚blue‘, ‚red‘]})<br />db.foo.find({c...
Arrays (2/2) – matchingarrays<br />db.bar.insert({users: [                         {name: ‚Hans‘, age:42},                ...
Part 3/4<br />UsingMongoDBfrom Python <br />PyMongo<br />InstallingPyMongo<br />UsingPyMongo<br />
InstallingandtestingPyMongo<br />Installpymongo<br />virtualenv –no-site-packagespymongo<br />bin/easy_installpymongo<br /...
Part 4/4<br />? High-level PyMongoframeworks<br />Mongokit<br />Mongoengine<br />MongoAlchemy<br />? Migration SQL toMongo...
Mongokit (1/3)<br />schemavalidation (wich usesimple pythontype forthedeclaration)<br />dotednotation<br />nestedandcomple...
Mongokit (2/3)<br />classBlogPost(Document):<br />structure = {<br />        'title': unicode,<br />        'body': unicod...
Mongokit (3/3)<br />Speed andperformanceimpact<br />Mongokitisalwaysbehindthemostcurrentpymongoversions<br />one-man devel...
Mongoengine (1/2)<br />MongoEngineis a Document-Object Mapper (think ORM, but fordocumentdatabases) forworkingwithMongoDBf...
Mongokit (2/2)<br />classBlogPost(Document):<br />    title = StringField(required=True)<br />body = StringField()<br />au...
MongoAlchemy (1/2)<br />MongoAlchemyis a layer on top ofthe Python MongoDBdriverwhichadds client-sideschemadefinitions, an...
MongoAlchemy(2/2)<br />frommongoalchemy.documentimportDocument, DocumentField<br />frommongoalchemy.fieldsimport *<br />fr...
From SQL toMongoDB<br />
The CAP theorem<br />Consistency<br />Availablity<br />TolerancetonetworkPartitions<br />Pick two...<br />
ACID versus Base<br />Atomicity<br />Consistency<br />Isolation<br />Durability<br />BasicallyAvailable<br />Soft state<br...
Upcoming SlideShare
Loading in...5
×

Python mongo db-training-europython-2011

8,813

Published on

Slides of my Python/MongoDB training given at EuroPython 2011 in Florence.

Published in: Technology

Transcript of "Python mongo db-training-europython-2011"

  1. 1. Python andMongoDBThe perfect Match<br />Andreas Jung, www.zopyx.com<br />
  2. 2. Trainer Andreas Jung<br />Python developersince 1993<br />Python, Zope & Plonedevelopment<br />Specialized in Electronic Publishing<br />DirectoroftheZopeFoundation<br />Authorofdozensadd-onsfor Python, ZopeandPlone<br />Co-Founderofthe German Zope User Group (DZUG)<br />Member ofthePloneFoundation<br />usingMongoDBsince 2009<br />
  3. 3. Agenda (45 minutes per slot)<br />IntroductiontoMongoDB<br />UsingMongoDB<br />UsingMongoDBfrom Python withPyMongo<br />(PyMongoextensions/ORM-ishlayersor Q/A)<br />
  4. 4. Things not coveredin thistutorial<br />Geospatialindexing<br />Map-reduce<br />Details on scaling (Sharding, Replicasets)<br />
  5. 5. Part I/4<br />IntroductiontoMongoDB:<br />ConceptsofMongoDB<br />Architecture<br />HowMongoDBcompareswith relational databases<br />Scalability<br />
  6. 6. MongoDBis...<br />an open-source,<br />high-performance,<br />schema-less,<br />document-oriented<br />database<br />
  7. 7. Let‘sagree on thefollowingorleave...<br />MongoDBis cool<br />MongoDBis not the multi-purpose-one-size-fits-all database<br />MongoDBisanotheradditionaltoolforthesoftwaredeveloper<br />MongoDBis not a replacementfor RDBMS in general<br />Usetherighttoolforeachtask<br />
  8. 8. And.....<br />Don‘taskmeabouthowto do JOINs in MongoDB<br />
  9. 9. Oh, SQL – let‘shavesomefunfirst<br />A SQL statementwalksinto a bar andseestwotables. He walksandsays: „Hello, may I joinyou“<br />A SQL injectionwalksinto a bar andstartstoquotesomething but suddenlystops, drops a tableanddashes out.<br />
  10. 10. The historyofMongoDB<br />10gen founded in 2007<br />Startedascloud-alternative GAE<br />App-engineed<br />Database p<br />Javascriptasimplementationlanguage<br />2008: focusing on thedatabasepart: MongoDB<br />2009: firstMongoDBrelease<br />2011: MongoDB 1.8:<br />Major deployments<br />A fast growingcommunity<br />Fast adoptationfor large projects<br />10gen growing<br />
  11. 11. Major MongoDBdeployments<br />
  12. 12. MongoDBis schema-less<br />JSON-style datastore<br />Eachdocumentcanhaveitsownschema<br />Documentsinside a collectionusuallyshare a commonschemabyconvention<br />{‚name‘ : ‚kate‘, ‚age‘:12, }<br />{‚name‘ : ‚adam‘, ‚height‘ : 180}<br />{‚q‘: 1234, ‚x‘ = [‚foo‘, ‚bar‘]}<br />
  13. 13. Terminology: RDBMS vs. MongoDB<br />
  14. 14. CharacteristicsofMongoDB (I)<br />High-performance<br />Rich querylanguage (similarto SQL)<br />Map-Reduce (ifyoureallyneedit)<br />Secondaryindexes<br />Geospatialindexing<br />Replication<br />Auto-sharing (partitioningofdata)<br />Manyplatforms, driversformanylanguages<br />
  15. 15. CharacteristicsofMongoDB (II)<br />Notransactionsupport, onlyatomicoperations<br />Default: „fire-and-forget“ modefor high throughput<br />„Safe-Mode“: waitforserverconfirmation, checkingforerrors<br />
  16. 16. Typicalperformancecharacteristics<br />Decentcommoditiyhardware:<br />Upto 100.000 read/writes per second (fire-and-forget)<br />Upto 50.000 reads/writes per second (safemode)<br />Yourmileagemayvary– depending on<br />RAM<br />Speed IO system<br />CPU<br />Client-sidedriver& application<br />
  17. 17. Functionality vs. Scability<br />
  18. 18. MongoDB: Pros & Cons<br />
  19. 19. Durability<br />Default: fire-and-forget (usesafe-mode)<br />Changesarekept in RAM (!)<br />Fsynctodiskevery 60 seconds (default)<br />Deploymentoptions:<br />Standaloneinstallation: usejournaling (V 1.8+)<br />Replicated: usereplicasets(s)<br />
  20. 20. Differences from Typical RDBMS<br />Memory mapped data<br />All data in memory (if it fits), synced to disk periodically<br />No joins<br />Reads have greater data locality<br />No joins between servers<br />No transactions<br />Improves performance of various operations<br />No transactions between servers<br />
  21. 21. Replica Sets<br />Cluster of N servers<br />Only one node is ‘primary’ at a time<br />This is equivalent to master<br />The node where writes go<br />Primary is elected by concensus<br />Automatic failover<br />Automatic recovery of failed nodes<br />
  22. 22. Replica Sets - Writes<br />A write is only ‘committed’ once it has been replicated to a majority of nodes in the set<br />Before this happens, reads to the set may or may not see the write<br />On failover, data which is not ‘committed’ may be dropped (but not necessarily)<br />If dropped, it will be rolled back from all servers which wrote it<br />For improved durability, use getLastError/w<br />Other criteria – block writes when nodes go down or slaves get too far behind<br />Or, to reduce latency, reduce getLastError/w<br />
  23. 23. Replica Sets - Nodes<br />Nodes monitor each other’s heartbeats<br />If primary can’t see a majority of nodes, it relinquishes primary status<br />If a majority of nodes notice there is no primary, they elect a primary using criteria<br />Node priority<br />Node data’s freshness<br />
  24. 24. Replica Sets - Nodes<br />Member 1<br />Member 2<br />Member 3<br />
  25. 25. Replica Sets - Nodes<br />{a:1}<br />Member 1<br />SECONDARY<br />{a:1}<br />{b:2}<br />Member 2<br />SECONDARY<br />{a:1}<br />{b:2}<br />{c:3}<br />Member 3<br />PRIMARY<br />
  26. 26. Replica Sets - Nodes<br />{a:1}<br />Member 1<br />SECONDARY<br />{a:1}<br />{b:2}<br />Member 2<br />PRIMARY<br />{a:1}<br />{b:2}<br />{c:3}<br />Member 3<br />DOWN<br />
  27. 27. Replica Sets - Nodes<br />{a:1}<br />{b:2}<br />Member 1<br />SECONDARY<br />{a:1}<br />{b:2}<br />Member 2<br />PRIMARY<br />{a:1}<br />{b:2}<br />{c:3}<br />Member 3<br />RECOVERING<br />
  28. 28. Replica Sets - Nodes<br />{a:1}<br />{b:2}<br />Member 1<br />SECONDARY<br />{a:1}<br />{b:2}<br />Member 2<br />PRIMARY<br />{a:1}<br />{b:2}<br />Member 3<br />SECONDARY<br />
  29. 29. Replica Sets – Node Types<br />Standard – can be primary or secondary<br />Passive – will be secondary but never primary<br />Arbiter – will vote on primary, but won’t replicate data<br />
  30. 30. SlaveOk<br />db.getMongo().setSlaveOk();<br />Syntax varies by driver<br />Writes to master, reads to slave<br />Slave will be picked arbitrarily<br />
  31. 31. Sharding Architecture<br />
  32. 32. Shard<br />A replica set<br />Manages a well defined range of shard keys<br />
  33. 33. Shard<br />Distribute data across machines<br />Reduce data per machine<br />Better able to fit in RAM<br />Distribute write load across shards<br />Distribute read load across shards, and across nodes within shards<br />
  34. 34. Shard Key<br />{ user_id: 1 }<br />{ lastname: 1, firstname: 1 }<br />{ tag: 1, timestamp: -1 }<br />{ _id: 1 }<br />This is the default<br />
  35. 35. Mongos<br />Routes data to/from shards<br />db.users.find( { user_id: 5000 } )<br />db.users.find( { user_id: { $gt: 4000, $lt: 6000 } } )<br />db.users.find( { hometown: ‘Seattle’ } )<br />db.users.find( { hometown: ‘Seattle’ } ).sort( { user_id: 1 } )<br />
  36. 36. Differences from Typical RDBMS<br />Memory mapped data<br />All data in memory (if it fits), synced to disk periodically<br />No joins<br />Reads have greater data locality<br />No joins between servers<br />No transactions<br />Improves performance of various operations<br />No transactions between servers<br />A weak authentication and authorization model<br />
  37. 37. Part 2/4<br />UsingMongoDB<br />StartingMongoDB<br />Usingtheinteractive Mongo console<br />Basic databaseoperations<br />
  38. 38. Gettingstarted...theserver<br />wget http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.8.1.tgz<br />tarxfzmongodb-osx-x86_64-1.8.1.tgz<br />cd mongodb-osx-x86_64-1.8.1<br />mkdir /tmp/db<br />bin/mongod –dbpath /tmp/db<br />Pick upyour OS-specificpackagefromhttp://www.mongodb.org/downloads<br />Take careof 32 bitbs. 64 bitversion<br />
  39. 39. Gettingstarted...theconsole<br />bin/mongod<br />mongodlistenstoport 27017 bydefault<br />HTTP interface on port 28017<br />> help<br />> db.help()<br />> db.some_collection.help()<br />
  40. 40. Datatypes...<br />Remember: MongoDBis schema-less<br />MongoDBsupports JSON + some extra types<br />
  41. 41. A smalladdressdatabase<br />Person:<br />firstname<br />lastname<br />birthday<br />city<br />phone<br />
  42. 42. Inserting<br />> db.foo.insert(document)<br />> db.foo.insert({‚firstname‘ : ‚Ben‘})<br />everydocumenthas an „_id“ field<br />„_id“ insertedautomaticallyif not present<br />
  43. 43. Querying<br />> db.foo.find(query_expression)<br />> db.foo.find({‚firstname‘ : ‚Ben‘})<br />Queriesareexpressedusing JSON notationwith JSON/BSON objects<br />queryexpressionscombinedusing AND (bydefault)<br />http://www.mongodb.org/display/DOCS/Querying<br />
  44. 44. Queryingwithsorting<br />> db.foo.find({}).sort({‚firstname‘ :1, ‚age‘: -1})<br />sortingspecification in JSON notation<br />1 = ascending, -1 = descending<br />
  45. 45. Advancedquerying<br />$all<br />$exists<br />$mod<br />$ne<br />$in<br />$nin<br />$nor<br />$or<br />$size<br />$type<br />http://www.mongodb.org/display/DOCS/Advanced+Queries<br />
  46. 46. Updating<br />> db.foo.update(criteria, obj, multi, upsert)<br />update() updatesonlyonedocumentbydefault (specifymulti=1)<br />upsert=1: ifdocumentdoes not exist, insertit<br />
  47. 47. Updating – modifieroperations<br />$inc<br />$set<br />$unset<br />$push<br />$pushAll<br />$addToSet<br />$pop<br />$pull<br />$pullAll<br />$rename<br />$bit<br />http://www.mongodb.org/display/DOCS/Updating<br />
  48. 48. Updating<br />> db.foo.update(criteria, obj, multi, upsert)<br />update() updatesonlyonedocumentbydefault (specifymulti=1)<br />upsert=1: ifdocumentdoes not exist, insertit<br />
  49. 49. Removing<br />db.foo.remove({}) // remove all<br />db.foo.remove({‚firstname‘ : ‚Ben‘}) // removebykey<br />db.foo.remove({‚_id‘ : ObjectId(...)}) // removeby _id<br />Atomicremoval(locksthedatabase)<br />db.foo.remove( { age: 42, $atomic : true } )<br />http://www.mongodb.org/display/DOCS/Removing<br />
  50. 50. Indexes<br />workingsimilartoindex in relational databases<br />db.foo.ensureIndex({age: 1}, {background: true})<br />onequery– oneindex<br />CompoundIndexes<br />db.foo.ensureIndex({age: 1, firstname:-1}<br />Orderingofqueryparametersmatters<br />http://www.mongodb.org/display/DOCS/Indexes<br />
  51. 51. Embedded documents<br />MongoDBdocs = JSON/BSON-like<br />Embeededdocumentssimilarnesteddicts in Python<br />db.foo.insert({firstname:‘Ben‘, data:{a:1, b:2, c:3})<br />db.foo.find({‚data.a‘:1})<br />Dottednotationforreachingintoembeddedocuments<br />Usequotesarounddottednames<br />Indexes work on embeddesdocuments<br />
  52. 52. Arrays (1/2)<br />Like (nested) lists in Python<br />db.foo.insert({colors: [‚green‘, ‚blue‘, ‚red‘]})<br />db.foo.find({colors: ‚red‘})<br />Useindexes<br />
  53. 53. Arrays (2/2) – matchingarrays<br />db.bar.insert({users: [ {name: ‚Hans‘, age:42}, {name:‘Jim‘, age: 30 }, ]})<br />db.bar.find({users : {‚$elemMatch‘: {age : {$gt:42}}}})<br />
  54. 54. Part 3/4<br />UsingMongoDBfrom Python <br />PyMongo<br />InstallingPyMongo<br />UsingPyMongo<br />
  55. 55. InstallingandtestingPyMongo<br />Installpymongo<br />virtualenv –no-site-packagespymongo<br />bin/easy_installpymongo<br />Start MongoDB<br />mkdir /tmp/db<br />mongod –dbpath /tmp/db<br />Start Python<br />bin/python<br />> importpymongo<br />> conn = pymongo.Connection(‚localhost‘, 27127)<br />
  56. 56. Part 4/4<br />? High-level PyMongoframeworks<br />Mongokit<br />Mongoengine<br />MongoAlchemy<br />? Migration SQL toMongoDB<br />? Q/A<br />? Lookingat a real worldprojectdonewithPyramidandMongoDB?<br />? Let‘stalkabout..<br />
  57. 57. Mongokit (1/3)<br />schemavalidation (wich usesimple pythontype forthedeclaration)<br />dotednotation<br />nestedandcomplexschemadeclaration<br />untypedfieldsupport<br />requiredfieldsvalidation<br />defaultvalues<br />customvalidators<br />crossdatabasedocumentreference<br />randomquerysupport (whichreturns a randomdocumentfromthedatabase)<br />inheritanceandpolymorphismesupport<br />versionizeddocumentsupport (in betastage)<br />partial authsupport (itbrings a simple User model)<br />operatorforvalidation (currently : OR, NOT and IS)<br />simple web frameworkintegration<br />import/exporttojson<br />i18n support<br />GridFSsupport<br />documentmigrationsupport<br />
  58. 58. Mongokit (2/3)<br />classBlogPost(Document):<br />structure = {<br /> 'title': unicode,<br /> 'body': unicode,<br /> 'author': pymongo.objectid.ObjectId,<br /> 'created_at': datetime.datetime,<br /> 'tags': [unicode],<br /> }<br />required_fields = ['title','author', 'date_creation']<br />blog_post = BlogPost()<br />blog_post['title'] = 'myblogpost'<br />blog_post['created_at'] = datetime.datetime.utcnow()<br />blog_post.save()<br />
  59. 59. Mongokit (3/3)<br />Speed andperformanceimpact<br />Mongokitisalwaysbehindthemostcurrentpymongoversions<br />one-man developershow<br />http://namlook.github.com/mongokit/<br />
  60. 60. Mongoengine (1/2)<br />MongoEngineis a Document-Object Mapper (think ORM, but fordocumentdatabases) forworkingwithMongoDBfrom Python. Ituses a simple declarative API, similartotheDjango ORM.<br />http://mongoengine.org/<br />
  61. 61. Mongokit (2/2)<br />classBlogPost(Document):<br /> title = StringField(required=True)<br />body = StringField()<br />author = ReferenceField(User)<br />created_at = DateTimeField(required=True)<br /> tags = ListField(StringField())<br />blog_post = BlogPost(title='myblogpost', created_at=datetime.datetime.utcnow())<br />blog_post.save()<br />
  62. 62. MongoAlchemy (1/2)<br />MongoAlchemyis a layer on top ofthe Python MongoDBdriverwhichadds client-sideschemadefinitions, an easiertoworkwithandprogrammaticquerylanguage, and a Document-Objectmapperwhichallowspythonobjectstobesavedandloadedintothedatabase in a type-safe way.<br />An explicit goalofthisprojectistobeabletoperformasmanyoperationsaspossiblewithouthavingtoperform a load/save cyclesincedoing so isbothsignificantlyslowerandmorelikelytocausedataloss.<br />http://mongoalchemy.org/<br />
  63. 63. MongoAlchemy(2/2)<br />frommongoalchemy.documentimportDocument, DocumentField<br />frommongoalchemy.fieldsimport *<br />fromdatetimeimportdatetime<br />frompprintimportpprint<br />class Event(Document):<br />name = StringField()<br />children = ListField(DocumentField('Event'))<br />begin = DateTimeField()<br /> end = DateTimeField()<br />def __init__(self, name, parent=None):<br />Document.__init__(self, name=name)<br />self.children = []<br />ifparent != None:<br />parent.children.append(self)<br />
  64. 64. From SQL toMongoDB<br />
  65. 65. The CAP theorem<br />Consistency<br />Availablity<br />TolerancetonetworkPartitions<br />Pick two...<br />
  66. 66. ACID versus Base<br />Atomicity<br />Consistency<br />Isolation<br />Durability<br />BasicallyAvailable<br />Soft state<br />Eventuallyconsistent<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×