복붙노트

[HADOOP] wordcount hadoop 출력 결과를 다시 정렬하고 값별로 정렬하는 방법

HADOOP

wordcount hadoop 출력 결과를 다시 정렬하고 값별로 정렬하는 방법

아래 코드를 사용하여 (Key, Value)와 같은 출력 결과를 얻습니다.

Apple 12
Bee 345 
Cat 123

내가 원하는 것은 값 (345)으로 정렬되어 내림차순으로 키 앞에 배치하는 것입니다 (Value, Key)

345 Bee
123 Cat
12 Apple

거짓말을하지 않는 "2 차 정렬"이라는 것이 발견되었지만 너무 길을 잃었습니다. .. context.write (key, result); 그러나 비참하게 실패했습니다. 저는 Hadoop을 처음 사용하는데이 문제를 어떻게 해결할 수 있는지 잘 모르겠습니다. 모든 추천을 부탁드립니다. 어떤 기능을 변경해야합니까? 또는 어떤 클래스를 수정해야합니까?

여기 내 수업이 있습니다.

package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

  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 {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        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);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length < 2) {
      System.err.println("Usage: wordcount <in> [<in>...] <out>");
      System.exit(2);
    }
    Job job = new Job(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    for (int i = 0; i < otherArgs.length - 1; ++i) {
      FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
    }
    FileOutputFormat.setOutputPath(job,
      new Path(otherArgs[otherArgs.length - 1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

해결법

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

    1.단어 개수를 올바르게 할 수있었습니다.

    단어 개수를 올바르게 할 수있었습니다.

    내림차순 정렬 및 키 값 교환의 두 번째 요구 사항을 수행하려면 두 번째 맵 전용 작업이 필요합니다.

  2. from https://stackoverflow.com/questions/28785337/how-to-re-arrange-wordcount-hadoop-output-result-and-sort-them-by-value by cc-by-sa and MIT license