那麼,MapReduce
範式是一個鍵 - 值對,每個映射器應以確切的格式輸出。
至於reducer,hadoop框架保證每個reducer使用shuffle-sort算法,將獲得某個鍵的所有值,所以不可能兩個不同的reducer將從同一個鍵獲得不同的條目。
但是,減速器可以有多個關鍵值來處理。
至於你的問題,讓我們假設你有相同的鍵3個不同的值,例如:
Nike $109.99
Nike $45.99
Nike $294.99
的減速首先將獲得2個值,所以根據你的鑰匙,減速功能將得到值:
,將需要輸出使用簡單的比較最高的國家之一,並且輸出應該是$109.99
這將是你的減速功能將運行第2次輸入,此時輸入:
再次,通過比較,你應該輸出的最高值,這就是:$294.99
至於代碼,您將需要一個很簡單的功能,像:
編輯:我認爲你的分隔符是標籤,但你可以改變格式,無論你正在使用
#!/usr/bin/env python
import sys
current_word = None
current_max_count = 0
word = None
# input comes from STDIN
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# parse the input we got from mapper.py
word, count = line.split('\t', 1)
# convert count (currently a string) to int
try:
count = int(count)
except ValueError:
# count was not a number, so silently
# ignore/discard this line
continue
# this IF-switch only works because Hadoop sorts map output
# by key (here: word) before it is passed to the reducer
if current_word == word:
if count > current_max_count:
current_max_count = count
else:
if current_word:
# write result to STDOUT
print '%s\t%s' % (current_word, current_max_count)
current_max_count = count
current_word = word
# do not forget to output the last word if needed!
if current_word == word:
print '%s\t%s' % (current_word, current_max_count)
數據格式如何?它是加載到一個字典,它是保存到一個TXT文件等...? – TheLazyScripter
它保存在一個我稍後會導入的文本文件中。 – Praneeth