错误:java.lang.NumberFormatException:空字符串map-reduce代码

时间:2017-06-26 10:31:52

标签: java numberformatexception

这是我的mapper类代码:

package airlinedataanalysis;

import java.io.IOException;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;

public class AirlineMapperClass extends Mapper<LongWritable, Text, Text, FloatWritable>
{
     public void map(LongWritable key, Text PassengerDetails, Context con)throws IOException, InterruptedException
     {
              String[] detail = PassengerDetails.toString().split(",");
              Integer survived=Integer.parseInt(detail[1]);
              if(survived==1)
              {
                  String sex = detail[4];

                  if(detail[5]==null)
                  { 
                      con.write(new Text(sex), new FloatWritable((float) 0));
                  }         
                  else
                  {   
                    Float age=Float.parseFloat(detail[5]);
                    con.write(new Text(sex), new FloatWritable(age));

                  }



              }

     }

}

这是错误

error

当数据输入集中没有空值时,我的代码工作正常。 但是当输入集中包含空值时,它不起作用。

数据输入文件如下所示:

1,0,3,"Braund Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S,
2,1,1,"Cumings Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C,
3,1,3,"Heikkinen Miss. Laina",female,26,0,0,STON/O2. 3101282,7.925,,S,
4,1,1,"Futrelle Mrs. Jacques Heath (Lily May Peel)",female,35,1,0,113803,53.1,C123,S,
5,0,3,"Allen Mr. William Henry",male,35,0,0,373450,8.05,,S,
6,0,3,"Moran Mr. James",male,,0,0,330877,8.4583,,Q,
7,0,1,"McCarthy Mr. Timothy J",male,54,0,0,17463,51.8625,E46,S,
8,0,3,"Palsson Master. Gosta Leonard",male,2,3,1,349909,21.075,,S,
9,1,3,"Johnson Mrs. Oscar W (Elisabeth Vilhelmina Berg)",female,27,0,2,347742,11.1333,,S,
10,1,2,"Nasser Mrs. Nicholas (Adele Achem)",female,14,1,0,237736,30.0708,,C,
11,1,3,"Sandstrom Miss. Marguerite Rut",female,4,1,1,PP 9549,16.7,G6,S,

我想要的输出:

female  Total Died: 9 :: Average Age:30.222221
male    Total Died: 2 :: Average Age:31.0

我的reducer类代码:

package airlinedataanalysis;

import java.io.IOException;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Reducer;

public class AirlineReducer extends Reducer<Text, FloatWritable, Text, Text> 
{
    public void reduce(Text key, Iterable<FloatWritable> value,Context con) throws IOException, InterruptedException
    {
               Float totalage =(float) 0;
               int count = 0;
               for (FloatWritable var : value) 
               {
                totalage += var.get();
                count++;
               }
               float avg =(totalage / count);
               String out = "Total Died: " + count + " :: " + "Average Age:" + avg;
               con.write(key, new Text(out));
    }          
}

1 个答案:

答案 0 :(得分:0)

您可以在mapper类中处理空String / Null条件,如下所示:

public void map(Object key, Text PassengerDetails, Context con)
            throws IOException, InterruptedException {

        String[] detail = PassengerDetails.toString().split(",");

        if (!detail[1].isEmpty() && !detail[1].equals(null)) {

            Integer survived = Integer.parseInt(detail[1]);

            if (survived != null && survived == 1) {

                String sex = "Undefined";

                if (!detail[4].equals(null)) {
                    sex = detail[4];
                }

                if (detail[5].isEmpty() || detail[5] == null) {
                    con.write(new Text(sex), new FloatWritable((int) 0));
                } else {
                    float age = Float.parseFloat(detail[5]);
                    con.write(new Text(sex), new FloatWritable(age));

                }

            }
        }
    }

它会工作。