Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas
Created July 12, 2012 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharoonthomas/3096137 to your computer and use it in GitHub Desktop.
Save sharoonthomas/3096137 to your computer and use it in GitHub Desktop.
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