Created
July 12, 2012 05:51
-
-
Save sharoonthomas/3096137 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mongoengine import Document, StringField | |
class Machine(Document): | |
data_center = StringField(choices=[ | |
('us-east', 'US East'), | |
('us-west', 'US West'), | |
('sa-sao', 'South America (SAO)'), | |
('eu', 'European Union'), | |
]) | |
# Connect to a mongo database | |
from mongoengine import connect | |
connect('test_map_reduce') | |
# Now let's create some random records to test map reduce | |
for x in xrange(0, 1000): | |
# Choose one of the data centers in random | |
Machine( | |
data_center=random.choice( | |
['us-east', 'us-west', 'sa-sao', 'eu'] | |
) | |
).save() | |
# Now execute the map reduce | |
The map function references the variable this to inspect the current object under consideration. See http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-MapFunction | |
from bson import Code | |
map = Code("""function () { | |
emit(this.data_center, {count: 1}); | |
}""") | |
# The reduce function | |
reduce = Code("""function(data_center, record_list) { | |
var result = {count: 0}; | |
record_list.forEach(function (record) { | |
result.count += record.count; | |
}); | |
return result; | |
}""") | |
result = Machine.objects.map_reduce(map, reduce, 'inline') | |
for document in result: | |
print document.key, document.value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment