2014-05-19 2 views
0

나는 내 코드에서 틀린 것이 틀림 없다고 생각했지만 그걸 찾지 못했습니다. 내 프로그램에서Mapreduce를 사용할 때 왜 장애가 발생했는지

내가 가진 :

public static class BrowserReduce extends 
     Reducer<Text, IntWritable, IntWritable, Text> { 
    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(result, key); 
    } 
} 

내 일

public static class BrowserMapper extends 
     Mapper<LongWritable, Text, Text, IntWritable> 

은 다음과 같이 구성 :이 작업을 실행할 때

public int run(String[] args) throws Exception { 
    Job job = Job.getInstance(); 

    job.setMapOutputKeyClass(Text.class); 
    job.setMapOutputValueClass(IntWritable.class); 

    job.setOutputKeyClass(IntWritable.class); 
    job.setOutputValueClass(Text.class); 

    job.setMapperClass(BrowserMapper.class); 
    job.setReducerClass(BrowserReduce.class); 

    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 

    FileInputFormat.setInputPaths(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

    job.setJarByClass(LogAnalysis.class); 

    job.waitForCompletion(true); 

    return 0; 
} 

, 나는이 같은 결과를 얻었다 :

175394 IE 
1475014 chrome 
508390 firefox 
23566 opera 
421729 other 
1266627 safari 
,

하지만이 같은 예상 된 결과 :

1475014 chrome 
1266627 safari 
508390 firefox 
421729 other 
175394 IE 
23566 opera 

어떤 생각? 많은 감사

답변

1

결과는 Reducer로 전송 된 키 (예 : IE, chrome ...)로 정렬됩니다. 두 번째 열이 알파벳순으로 정렬되어 있음을 알 수 있습니까?

합계 개수로 정렬하려면 다른 MapReduce 단계를 추가하고 개수를 키로 설정해야합니다.

1

변수 개인 인스턴스를 생성

TreeMap sortedMap = new TreeMap(); 
대신 context.write 일을

(결과, 키); Reducer에서 TreeMap에 그 값을 저장하십시오. sortedMap.put(sum,key);

정리 방법에서 출력을 원하는 방식으로 context.write()를 수행 할 수 있습니다. TreeMap은 출력을 정렬합니다. 그래서 당신은 당신이 찾고있는 것을 성취 할 것입니다.

관련 문제