복붙노트

[HADOOP] MapReduce 값을 내림차순으로 정렬

HADOOP

MapReduce 값을 내림차순으로 정렬

난 내림차순으로 정렬 된 항목을 반환하는 MapReduce 작업을 의사 코드로 작성하려고합니다. 예를 들면 다음과 같습니다 : wordcount 작업의 경우 :

apple 1
banana 3
mango 2

나는 출력을 원한다.

banana 3
mango 2
apple 1

그것을하는 방법의 아이디어? 나는 그것을 오름차순으로 (매퍼 작업에서 키와 값을 대체하는) 수행하는 방법을 알고 있지만 내림차순에서는 수행하지 않습니다.

해결법

  1. ==============================

    1.여기서 내림차순으로 정렬하기 위해 아래의 감속기 코드의 도움을받을 수 있습니다.

    여기서 내림차순으로 정렬하기 위해 아래의 감속기 코드의 도움을받을 수 있습니다.

    매퍼가 (Banana, 1) 등으로 출력을 생성 할 매퍼 및 드라이버 코드를 작성했다고 가정합니다.

    감속기에서 특정 키에 대한 모든 값을 합한 다음 최종 결과를지도에 넣은 다음지도를 값을 기반으로 정렬하고 최종 결과를 reduce의 정리 함수로 작성합니다.

    자세한 내용은 아래 코드를 참조하십시오.

    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 ;
    
    }
    
      }
    
  2. from https://stackoverflow.com/questions/44679841/mapreduce-sort-by-value-in-descending-order by cc-by-sa and MIT license