I was trying to process four lines of a data-set together. I have used a variable lineCount in the mapper for this. But am not getting some part of the outputs correctly.
Here is my mapper class:-
public class GC_Mapper extends Mapper<LongWritable, Text, Text, IntWritable> {
int lineCount = 0;
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
if (lineCount % 4 == 0) {
context.write(new Text("#Reads"), new IntWritable(1));
lineCount++;
return;
}
if (lineCount % 4 == 1) {
context.write(new Text("X"), new IntWritable(1));
lineCount++;
return;
}
if (lineCount % 4 == 2) {
context.write(new Text("Y"), new IntWritable(1));
lineCount++;
return;
}
if (lineCount % 4 == 3) {
context.write(new Text("Z"), new IntWritable(1));
lineCount++;
return;
}
}
}
My Reducer :-
public class GC_Reducer extends
Reducer<Text, IntWritable, Text, DoubleWritable> {
int numReads;
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
if ((key.toString()).startsWith("#")) {
for (IntWritable read : values) {
numReads += read.get();
}
context.write(key, new DoubleWritable(numReads));
}
if ((key.toString().startsWith("X"))) {
double sum1 = 0;
for (IntWritable val : values) {
sum1 += val.get();
}
context.write(key, new DoubleWritable(sum1));
}
if ((key.toString().startsWith("Y"))) {
double sum2 = 0;
for (IntWritable val : values) {
sum2 += val.get();
}
context.write(key, new DoubleWritable(sum2));
}
if ((key.toString().startsWith("Z"))) {
double sum3 = 0;
for (IntWritable val : values) {
sum3 += val.get();
}
context.write(key, new DoubleWritable(sum3));
}
}
}
My intention was to take the number of Reads(provided 4 lines are taken as a single record) and to process four lines differently. But the problem am facing that I got the output as :-
#Reads 50.0
X 100.0
Y 100.0
Z 100.0
But my desired output was 50.0 for all the keys. Only #Reads value is correct. Please help me to find a solution. Thanks in advance !