2017-11-06 1 views
0

내가 hadoop mapreduce 프로그래밍 패러다임을 처음 사용하는 사람은 어떻게 값을 기반으로 쉽게 정렬 할 수 있습니까? 나는 다른 비교기 클래스를 구현하려했지만 감속기의 값을 기준으로 정렬 작업을 수행하는 것과 같은 간단한 방법이 있습니다. 기본적으로 로그 파일을 읽고 있는데 오름차순으로 조회수를 계산할 URL을 주문하고 싶습니다.감속기의 값을 기준으로 오름차순 정렬

public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { 

private final static IntWritable ONE = new IntWritable(1); 
private Text word = new Text(); 

public void map(Object key, Text value, Context context 
       ) throws IOException, InterruptedException { 
    String[] split = value.toString().split(" "); 
    for(int i=0; i<split.length; i++){ 
     if (i==6) 
      word.set(split[i]); 
      context.write(word, ONE); 
    } 
} 
} 

public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {  
private IntWritable result = new IntWritable(); 

public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { 
    int sum = 0; 
    for (IntWritable val : values) { 
    sum += val.get(); 
    } 
    result.set(sum); 
    context.write(key, result);  
} 
} 
+0

쉽게 정렬 할 수 있습니까? MapReduce를 작성하지 마십시오. 돼지/하이브/스파크 사용 –

+0

불행히도 맵 제한을 사용해야합니다. – cowgirl

답변

0

감속기 클래스 내에 하나의지도를 선언하고 키와 값을지도에 넣습니다. 감속기 클래스의 cleanup() 메소드에서 값으로 맵을 정렬 한 다음 마지막으로 context.write (key, value)에 값을 제공하십시오.

public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {  
private IntWritable result = new IntWritable(); 

TreeMap<Text,IntWritable>result=new TreeMap<Text, IntWritable>(); 

public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { 
    int sum = 0; 
    for (IntWritable val : values) { 
    sum += val.get(); 
    } 
result.put(new Text(key),new IntWritable(sum)); 
} 
} 

    @Override 
    protected void cleanup(Context context) 
      throws IOException, InterruptedException { 

     Set<Entry<Text, IntWritable>> set = result.entrySet(); 
     List<Entry<Text, IntWritable>> list = new ArrayList<Entry<Text,IntWritable>>(set); 
     Collections.sort(list, new Comparator<Map.Entry<Text, IntWritable>>() 
     { 
      public int compare(Map.Entry<Text, IntWritable> o1, Map.Entry<Text,IntWritable> o2) 
      { 
       return (o2.getValue()).compareTo(o1.getValue()); 
      } 
     }); 
     for(Map.Entry<Text,IntWritable> entry:list){ 

      context.write(entry.getKey(),entry.getValue()); 
     } 

    } 
    } 
0

이 경우 맵 축소 작업을 두 번 작성해야합니다. 첫 번째 직업은 URL 수입니다. 될 것입니다 fisrt 작업의 출력과 같은 -

yahoo.com,100 
google.com,200 
msn.com,50 

패스이 두 번째지도에 작업을 줄이고 그 수에 따라 정렬합니다.