-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmapreduce.py
More file actions
36 lines (31 loc) · 746 Bytes
/
mapreduce.py
File metadata and controls
36 lines (31 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#coding=utf8
from pymongo import Connection
from bson.code import Code
db = Connection().test
#先删除things集合
db.things.drop()
db.things.insert({"x":1,"tags":["dog","cat"]})
db.things.insert({"x":2,"tags":["cat"]})
db.things.insert({"x":3,"tags":["mouse","cat","dog"]})
db.things.insert({"x":4,"tags":[]})
mapper = Code("""function() {
this.tags.forEach(function(z) {
emit(z, 1);
});
}
""")
reducer = Code("""
function(key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i];
}
return total;
}
""")
result=db.things.map_reduce(mapper,reducer,out ="myresults",full_response=True,query={"tags":{"$exists": "true"}})
print result
for doc in db.myresults.find():
print doc
#删除保存结果的集合
db.myresults.d