0

Here is my mapper class code:

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));

                  }



              }

     }

}

Here is the error

error

My code works fine when data input sets do not have empty values in it. But it does not work when input sets have empty values in it .

The data input files look like:

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,

The desired output that I would like:

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

My reducer class code:

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));
    }          
}
Nuageux
  • 1,644
  • 1
  • 13
  • 24
Sanjeev Jangra
  • 1
  • 1
  • 1
  • 3
  • Well, for starters, your second line will evaluate to `survived`, and `detail[5]`(38) is not a `Float`. You are going to have many issues. – jiveturkey Jun 26 '17 at 19:45
  • But it works fine for non empty values – Sanjeev Jangra Jun 27 '17 at 01:25
  • It's telling you right where it is, `java.lang.Float.parseFloat(Float.java:452)`. Go check out line 452. – jiveturkey Jun 27 '17 at 13:11
  • If i knew how to handle this error by myself then i wouldn't had asked for help – Sanjeev Jangra Jun 27 '17 at 13:48
  • Have a look at this Accepted answer. https://stackoverflow.com/questions/31642352/how-do-i-read-input-that-could-be-an-int-or-a-double. To be able to take in either Floats or integrals, you may be pulling them in as Doubles. – jiveturkey Jun 27 '17 at 14:06
  • Okay i am gonna parse it as double but i don't think it will work – Sanjeev Jangra Jun 27 '17 at 14:19
  • Didn't worked mate – Sanjeev Jangra Jun 27 '17 at 15:52
  • Sir the desired output came by running my program but at that time i took only those inputs in which age value is not missing and the code works fine therefore i don't think that there is any problem in parsing values. The problem arises in the case when input file contains details in which age is not given for few passengers. And age is given after sex in input file – Sanjeev Jangra Jun 27 '17 at 16:03

1 Answers1

0

You can handle the empty String/Null conditions in your mapper class like below:

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));

                }

            }
        }
    }

It'll work.

Neeraj
  • 1