我正在使用 Hadoop MapReduce 计算每年的最小值和最大值,但是当我运行该程序时,出现错误:FAILED Error: java.lang.ArrayIndexOutOfBoundsException: 5我认为这是因为我的数据中有空值,因为当没有空值时程序运行正常。因此,在我的 map 函数中,我写了 if 语句来检查是否有 header 以及是否有 null 值: public static class ExposureMapper extends Mapper<Object, Text, Text, MinMaxExposure> { private Text year = new Text(); private double minexposure; private Double maxexposure; private MinMaxExposure outPut = new MinMaxExposure(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { try { //Some condition satisfying it is header if (value.toString().contains("Product")) { return; } else if(value.toString()==null) { return; } else{ } } catch (Exception e) { e.printStackTrace(); } String[] solarFields = value.toString().split(","); year.set(solarFields[2]); minexposure = Double.parseDouble(solarFields[5]); maxexposure = Double.parseDouble(solarFields[5]); try { outPut.setMinExposure(minexposure); outPut.setMaxExposure(maxexposure); context.write(year, outPut); } catch (IOException e) { e.printStackTrace(); } }但是同样的错误发生了......是不是因为value.toString()==null检查空值的方法不正确?
1 回答

胡子哥哥
TA贡献1825条经验 获得超6个赞
如果value.toString().split(",");has 少于六个元素,solarFields[5]则不会是一个元素,因此您会看到一个ArrayIndexOutOfBoundsException.
创建后solarFields你应该立即检查它的长度:
if (solarFields == null || solarFields.length < 6) {
return;
}
您还想确保Double.parseDouble(solarFields[5]);不会抛出NumberFormatException:
Double exposure;
try {
exposure = Double.parseDouble(solarFields[5]);
} catch (NumberFormatException e) {
return;
}
添加回答
举报
0/150
提交
取消