Mapper and Reducer Program Using MongoDB

Mapper and Reducer Program Using MongoDB

😊 🅷🅴🅻🅻🅾 🅲🅾🅽🅽🅴🅲🆃🅸🅾🅽🆂

In this Article I am going to Create Mapper and Reducer Program in MongoDB database.

✍️ Firstly we wii discuss about Map-Reduce. Then we will create program in MongoDB.

1. Map-Reduce :

MapReduce is a processing technique and a program model for distributed computing based on java. It is designed for processing the data in parallel which is divided on various machines(nodes). The MapReduce algorithm contains two important tasks, namely Map and Reduce. 

i) Mapper :

Mapper is a function or task which is used to process all input records from a file and generate the output which works as input for Reducer. It produces the output by returning new key-value pairs.

ii) Reducer :

Reducer is the second part of the Map-Reduce programming model. But before sending this intermediate key-value pairs directly to the Reducer some process will be done which shuffle and sort the key-value pairs according to its key values, which means the value of the key is the main decisive factor for sorting. The output generated by the Reducer will be the final output.Reducer mainly performs some computation operation like addition, filtration, and aggregation.

No alt text provided for this image

❄️ Now we will create Mapper and Reducer Program using MongoDB.

👉 Firstly we need Collection / Table. So we create one collection ie. student in college database.

👉 In that collection normally there is 4 values in one object such as name, subject, marks and result.

👉 For create Database name as college,

# use college
        

👉 Then we create collection name as "student" in that perticular database ,


# db.createCollection("student")
        

👉 Then we insert many objects in that perticular collection. For inserting ,


# db.student.insert()
        

👉 After we can see all the objects that we added using previous command.

👉 And For see all the objects ,


# db.student.find()
        
No alt text provided for this image

🔰 Problem :

Problem is calculate average of marks per student but condition is result should be "P" means Pass.

🔰 Solutions :

👉 So as we can see in collection there is total 4 students and each student has total 4 subjects. Out off 4 students 3 students get pass result and 1 student get fail result.

👉 Therefore we need to create 3 student's marks average.

👉 To perform map-reduce operations, MongoDB provides the mapReduce database command or function.

🔰 Syntax :


# db.collection.mapReduce(
           
    function() {emit(key,value);},  //map function
    function(key,values) {return reduceFunction}, {   //reduce function
    {
      query: { query },
      out: collection, 
    }
  )        

        

🎯 Now we will create Mapper and Reducer Program using MongoDB.

No alt text provided for this image

👉 Above example , in map function "name" is key and "marks" is value. So whenever same student name find then reduce function will apply. In that reduce function it will calculate the average of same student's marks. And that output will stored in collection name as "marks_avg".

No alt text provided for this image

🎯 Now finally we see average of marks per student using mapper reducer program.

👍 I Hope You Like It..

😊 Thank You for reading..

To view or add a comment, sign in

More articles by Saurabh Kothawade

Others also viewed

Explore content categories