简体   繁体   English

自定义SortComparator在MapReduce wordcount程序中不起作用

[英]Custom SortComparator not working in MapReduce wordcount program

I am trying to understand how MapReduce Sorts the Map output keys and what is the sort algorithm which it uses. 我正在尝试了解MapReduce如何对Map输出键进行排序以及它使用的排序算法是什么。 I have a text file like this 我有一个这样的文本文件

a b e f c b

how it performs the sorting with these keys. 它如何使用这些键执行排序。 I implemented a custom SortComparator class extending WritableComparator interface. 我实现了扩展WritableComparator接口的自定义SortComparator类。 I wanted to see how sorting is happening so I am writing the operations into a file. 我想看看排序是如何发生的,所以我将操作写入文件中。

public static class MySortComparator2 extends WritableComparator{

  @Override
  public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {

      Text x=new Text("");
      Text y=new Text("");
      try {
        x.readFields(new DataInputStream(new ByteArrayInputStream(b1)));
        y.readFields(new DataInputStream(new ByteArrayInputStream(b2)));

        FileWriter writerOfComparisions=new FileWriter("/home/srimanth/Comparisons",true);
        writerOfComparisions.write("Comparing "+x.toString()+" with "+y.toString());
        writerOfComparisions.write(" ----> returning "+x.compareTo(y));
        writerOfComparisions.write("\n");
        writerOfComparisions.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
      return x.compareTo(y);
  }

the output of the Comparisons file is this 比较文件的输出是这个

Comparing a with a ----> returning 0
Comparing a with a ----> returning 0
Comparing a with a ----> returning 0
Comparing a with a ----> returning 0
Comparing a with a ----> returning 0
Comparing b with a ----> returning 1
Comparing c with a ----> returning 2
Comparing f with a ----> returning 5
Comparing e with a ----> returning 4
Comparing b with a ----> returning 1
Comparing b with c ----> returning -1
Comparing c with f ----> returning -3
Comparing f with e ----> returning 1
Comparing e with b ----> returning 3
Comparing b with a ----> returning 1

What is this sorting algorithm and my final output of wordcount is this 这是什么排序算法,我的wordcount最终输出是这个

b   1
c   1
f   1
e   1
b   1
a   1

Looks like the mapreduce did not happen properly. 看起来mapreduce发生不正确。 Here is my Mapper and Reducer classes. 这是我的Mapper和Reducer类。

  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);
    }
  }

What am I doing wrong. 我究竟做错了什么。 I would appreciate some help. 我将不胜感激。 Thanks in Advance. 提前致谢。

Did you set the following the driver code? 您是否设置了以下驱动程序代码?

job.setSortComparatorClass(MySortComparator2.class); job.setSortComparatorClass(MySortComparator2.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM