Assgnment 9 Dbms
Assgnment 9 Dbms
Batch:C3
AIM:
Implement Map reduces operation with suitable example using MongoDB.
Objectives:
To learn a powerful, flexible and scalable general purpose database to handle big data
Theory:
MapReduce is a generic multi-phase data aggregation modality for processing quantities of data.
MongoDB provides map-reduce with the MapReduce database command.
The map function is a JavaScript function that associates or “maps” a value with a key and emits
the key and value pair during a map-reduce operation.
In general, map-reduce operations have two phases: a map stage that processes each document and
emits one or more objects for each input document, and reduce phase that combines the output of
the map operation. Optionally, map-reduce can have a finalize stage to make final modifications
to the result. Like other aggregation operations, map-reduce can specify a query condition to select
the input documents as well as sort and limit the results.
Map-reduce uses custom JavaScript functions to perform the map and reduce operations, as well
as the optional finalize operation. While the custom JavaScript provides great flexibility compared
to the aggregation pipeline, in general, MapReduce is less efficient and more complex than the
aggregation pipeline.
Additionally, map-reduce operations can have output sets that exceed the 16 megabyte output
limitation of the aggregation pipeline.
PROGRAM:
1)Create Collection
> db.createCollection("assg");
{ "ok" : 1 }
2)Create Documents in assg Collection
> db.assg.insert({"roll no":1,name:"ram",subject:"CN",marks:78})
WriteResult({ "nInserted" : 1 })
> db.assg.insert({"roll no":2,name:"sss",subject:"SDE",marks:75})
WriteResult({ "nInserted" : 1 })
> db.assg.insert({"roll no":3,name:"shlok",subject:"c++",marks:69})
WriteResult({ "nInserted" : 1 })
> db.assg.insert({"roll no":4,name:"sham",subject:"CN",marks:80})
WriteResult({ "nInserted" : 1 })
> db.assg.insert({"roll no":5,name:"abc",subject:"TOC",marks:70})
WriteResult({ "nInserted" : 1 })
> db.assg.insert({"roll no":6,name:"shubhz",subject:"DSA",marks:90})
WriteResult({ "nInserted" : 1 })
> db.assg.find()
{ "_id" : ObjectId("60701afdd5b6eb313e92e7cd"), "roll no" : 1, "name" : "ram", "subject" :
"CN", "marks" : 78 }
{ "_id" : ObjectId("60701b16d5b6eb313e92e7ce"), "roll no" : 2, "name" : "sss", "subject" :
"SDE", "marks" : 75 }
{ "_id" : ObjectId("60701b2ad5b6eb313e92e7cf"), "roll no" : 3, "name" : "shlok", "subject" :
"c++", "marks" : 69 }
{ "_id" : ObjectId("60701b43d5b6eb313e92e7d0"), "roll no" : 4, "name" : "sham", "subject" :
"CN", "marks" : 80 }
{ "_id" : ObjectId("60701b5fd5b6eb313e92e7d1"), "roll no" : 5, "name" : "abc", "subject" :
"TOC", "marks" : 70 }
{ "_id" : ObjectId("60701b7cd5b6eb313e92e7d2"), "roll no" : 6, "name" : "shubhz", "subject" :
"DSA", "marks" : 90 }
>
3) Map
var mf=function(){emit(this.subject,this.marks);}
4)Reduce
var red=function(key,values){
... var mark=0;
... for(var i=0;i<values.length;i++)
... {
... mark=mark+values[i];
... }
... return mark;
... }
5)MapReduce
> db.assg.mapReduce(mf,red,{out:"Total"})
{ "result" : "Total", "ok" : 1 }
> db.Total.find()
{ "_id" : "CN", "value" : 158 }
{ "_id" : "SDE", "value" : 75 }
{ "_id" : "DSA", "value" : 90 }
{ "_id" : "TOC", "value" : 70 }
{ "_id" : "c++", "value" : 69 }
> var red=function(key,values){ return Array.sum(values) }
> db.assg.mapReduce(mf,red,{out:"Total"})
{ "result" : "Total", "ok" : 1 }
> db.assg.mapReduce(mf,red,{out:"Total2"})
{ "result" : "Total2", "ok" : 1 }
> db.Total2.find()
{ "_id" : "CN", "value" : 158 }
{ "_id" : "SDE", "value" : 75 }
{ "_id" : "TOC", "value" : 70 }
{ "_id" : "DSA", "value" : 90 }
{ "_id" : "c++", "value" : 69 }
PROCEDURE:
Output
Total1:
Total2:
Output:
Remark: