I have a problem with a python-script witch i use to aggregate large collections into smaller pieces and have them grouped by a timestamp.
map = Code("function(number) {"
"emit({"
"ts : new Date(new Date((this.ts - (this.ts % (60 * number))) * 1000).setSeconds(0))"
"}, 1);"
"}")
reduce = Code("function(key, vals) {"
"var sum = 0;"
"for (var i in vals) {"
"sum += vals[i]"
"}"
"return sum;"
"}")
As you can see, it’s a pretty simple MapReduce and the timestamp (ts) should be grouped by anny given number of minutes. I’ve tested the Javascript here http://jsfiddle.net/QgMzK/1/ and it seems to work fine. But when i run it in Python all timestamps become ISODate(“1970-01-01T00:00:00Z”).
Any ideas?
Your map function takes one parameter:
number, which, when called by map-reduce, will be set to null and after coertion (and some%by zero) will make the date your map is returningISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ"). This after type conversion becomesdatetime.datetime(1970, 1, 1, 0, 0).Remove the parameter and it should work.
edit
To confirm that, try running this code:
The result on my machine is :
Note that
tsisNonein the result, causenumberwas not set when the mapping function executed.edit 2
AFAIK the only way to pass a parameter to
mapis by usingscopeoptional parameter inmap_reduce, but then you’ll have to remove it from map signature anyway.So by changing your map to:
and by calling:
you can get the result you want.