Performing Transformations Using MapReduce (Map Only)
we also offer , online and classroom trainings
we support in POC
author: Bharat (sree ram)
contact : 04042026071
__________________________________________________________________
input file : emp.txt_____________________
101,amar,m,20000,hyd
102,amala,f,30000,pune
103,siva,m,40000,hyd
104,sivani,f,50000,hyd
105,hari,m,40000,pune
____________________
IN input file 3rd field is sex, and 4th field is Salary.
I want to transform "m" as "Male"
and Salary into grades as A,B,C with following criteria.
if salary is < 30000 ---> C
if salary is >=30000 and <50000 ------>B
is salary is >=50000 ------------------>A
in this case also we dont want Reducer... output expected:
101,amar,Male,C,hyd
102,amala,Female,B,pune
103,siva,Male,B,hyd
104,sivani,Female,A,hyd
105,hari,Male,B,pune
package my.map.red;
102,amala,Female,B,pune
103,siva,Male,B,hyd
104,sivani,Female,A,hyd
105,hari,Male,B,pune
package my.map.red;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class Transformation{
public static class Map1 extends Mapper<LongWritable,Text,Text,Text> {
public void map(LongWritable k, Text v, Context con)
throws IOException, InterruptedException{
throws IOException, InterruptedException{
String line=v.toString();
String[] words=line.split(",");
String sex=words[2];
int sal=Integer.parseInt(words[3]);
String grade=new String();
if (sal>=50000)
grade="A";
else if(sal>=30000)
grade="B";
else
grade="C";
if(sex.matches("f"))
sex="Female";
else
sex="Male";
String sex=words[2];
int sal=Integer.parseInt(words[3]);
String grade=new String();
if (sal>=50000)
grade="A";
else if(sal>=30000)
grade="B";
else
grade="C";
if(sex.matches("f"))
sex="Female";
else
sex="Male";
String newline=words[0]+","+words[1]+","+sex+","+grade+words[4];
con.write(new Text(newline), new Text());
}
}
public static void main(String[] args) throws Exception {
Configuration c=new Configuration();
String[] files=new GenericOptionsParser(c,args).getRemainingArgs();
Path p1=new Path(files[0]);
Path p2=new Path(files[1]);
Job j = new Job(c,"trans");
j.setJarByClass(Transformation.class);
j.setMapperClass(Map1.class);
j.setNumReduceTasks(0);
j.setOutputKeyClass(Text.class);
j.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(j,p1);
FileOutputFormat.setOutputPath(j, p2);
FileOutputFormat.setOutputPath(j, p2);
System.exit(j.waitForCompletion(true) ? 0:1);
}
}
0 comments: