Created
December 29, 2010 09:09
-
-
Save ruanwz/758355 to your computer and use it in GitHub Desktop.
This file contains 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
doc1= | |
"_id" : 12345678 | |
"starttime" : "20101207010500" | |
"length" : 300 | |
"interval" : 60 | |
"device_data" : | |
"2323" : | |
"123" : | |
"ca" : [ 4, 7, 3, 3, 3 ] | |
"cd" : [ 7, 3, 6, 9, 8 ] | |
"1234" : | |
"ca" : [ 5, 2, 4, 3, 9 ] | |
"cd" : [ 0, 6, 8, 7, 4 ] | |
"2324" : | |
"123" : | |
"ca" : [ 7, 1, 5, 8, 1 ] | |
"cd" : [ 7, 9, 1, 4, 5 ] | |
"1234" : | |
"ca" : [ 5, 2, 5, 3, 5 ] | |
"cd" : [ 8, 9, 3, 5, 2 ] | |
"mac" : "C8D2C11A4D00" | |
doc2= | |
"_id" : 12345673 | |
"starttime" : "20101207011000" | |
"length" : 300 | |
"interval" : 60 | |
"device_data" : | |
"2323" : | |
"123" : | |
"ca" : [ 4, 7, 9, 3, 4 ] | |
"cd" : [ 7, 3, 9, 9, 4 ] | |
"1234" : | |
"ca" : [ 5, 2, 9, 3, 4 ] | |
"cd" : [ 0, 6, 9, 7, 4 ] | |
"2324" : | |
"123" : | |
"ca" : [ 7, 1, 9, 8, 4 ] | |
"cd" : [ 7, 9, 9, 4, 4 ] | |
"1234" : | |
"ca" : [ 5, 2, 9, 3, 4 ] | |
"cd" : [ 8, 9, 9, 5, 4 ] | |
"mac" : "C8D2C11A4D00" | |
map=(doc)-> | |
#console.log(doc.length) | |
starttime = doc.starttime.slice(0,10) | |
delete doc.starttime | |
delete doc.length | |
delete doc.interval | |
for dk,device of doc.device_data | |
for sk,sensor of device | |
for ak,attr of sensor | |
sum = 0 | |
for v in attr | |
sum += v | |
sensor[ak]=sum | |
#console.log doc | |
map_doc= | |
id: [doc.mac, starttime] | |
doc: doc | |
return map_doc | |
reduce=(key,docs)-> | |
sum= | |
mac: key[0] | |
starttime: key[1] | |
for doc in docs | |
for dk,device of doc.device_data | |
for sk, sensor of device | |
for ak, attr of sensor | |
sum["device_data"]?={} | |
sum.device_data[dk]?={} | |
sum.device_data[dk]?={} | |
sum.device_data[dk][sk]?={} | |
sum.device_data[dk][sk][ak]?=0 | |
sum.device_data[dk][sk][ak]+=attr | |
console.log "the final doc:" | |
console.log sum | |
ret= | |
id: key | |
value: sum | |
return ret | |
ret_doc=map doc1 | |
ret_doc2=map doc2 | |
#console.log ret_doc | |
console.log "value from doc1:" | |
console.log ret_doc["doc"]["device_data"]["2323"]["123"] | |
console.log "value from doc2:" | |
console.log ret_doc2["doc"]["device_data"]["2323"]["123"] | |
ret_doc=reduce ret_doc.id, [ret_doc.doc,ret_doc2.doc] | |
console.log "value from doc1 and doc2 after map-reduce:" | |
console.log ret_doc["value"]["device_data"]["2323"]["123"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment