Finding Top-K using Python & Hadoop Streaming
<p>So I have an output file from a previous job in this format (.txt file) </p>

<pre><code>"   145
"Defects,"  1
"Information    1
"Plain  2
"Project    5
"Right  1
#51302] 1
$5,000) 1
&amp;   3
'AS-IS',    1
(   1
("the   1
</code></pre>

<p>The left hand side of each line is the word that I read from the document and the number on the right hand side of each line is the number of times I have counted it. I want to create another map reduce job, using Python &amp; Hadoop Streaming, to find the top-k values. Let's say 5 in this case. I am having trouble visualizing what the mapper is supposed to do. </p>

<p>Should I be parsing each line and appending each word and count to a list. Then from those lists, will I be taking the top-k values and sending it to the reducer? Then the reducer reads through all these lists and returns only the top-k values? If someone could offer some advice through pseudo code or correct me if I am on the wrong path it would be appreciated. Thanks!</p>

<p>You are almost on the right track. Consider your word as the Key and the count as the Value for your mapper task. If in your input file, you can get multiple entries for the same word and different count, then you can't take out top K from it. Then you shall have to aggregate the data and then find out top K . This shall be done in reducer then. As reducer shall receive all the data for the same key, it can aggregate the complete data and take out top K. But then there has to be another chained map reduce to find out  top K amongst all records, where you shall have 1 reducer for finding the top elements.</p>

<p>But if your input file has an entry for the key once, you can just emit top K from all mappers and then send it to 1 Reducer to find out the top K from all the map entries. </p>

python python-2.7 hadoop mapreduce hadoop-streaming