Average of Integer: Driver
Average of Integer: Driver
Average of Integer: Driver
DRIVER:
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.io.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import java.io.*;
import java.util.*;
MAPPER:
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.io.*;
import java.io.*;
public class AvgIntMapper extends
Mapper<LongWritable,Text,Text,Text>{
public void map(LongWritable key,Text value,Context context)
throws IOException,InterruptedException
{
int i=0,sum=0,j;
String line=value.toString();
String[] wordsinline=line.split(" ");
for(i=0;i<wordsinline.length;i++)
{
j=Integer.parseInt(wordsinline[i]);
sum+=j;
}
Text dum=new Text(""+sum+" "+i+" ");
context.write(new Text("Avg ="),dum);
}
}
REDUCER:
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.io.*;
import java.io.*;
import java.util.*;
public class AvgIntReducer extends
Reducer<Text,Text,Text,DoubleWritable>{
DoubleWritable Avg=new DoubleWritable();
public void reduce(Text key,Iterable<Text>values,Context
context)throws IOException,InterruptedException {
int a,v,sum=0,j=0,c=0;
double avg=0.0;
for(Text txt:values)
{
String line=txt.toString();
String[] wordsinline=line.split(" ");
for(int i=0;i<wordsinline.length;i++)
{
a=Integer.parseInt(wordsinline[0]);
v=Integer.parseInt(wordsinline[1]);
sum+=a;
c+=v;
}
}
avg=sum/c;
Avg.set(avg);
context.write(key,Avg);
}
}
INPUT :
10 20 30
-10 40
OUTPUT: