Monami Sen
Monami Sen

Reputation: 129

How to design the Key Value pairs for Mapreduce to find the maximum value in a set?

I am beginner of MapReduce programmer. Can you help me design the key-value pairs for the following problem ?

Problem statement - Find the maximum value and print it along with the key

Input :

   Key       Value
   ABC       10
   TCA       13
   RTY       23
   FTY       45

The key on the left-hand side column will be unique.No duplicates allowed.

Output :

   FTY       45

Since 45 is the highest of all values, it has to be printed along with the key.

Can you help me in designing the map() and reduce() function? What will be the key-value pairs for both functions?

Upvotes: 0

Views: 462

Answers (1)

AdamSkywalker
AdamSkywalker

Reputation: 11609

In mapper, remember the greatest number

class Mapper {
   V maxV;
   K maxK;

   map(K, V, context) {
      if (V > maxV) { maxV = V; maxK = K; }
   }

   cleanup(context) {
      context.store(maxK, maxV)
   }
}

Do the same in reducer. Configure the job to have only 1 reducer.

Upvotes: 1

Related Questions