`
sunasheng
  • 浏览: 122434 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

mapreduce排序中java.io.IOException: Illegal partition for 2 (-1)

阅读更多
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;
}

}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics