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