What you are doing above should work as well, but this is assuming that all of the data for a single key will fit in memory. If it does, then at Reducer you can hold all values in memory and then compute your total to then calculate the marginal for each key-value pair. This is commonly known as the "stripes" approach.
However, most of the times this might now be true and the data might not fit in memory. In this case you will have to find a way to send values to compute your total before the actual key-value pair so that when they can then be used to compute the marginal and emit the value right away.
This is a candidate for the "order of inversion" design pattern. Its useful when you need to calculate relative frequencies. The basic idea is at the Mapper's end you emit 2 key-value pairs for each intermediate data where one of the key-value pair will have the same common key for all values. This will be used to calculate the total.
Example:
For a, (r, 5) :
---------------
emit (a, r), 5
emit (a, *), 5
For a, (e, 6) :
---------------
emit (a, e), 6
emit (a, *), 6
For a, (w, 7) :
---------------
emit (a, w), 7
emit (a, *), 7
Once this is done, you need a partitioner that will partition each of the intermediate key-value pair using only the first value in the key. In the example above using "a".
You will also need a key sort order that always places the key having * in the second part of the key above all.
This way all intermediate keys have "a" in the first part of the key will end up in the same reducer. Also, they will sorted in a fashion as shown below -
emit (a, *), 5
emit (a, *), 6
emit (a, *), 7
emit (a, e), 6
emit (a, r), 5
emit (a, w), 7
At the reducer as you iterate through the key-value pairs, you will have to simply accumulate the values from the keys if they have a * in the second part of the key. You can then use the accumulated value to calculate your marginal for all the other key-value pairs.
total = 0
for(value : values){
if (key.second == *)
total += value
else
emit (key.first , key.second, value, value/total)
}
This design pattern is commonly known as Order of inversion that uses the pairs approach.
For more info on this and other design patterns I would suggest reading the chapter on MapReduce design patterns in this book - http://lintool.github.com/MapReduceAlgorithms/.
It very well explained with examples.