 |
MapReduce function in MongoDB with the example |
=> BASIC OF MapReduce
syntax: db.collectionname.mapReduce(
function(){emit(key,value)},
function(key,value){return reducefunction},
{
query:document,
sort:document,
limit:number,
verbose:boolean,
out:newcollection
})
In Detail Explaination
=> The MapReduce() the first Query's the Collection then maps the result the document to emit key-value pairs which are then reduced based on the keys that have multiple values.
=> Map: It is a javascript function that maps a value with a key and emits key-value pair.
=> Reduce: It is a javascript function that reduces or groups all the documents having the same key.
=> out: It specify's the location of the map reduce (Query if Fire) result.
=> query: It is used to specify the optional selection criteria for selecting the documents.
=> verbose: It is used to specify Boolean Value which signifies whether the details regarding the functioning of map-reduce should be displayed or not. (By default TRUE)
=> limit: It specifies the number of documents which you want to display.
=>Using Of MapReduce
>use emp
>db.createCollection("stud")
>db.stud.insert([
{eno:1,ename:"Smit",course:"MBA",collage:"Marwadi"},
{eno:2,ename:"Amit",course:"BE_IT",collage:"Marwadi"},{eno:3,ename:"Dipak",course:"MCA",collage:"Marwadi"},
{eno:4,ename:"Umang",course:"BE_COMPUTER",collage:"Marwadi"}])
=>Create map and reduce function
> var map = function(){emit(this.ename,this.collage);}
> var reduce = function(key,value){return Array.sum(value)}
> db.stud.mapReduce(map,reduce,{out:"res1"})
OUTPUT IS
{
"result" : "res1",
"timeMillis" : 2109,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 0,
"output" : 4
},
"ok" : 1
}
=>Displayed Reduced Documents which we can store in the new Collection res1.
> db.res1.find()
{ "_id" : "Amit", "value" : "Marwadi" }
{ "_id" : "Dipak", "value" : "Marwadi" }
{ "_id" : "Smit", "value" : "Marwadi" }
{ "_id" : "Umang", "value" : "Marwadi" }
NOTE: For COUNT Put <1> in Value in emit().(eg.(key,1)) 1 is there Because every time it can be matched same value then it will be increased automatically.
=>Count total Reduced Documents and store in res2.
> var map = function(){emit(this.collage,1);}
> var reduce = function(key,value){return Array.sum(value)
> db.stud.mapReduce(map,reduce,{out:"res2"})
{
"result" : "res2",
"timeMillis" : 771,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 1,
"output" : 1
},
"ok" : 1
}
=>Displayed Reduced Docuements which we can store in the new Collection res2.
> db.res2.find()
{ "_id" : "Marwadi", "value" : 4 }
For Different Result
=>Update the document set Collage name IIT where name Smit, Amit.
> db.stud.update({ename:"Smit"},{$set:{collage:"IIT"}})
> db.stud.update({ename:"Dipak"},{$set:{collage:"IIT"}})
=>Now Map Reduce result store in res3.
> db.stud.mapReduce(map,reduce,{out:"res3"})
{
"result" : "res3",
"timeMillis" : 1051,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 2
},
"ok" : 1
}
NOTE: From the Following output it can be matched with different colleges so matching value changed that is "2" in this output.
> db.res3.find()
{ "_id" : "IIT", "value" : 2 }
{ "_id" : "Marwadi", "value" : 2 }
Comments
Post a Comment