数组outof bound Mapreduce

时间:2015-12-05 17:47:14

标签: java sorting hadoop

我试图使用mapreduce运行排序算法,但总是从语音中接收到数组

异常

java.lang.ArrayIndexOutOfBoundsException: 6
at to.SecSortMapper.map(SecSortMapper.java:17)
at to.SecSortMapper.map(SecSortMapper.java:1)

代码

if (value.toString().length() > 0) {
    String arrEmpAttributes[] = value.toString().split("\\t");
    context.write(new TextPair(arrEmpAttributes[6].toString(),
            (arrEmpAttributes[3].toString() + "\t" + arrEmpAttributes[2].toString() + "\t" + arrEmpAttributes[0].toString())), nullWritable.get());

}
  

第17行是新的TextPair(.............)

我试图更改值字符串长度> 1但它没有用,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您假设arrEmpAttributes包含至少7个元素,其中没有一个是null。这是一个冒险的假设,特别是在大数据世界中,您几乎可以保证处理输入数据中的某些不一致。

您的if语句应如下所示:

valueStr = value.toString(); //Declare this outside of the loop for efficiency
if (!StringUtils.isEmpty(valueStr)) {
    arrEmpAttributes[] = valueStr.split(TAB_SEPARATOR); //Also declare these two outside of the loop
    if(!ArrayUtils.isEmpty(arrEmpAttributes) && arrEmpAttributes.length==SEVEN) {
    //context.write...
    }
}
相关问题