-
-
Save jimklo/1895483 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, rereduce) { | |
var max_ts = ""; | |
var max_id = null; | |
for (var idx=0; idx<values.length; idx++){ | |
if (values[idx].last_update > max_ts) { | |
max_ts = values[idx].last_update; | |
max_id = values[idx].symbol; | |
} | |
} | |
return { "symbol": max_id, "last_update": max_ts}; | |
} | |
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