【问题标题】:ArrayIndexOutOfBoundException in Reducer function of mapreducemapreduce 的 Reducer 函数中的 ArrayIndexOutOfBoundException
【发布时间】:2018-10-11 15:44:27
【问题描述】:

当我删除了 job.setSortComparatorClass(LongWritable.DecreasingComparator.class);

我得到了输出,但是当我尝试使用它时,我得到了这个异常。

我试图根据值从减速器中按降序获取输出,因此我使用了setsortcomparator 类,所以请帮帮我

package topten.mostviewed.movies;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class MostViewdReducer extends Reducer<Text,IntWritable,Text,LongWritable>
{
    public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException
    {
        int sum = 0;
        for(IntWritable value:values)
        {
            sum = sum+1;
        }
        context.write(key, new LongWritable(sum));
    }
}
package topten.mostviewed.movies;

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.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.RawComparator;
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 MostViewdDriver 
{

   // @SuppressWarnings("unchecked")
    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: movie <input> <out>");
            System.exit(2);
        }
    Job job = new Job(conf, "Movie ");
    job.setJarByClass(MostViewdDriver.class);
    job.setMapperClass(MostviewdMapper.class);
    job.setReducerClass(MostViewdReducer.class);

    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(LongWritable.class);
    job.setSortComparatorClass(LongWritable.DecreasingComparator.class);
//  job.setSortComparatorClass((Class<? extends RawComparator>) LongWritable.class);

    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

我得到的异常如下:

18/10/11 11:35:05 信息 mapreduce.Job:任务 ID:尝试_1539236679371_0004_r_000000_2,状态:失败 错误:java.lang.ArrayIndexOutOfBoundsException:7 在 org.apache.hadoop.io.WritableComparator.readInt(WritableComparator.java:212) 在 org.apache.hadoop.io.WritableComparator.readLong(WritableComparator.java:226) 在 org.apache.hadoop.io.LongWritable$Comparator.compare(LongWritable.java:91) 在 org.apache.hadoop.io.LongWritable$DecreasingComparator.compare(LongWritable.java:106) 在 org.apache.hadoop.mapreduce.task.ReduceContextImpl.nextKeyValue(ReduceContextImpl.java:158) 在 org.apache.hadoop.mapreduce.task.ReduceContextImpl.nextKey(ReduceContextImpl.java:121) 在 org.apache.hadoop.mapreduce.lib.reduce.WrappedReducer$Context.nextKey(WrappedReducer.java:307) 在 org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:170) 在 org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:627) 在 org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:389) 在 org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:168) 在 java.security.AccessController.doPrivileged(本机方法) 在 javax.security.auth.Subject.doAs(Subject.java:422) 在 org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1642) 在 org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)

【问题讨论】:

    标签: hadoop mapreduce


    【解决方案1】:

    您的地图输出键是整数,但您尝试使用用于长整数的比较器。将LongWritable.DecreasingComparator.class 替换为IntWritable.DecreasingComparator.class

    【讨论】:

    • 听起来很合理。可能 hadoop 只是查看原始数据,当您将 long 和 int 放入数组(字节)中时,您最终可能会得到不同长度的数组,从而导致此类问题。虽然它更像是一个错误。框架应该检查数组长度,并在这种情况下抛出更有意义的消息。
    • 比较器类仅适用于 LongWritable 以及 Reduce 函数的输出
    • 不,比较器类适用于映射输出键,而不是减少输出键。它的工作(与分组比较器一起)是按键对键/值对进行分组,以便每个这样的组都可以输入单个 reduce() 调用。
    • 那么应该使用哪个类对reduce函数的输出进行排序
    • 即使在我将 IntWritable 的东西从 Map 的输出中更改为 LongWritable 之后,它也会给出相同的异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 2018-08-02
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多