2017-06-21 111 views
0

我想在僞代碼中編寫一個MapReduce任務,該任務返回按降序排序的項目。例如:對於單詞計數的任務,而不是獲取:MapReduce按價值降序排列

apple 1 
banana 3 
mango 2 

我所要的輸出是:

banana 3 
mango 2 
apple 1 

的怎麼辦呢任何想法?我知道如何按照升序排列(替換mapper作業中的鍵和值),但不是按降序排列。

+1

只要搜索「MapReduce的第二排序」,你會發現很多的例子。 –

+0

@BinaryNerd如果我沒有弄錯,這不是次要的。這只是一種比第二種更容易的價值。 – philantrovert

+0

按值排序是hadoop mapreduce中的次要排序,主要排序在關鍵字上。 –

回答

0

在這裏你可以幫助下面的reducer代碼實現降序排序。

假設你已經寫映射器和驅動程序代碼,其中映射器會產生輸出(香蕉,1)等

在減速,我們將總結所有值的一個特定的鍵,並把最終結果在地圖中,然後進行排序地圖在值的基礎上寫出最終的結果,並在清理函數中減少。

請參見下面的代碼爲進一步understadnind:

public class Word_Reducer extends Reducer<Text,IntWritable, Text , 
    IntWritable> { 
// Change access modifier as per your need 
public Map<String , Integer > map = new LinkedHashMap<String , Integer>(); 
public void reduce(Text key , Iterable<IntWritable> values ,Context context 
) 
{ 

// write logic for your reducer 
// Enter reduced values in map for each key 
for (IntWritable value : values){ 

    // calculate "count" associated with each word 

} 
map.put(key.toString() , count); 



    } 

     public void cleanup(Context context){ 
    //Cleanup is called once at the end to finish off anything for reducer 
    //Here we will write our final output 
    Map<String , Integer> sortedMap = new HashMap<String , Integer>(); 

/
    sortedMap = sortMap(map); 

    for (Map.Entry<String,Integer> entry = sortedMap.entrySet()){ 
    context.write(new Text(entry.getKey()),new 
     IntWritable(entry.getValue())); 
     } 


    } 
    public Map<String , Integer > sortMap (Map<String,Integer> unsortMap){ 

    Map<String ,Integer> hashmap = new LinkedHashMap<String,Integer>(); 
    int count=0; 
    List<Map.Entry<String,Integer>> list = new 
    LinkedList<Map.Entry<String,Integer>>(unsortMap.entrySet()); 
//Sorting the list we created from unsorted Map 
    Collections.sort(list , new Comparator<Map.Entry<String,Integer>>(){ 

    public int compare (Map.Entry<String , Integer> o1 , Map.Entry<String , 
     Integer> o2){ 
     //sorting in descending order 
     return o2.getValue().compareTo(o1.getValue()); 

    } 


}); 

    for(Map.Entry<String, Integer> entry : list){ 
    // only writing top 3 in the sorted map 
     if(count>2) 
     break; 

     hashmap.put(entry.getKey(),entry.getValue()); 


} 

    return hashmap ; 

} 

    } 
+0

謝謝你的回答!我從你的回答中瞭解如何對同一個鍵的值進行排序。我不認爲所有字符串和整數的地圖都將通過這個全過程得以保存。另外,我如何知道何時調用清理函數? –

+1

清理功能在reduce任務結束時自動調用一次。 –

+0

好的,很好。我提到的第一件事情呢?我不認爲鍵和值的映射被保存並在整個過程中被更新,我認爲唯一的變量是Counter。我錯了嗎? –