Database Reference
In-Depth Information
mean: 0,
ts: null });
}'''
)
In this case,
mapf_hour
emits key-value pairs that contain the data you want to aggregate, as
you'd expect. The function also emits a
ts
value that makes it possible to cascade aggrega-
tions to coarser-grained aggregations (hour to day, etc.).
Next, we define the following reduce function:
reducef
=
bson
.
Code
(
'''function(key, values) {
var r = { total: 0, count: 0, mean: 0, ts: null };
values.forEach(function(v) {
r.total += v.total;
r.count += v.count;
});
return r;
}'''
)
The reduce function returns a document in the same format as the output of the map function.
This pattern for map and reduce functions makes MapReduce processes easier to test and de-
bug.
While the reduce function ignores the
mean
and
ts
(timestamp) values, the finalize step, as
follows, computes these data:
finalizef
=
bson
.
Code
(
'''function(key, value) {
if(value.count > 0) {
value.mean = value.total / value.count;
}
value.ts = new Date();
return value;
}'''
)
With the preceding functions defined, our actual
mapreduce
call resembles the following:
cutoff
=
datetime
.
utcnow
()
-
timedelta
(
seconds
=
60
)
query
=
{
'ts'
: {
'$gt'
:
last_run
,
'$lt'
:
cutoff
} }
db
.
events
.
map_reduce
(
map
=
mapf_hour
,