Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Average of Integer: Driver

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

AVERAGE OF INTEGER:

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.*;

public class AvgInt{


public static void main(String args[]) throws Exception
{
Configuration conf=new Configuration();
Job job=Job.getInstance(conf,"xyz");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(AvgIntMapper.class);
job.setReducerClass(AvgIntReducer.class);
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
job.setJarByClass(AvgInt.class);
job.waitForCompletion(true);
}
}

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:

You might also like