fix map-reduce-example formatting
This commit is contained in:
parent
4e53e1b413
commit
8541b38f2b
1 changed files with 71 additions and 51 deletions
|
@ -10,33 +10,33 @@ This document has been shamelessly ported from the similar
|
||||||
Setup
|
Setup
|
||||||
-----
|
-----
|
||||||
|
|
||||||
To start, we’ll insert some example data which we can perform
|
To start, we'll insert some example data which we can perform
|
||||||
map/reduce queries on:
|
map/reduce queries on:
|
||||||
|
|
||||||
> $ ghci -package mongoDB
|
$ ghci -package mongoDB
|
||||||
> GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help
|
GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help
|
||||||
> ...
|
...
|
||||||
> Prelude> :set prompt "> "
|
Prelude> :set prompt "> "
|
||||||
> > import Database.MongoDB
|
> import Database.MongoDB
|
||||||
> > import Database.MongoDB.BSON
|
> import Database.MongoDB.BSON
|
||||||
> > import Data.ByteString.Lazy.UTF8
|
> import Data.ByteString.Lazy.UTF8
|
||||||
> > c <- connect "localhost" []
|
> c <- connect "localhost" []
|
||||||
> > let col = (fromString "test.mr1")
|
> let col = (fromString "test.mr1")
|
||||||
> > :{
|
> :{
|
||||||
> insertMany c col [
|
insertMany c col [
|
||||||
> (toBsonDoc [("x", BsonInt32 1),
|
(toBsonDoc [("x", BsonInt32 1),
|
||||||
> ("tags", BsonArray [toBson "dog",
|
("tags", BsonArray [toBson "dog",
|
||||||
> toBson "cat"])]),
|
toBson "cat"])]),
|
||||||
> (toBsonDoc [("x", BsonInt32 2),
|
(toBsonDoc [("x", BsonInt32 2),
|
||||||
> ("tags", BsonArray [toBson "cat"])]),
|
("tags", BsonArray [toBson "cat"])]),
|
||||||
> (toBsonDoc [("x", BsonInt32 3),
|
(toBsonDoc [("x", BsonInt32 3),
|
||||||
> ("tags", BsonArray [toBson "mouse",
|
("tags", BsonArray [toBson "mouse",
|
||||||
> toBson "cat",
|
toBson "cat",
|
||||||
> toBson "doc"])]),
|
toBson "doc"])]),
|
||||||
> (toBsonDoc [("x", BsonInt32 4),
|
(toBsonDoc [("x", BsonInt32 4),
|
||||||
> ("tags", BsonArray [])])
|
("tags", BsonArray [])])
|
||||||
> ]
|
]
|
||||||
> :}
|
:}
|
||||||
|
|
||||||
Basic Map/Reduce
|
Basic Map/Reduce
|
||||||
----------------
|
----------------
|
||||||
|
@ -49,36 +49,43 @@ tags array, across the entire collection.
|
||||||
Our map function just emits a single (key, 1) pair for each tag in the
|
Our map function just emits a single (key, 1) pair for each tag in the
|
||||||
array:
|
array:
|
||||||
|
|
||||||
> > :{
|
> :{
|
||||||
> let mapFn = "
|
let mapFn = "
|
||||||
> function() {\n
|
function() {\n
|
||||||
> this.tags.forEach(function(z) {\n
|
this.tags.forEach(function(z) {\n
|
||||||
> emit(z, 1);\n
|
emit(z, 1);\n
|
||||||
> });\n
|
});\n
|
||||||
> }"
|
}"
|
||||||
> :}
|
:}
|
||||||
|
|
||||||
The reduce function sums over all of the emitted values for a given
|
The reduce function sums over all of the emitted values for a given
|
||||||
key:
|
key:
|
||||||
|
|
||||||
> > :{
|
> :{
|
||||||
> let reduceFn = "
|
let reduceFn = "
|
||||||
> function (key, values) {\n
|
function (key, values) {\n
|
||||||
> var total = 0;\n
|
var total = 0;\n
|
||||||
> for (var i = 0; i < values.length; i++) {\n
|
for (var i = 0; i < values.length; i++) {\n
|
||||||
> total += values[i];\n
|
total += values[i];\n
|
||||||
> }\n
|
}\n
|
||||||
> return total;\n
|
return total;\n
|
||||||
> }"
|
}"
|
||||||
> :}
|
:}
|
||||||
|
|
||||||
Note: We can’t just return values.length as the reduce function might
|
Note: We can't just return values.length as the reduce function might
|
||||||
be called iteratively on the results of other reduce steps.
|
be called iteratively on the results of other reduce steps.
|
||||||
|
|
||||||
Finally, we call map_reduce() and iterate over the result collection:
|
Finally, we call map_reduce() and iterate over the result collection:
|
||||||
|
|
||||||
> > mapReduce c col (fromString mapFn) (fromString reduceFn) [] >>= allDocs
|
> mapReduce c col (fromString mapFn) (fromString reduceFn) [] >>= allDocs
|
||||||
> [[(Chunk "_id" Empty,BsonString (Chunk "cat" Empty)),(Chunk "value" Empty,BsonDouble 6.0)],[(Chunk "_id" Empty,BsonString (Chunk "doc" Empty)),(Chunk "value" Empty,BsonDouble 1.0)],[(Chunk "_id" Empty,BsonString (Chunk "dog" Empty)),(Chunk "value" Empty,BsonDouble 3.0)],[(Chunk "_id" Empty,BsonString (Chunk "mouse" Empty)),(Chunk "value" Empty,BsonDouble 2.0)]]
|
[[(Chunk "_id" Empty,BsonString (Chunk "cat" Empty)),
|
||||||
|
(Chunk "value" Empty,BsonDouble 6.0)],
|
||||||
|
[(Chunk "_id" Empty,BsonString (Chunk "doc" Empty)),
|
||||||
|
(Chunk "value" Empty,BsonDouble 1.0)],
|
||||||
|
[(Chunk "_id" Empty,BsonString (Chunk "dog" Empty)),
|
||||||
|
(Chunk "value" Empty,BsonDouble 3.0)],
|
||||||
|
[(Chunk "_id" Empty,BsonString (Chunk "mouse" Empty)),
|
||||||
|
(Chunk "value" Empty,BsonDouble 2.0)]]
|
||||||
|
|
||||||
Advanced Map/Reduce
|
Advanced Map/Reduce
|
||||||
-------------------
|
-------------------
|
||||||
|
@ -86,11 +93,24 @@ Advanced Map/Reduce
|
||||||
MongoDB returns additional information in the map/reduce results. To
|
MongoDB returns additional information in the map/reduce results. To
|
||||||
obtain them, use *runMapReduce*:
|
obtain them, use *runMapReduce*:
|
||||||
|
|
||||||
> > res <- runMapReduce c col (fromString mapFn) (fromString reduceFn) []
|
> res <- runMapReduce c col (fromString mapFn) (fromString reduceFn) []
|
||||||
> > res
|
> res
|
||||||
> [(Chunk "result" Empty,BsonString (Chunk "tmp.mr.mapreduce_1268105512_18" Empty)),(Chunk "timeMillis" Empty,BsonInt32 90),(Chunk "counts" Empty,BsonDoc [(Chunk "input" Empty,BsonInt64 8),(Chunk "emit" Empty,BsonInt64 12),(Chunk "output" Empty,BsonInt64 4)]),(Chunk "ok" Empty,BsonDouble 1.0)]
|
[(Chunk "result" Empty, BsonString (Chunk "tmp.mr.mapreduce_1268105512_18" Empty)),
|
||||||
|
(Chunk "timeMillis" Empty, BsonInt32 90),
|
||||||
|
(Chunk "counts" Empty,
|
||||||
|
BsonDoc [(Chunk "input" Empty,BsonInt64 8),
|
||||||
|
(Chunk "emit" Empty,BsonInt64 12),
|
||||||
|
(Chunk "output" Empty,BsonInt64 4)]),
|
||||||
|
(Chunk "ok" Empty,BsonDouble 1.0)]
|
||||||
|
|
||||||
You can then obtain the results using *mapReduceResults*:
|
You can then obtain the results using *mapReduceResults*:
|
||||||
|
|
||||||
> > mapReduceResults c (fromString "test") res >>= allDocs
|
> mapReduceResults c (fromString "test") res >>= allDocs
|
||||||
> [[(Chunk "_id" Empty,BsonString (Chunk "cat" Empty)),(Chunk "value" Empty,BsonDouble 6.0)],[(Chunk "_id" Empty,BsonString (Chunk "doc" Empty)),(Chunk "value" Empty,BsonDouble 1.0)],[(Chunk "_id" Empty,BsonString (Chunk "dog" Empty)),(Chunk "value" Empty,BsonDouble 3.0)],[(Chunk "_id" Empty,BsonString (Chunk "mouse" Empty)),(Chunk "value" Empty,BsonDouble 2.0)]]
|
[[(Chunk "_id" Empty,BsonString (Chunk "cat" Empty)),
|
||||||
|
(Chunk "value" Empty,BsonDouble 6.0)],
|
||||||
|
[(Chunk "_id" Empty,BsonString (Chunk "doc" Empty)),
|
||||||
|
(Chunk "value" Empty,BsonDouble 1.0)],
|
||||||
|
[(Chunk "_id" Empty,BsonString (Chunk "dog" Empty)),
|
||||||
|
(Chunk "value" Empty,BsonDouble 3.0)],
|
||||||
|
[(Chunk "_id" Empty,BsonString (Chunk "mouse" Empty)),
|
||||||
|
(Chunk "value" Empty,BsonDouble 2.0)]]
|
||||||
|
|
Loading…
Reference in a new issue