Skip to main content

MapReduce max marks per college

Short answer

Map input: (byte offest, 'Ram,ABC,70') etc. for all eight rows. Mapper outputs (college, marks) pairs. Reducer input: (ABC,[70,80,60]), (PQR,[90,70,60]), (XYZ,[90,80]). Final output maximum marks: ABC 80%, PQR 90%, XYZ 90%.

For following data, list the input to/output from both the map and reduce functions for getting maximum marks of each college.

Student NameCollege NameFinal Marks in %
RamABC70
SitaABC80
HariABC60
GitaXYZ90
RitaXYZ80
ShyamPQR90
LaxmiPQR70
GopalPQR60

We use one MapReduce job. The mapper emits (college, marks) for each record. The framework groups values by key (shuffle and sort). The reducer takes the maximum of the list of marks for each college.

Mapper and reducer pseudocode

map(key = byte offset, value = "Name,College,Marks"):
    fields = split(value, ",")
    emit(trim(fields[1]), int(trim(fields[2])))   // (college, marks)

reduce(key = college, values = list of marks):
    max = 0
    for v in values:
        if v > max: max = v
    emit(college, max)

Input to the map function

Each row is one (key, value) pair. The key is the byte offset of the line (not used), the value is the line.

Key (offset)Value
0Ram, ABC, 70
1Sita, ABC, 80
2Hari, ABC, 60
3Gita, XYZ, 90
4Rita, XYZ, 80
5Shyam, PQR, 90
6Laxmi, PQR, 70
7Gopal, PQR, 60

(Offsets are shown as line numbers for simplicity.)

Output of the map function (input to shuffle)

(ABC, 70)   (ABC, 80)   (ABC, 60)
(XYZ, 90)   (XYZ, 80)
(PQR, 90)   (PQR, 70)   (PQR, 60)

After shuffle and sort (input to the reduce function)

The framework groups all values of the same key and sorts keys:

KeyList of values
ABC[70, 80, 60]
PQR[90, 70, 60]
XYZ[90, 80]

Output of the reduce function (final output)

CollegeMaximum marks
ABC80
PQR90
XYZ90

Data flow

 Input lines -> MAP -> (college,marks)
      -> SHUFFLE/SORT -> (college,[marks])
      -> REDUCE -> (college,max)

Since max is associative and commutative, the same reducer can also be used as a combiner, to reduce network traffic by sending only the local maximum from each mapper.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Big Data Technologies chapter 3 questions →