mapreduce排序中java.io.IOException: Illegal partition for 2 (-1) hadoopmapreducepartitionIOException
程序员文章站
2022-05-25 09:40:14
...
mapreduce程序,自定义了Partition,运行时出现以下异常:
java.io.IOException: Illegal partition for 2 (-1)
自定义Partition代码:
public static class Partition extends Partitioner<IntWritable,IntWritable> { @Override public int getPartition(IntWritable key, IntWritable value, int numPartitions) { int Maxnumber = 65223; int bound = Maxnumber/numPartitions+1; int keynumber = key.get(); for (int i = 0; i < numPartitions; i++) { if(keynumber<bound*i&&keynumber>=bound*(i-1)) { return i-1; } } return -1; } }
原因:
出现错误的原因是在某种情况下getPartition函数返回了-1,Partition是从0开始的,默认返回-1是不正确的。
解决方法:
默认返回0
public static class Partition extends Partitioner<IntWritable,IntWritable> { @Override public int getPartition(IntWritable key, IntWritable value, int numPartitions) { int Maxnumber = 65223; int bound = Maxnumber/numPartitions+1; int keynumber = key.get(); for (int i = 0; i < numPartitions; i++) { if(keynumber<bound*i&&keynumber>=bound*(i-1)) { return i-1; } } return 0; } }