Created
February 23, 2012 22:30
-
-
Save nisbus/1895397 to your computer and use it in GitHub Desktop.
Map reduce problem
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
map function: | |
function(doc) { | |
if(doc.symbol.lastIndexOf("HFF") == 0){ | |
emit(doc.symbol,{last_update:doc.last_updatedutc,symbol:doc.symbol,last_yield:doc.last_yield}); | |
} | |
} | |
View output: | |
{"total_rows":19,"offset":0,"rows":[ | |
{"id":"HFF1502242012-02-22 15:59:59","key":"HFF150224","value":{"last_update":"2012-02-22 15:59:59.00","symbol":"HFF150224","last_yield":0.01094398}}, | |
{"id":"HFF1502242012-02-22 18:04:10.4398","key":"HFF150224","value":{"last_update":"2012-02-22 18:04:10.43","symbol":"HFF150224","last_yield":0.01094398}}, | |
{"id":"HFF1502242012-02-23 09:21:04.5628","key":"HFF150224","value":{"last_update":"2012-02-23 09:21:04.56","symbol":"HFF150224","last_yield":0.01093423}} | |
]} | |
Reduce function: | |
function(keys, values) { | |
result = []; | |
keys.foreach(key,idx,arg){ | |
var k = {yield:0,date:new Date(1, 1, 1, 0, 0, 0, 0)} | |
values.foreach(val,i,a){ | |
if(val.symbol == key && val.last_update > k.date){ | |
k.date = val.last_update; | |
k.yield = val.yield; | |
} | |
} | |
result.push({key, k}); | |
} | |
return result; | |
} | |
result (reduce=true&group=true): | |
{"rows":[ | |
{"key":"HFF150224","value":null}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment