복붙노트

[HADOOP] mapreduce 카운트 예제

HADOOP

mapreduce 카운트 예제

내 질문에 대한 Java에서 mapreduce 프로그래밍입니다.

WordCount.java 예제 (표준 mapreduce 프로그램)가 있다고 가정합니다. map 함수가 정보를 수집하고 reduce 함수 맵으로 돌아가려면 ,

그래서 어떤 노예 노드가 어떤 데이터를 수집했는지 알 수 있습니다.

public class WordCount {

    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
      private final static IntWritable one = new IntWritable(1);
      private Text word = new Text();

      public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
          word.set(tokenizer.nextToken());
          output.collect(word, one);
        }
      }
    }

    public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
      public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
        int sum = 0;
        while (values.hasNext()) {
          sum += values.next().get();
        }
        output.collect(key, new IntWritable(sum));
      }
    }

    public static void main(String[] args) throws Exception {
      JobConf conf = new JobConf(WordCount.class);
      conf.setJobName("wordcount");

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

      conf.setMapperClass(Map.class);
      conf.setCombinerClass(Reduce.class);
      conf.setReducerClass(Reduce.class);

      conf.setInputFormat(TextInputFormat.class);
      conf.setOutputFormat(TextOutputFormat.class);

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

      JobClient.runJob(conf);
    }
}

고맙습니다!!

해결법

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

    1.당신이 요구하는 것은 그것이 실행 된 인프라에 대해 애플리케이션 (지도 축소)에 알리는 것입니다.

    당신이 요구하는 것은 그것이 실행 된 인프라에 대해 애플리케이션 (지도 축소)에 알리는 것입니다.

    일반적으로 응용 프로그램에는이 정보가 필요하지 않습니다. Mapper에 대한 각 호출과 Reducer에 대한 각 호출은 동일한 노드에서 다른 노드 또는 모두에서 실행될 수 있습니다. MapReduce의 장점은 결과가 동일하므로 애플리케이션에 중요합니다.

    결과적으로 API에는이 요청을 지원하는 기능이 없습니다.

    하둡 배우기

    추신 내가 생각할 수있는 유일한 방법은 (적어도 말하기에 더러운 점이다) Mapper에 어떤 종류의 시스템 호출을 포함시키고 그 밑의 OS에 이름 / 속성 / etc를 묻는 것이다. 이런 종류의 구조는 응용 프로그램을 매우 이식 불가능하게 만듭니다. 즉 Windows 또는 Amazon의 Hadoop에서는 실행되지 않습니다.

  2. ==============================

    2.단어 수는 잘못된 예입니다. 모든 정보를 병합하기 만하면됩니다. 이것은 단어 수를 반전시킵니다.

    단어 수는 잘못된 예입니다. 모든 정보를 병합하기 만하면됩니다. 이것은 단어 수를 반전시킵니다.

    기본적으로 당신은 당신의 slaveNode_id를 IntWritable (가능하다면)과 정보를 Text로 출력하고 있습니다.

      public static class Map extends MapReduceBase implements Mapper<LongWritable, Text,IntWritable, Text> {
        private Text word = new Text();
    
      public void map(LongWritable key, Text value, OutputCollector<IntWritable, Text> output, Reporter reporter) throws IOException {
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
          word.set(tokenizer.nextToken());
          // you have to split your data here: ID and value
          IntWritable id = new IntWritable(YOUR_ID_HERE);
    
          output.collect(id, word);
        }
      }
    }
    

    감속기도 같은 방식으로 작동합니다.

     public static class Reduce extends MapReduceBase implements Reducer<IntWritable, Text,IntWritable, Text> {
      public void reduce(IntWritable key, Iterator<Text> values, OutputCollector<IntWritable,Text> output, Reporter reporter) throws IOException {
    
          // now you have all the values for a slaveID as key. Do whatever you like with that...
          for(Text value : values)
             output.collect(key, value)
      }
    }
    
  3. from https://stackoverflow.com/questions/6164230/mapreduce-count-example by cc-by-sa and MIT license