2016-06-13 46 views
0

我實際上試圖開發一個使用python的特定數據的映射器和reducer。 我已經編寫了映射器代碼,該代碼將提供商店名稱和在商店完成的交易成本。比較python中的一個鍵的多個值

例如:

Nike $45.99 Adidas $72.99 Puma $56.99 Nike $109.99 Adidas $85.99

這裏的關鍵是商店名稱和價值是交易的成本。 現在我正試圖編寫比較每個商店的交易成本並在每個商店給予最高交易的還原劑代碼。

現在,我想要得到的輸出是

Nike $109.99 Adidas $85.99 Puma $56.99

我的問題是我怎麼可以比較給Python中的鍵的值不同?

+0

數據格式如何?它是加載到一個字典,它是保存到一個TXT文件等...? – TheLazyScripter

+0

它保存在一個我稍後會導入的文本文件中。 – Praneeth

回答

1

那麼,MapReduce範式是一個鍵 - 值對,每個映射器應以確切的格式輸出。

至於reducer,hadoop框架保證每個reducer使用shuffle-sort算法,將獲得某個鍵的所有值,所以不可能兩個不同的reducer將從同一個鍵獲得不同的條目。

但是,減速器可以有多個關鍵值來處理。

至於你的問題,讓我們假設你有相同的鍵3個不同的值,例如:

Nike $109.99 
Nike $45.99 
Nike $294.99 

的減速首先將獲得2個值,所以根據你的鑰匙,減速功能將得到值:

  • $109.99
  • $45.99

,將需要輸出使用簡單的比較最高的國家之一,並且輸出應該是$109.99這將是你的減速功能將運行第2次輸入,此時輸入:

  • $109.99
  • $294.99

再次,通過比較,你應該輸出的最高值,這就是:$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) 
+0

謝謝你的幫助Avihoo – Praneeth

0
def largets_value(_dict): 
    d = {} 
    for i, v in enumerate(_dict.keys()): 
     d[v] = max(_dict.values()[i]) 
    return d 

def dict_from_txt(file, sep): 
    d = {} 
    f = [x.rstrip().replace('$', '').split(sep) for x in open(file, 'rb').readlines()] 
    for i in f: 
     if i[0] in d: 
      d[i[0]].append(float(i[1])) 
     else: 
      d[i[0]] = [float(i[1])] 
    return d 

def dict_from_iterable(iterable, sep): 
    d = {} 
    f = [x.rstrip().replace('$', '').split(sep) for x in iterable] 
    for i in f: 
     if i[0] in d: 
      d[i[0]].append(float(i[1])) 
     else: 
      d[i[0]] = [float(i[1])] 
    return d 

data = ['Nike $45.99', 
     'Adidas $72.99', 
     'Puma $56.99', 
     'Nike $109.99', 
     'Adidas $85.99'] 
print largets_value(dict_from_iterable(data, ' ')) 
#Uncomment next line and delete the previous to use for yourself 
#print largets_value(dict_from_txt('my_file', ' ')) 
0

Hadoop應該先將映射器的輸出排序,然後再傳遞給reducer。既然你可以使用itertools.groupby()像鍵羣到一個列表,然後選擇從每個分組列表中的最大:

#!/usr/bin/env python 

import sys 
from itertools import groupby 

for store, transactions in groupby((line.split() for line in sys.stdin), 
            key=lambda line: line[0]): 
    print(store, max(float(amount[1].replace('$', '')) for amount in transactions)) 

這當然假定您映射器的輸出由用於儲存2個空格分隔的字段和交易價值。