Skip to content

Instantly share code, notes, and snippets.

@Vatsal596
Created September 1, 2023 13:38
Show Gist options
  • Save Vatsal596/becf65af3eaf079bea6a24d39e5092b2 to your computer and use it in GitHub Desktop.
Save Vatsal596/becf65af3eaf079bea6a24d39e5092b2 to your computer and use it in GitHub Desktop.
package count;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.Reducer;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class count {
public static class mapper extends Mapper<LongWritable,Text , Text, IntWritable> {
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
String field[]=value.toString().split(",");
String gender=field[2].trim();
if(gender.equalsIgnoreCase("female")) {
context.write(new Text("female count"),new IntWritable(1));
}
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>{
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,InterruptedException{
int count=0;
for(IntWritable i:values) {
count+=i.get();
}
context.write(key, new IntWritable(count));
}
}
public static void main(String args[]) throws Exception{
Configuration conf=new Configuration();
Job job=Job.getInstance(conf,"condition");
job.setJobName("condition");
job.setJar("condition.jar");
job.setJarByClass(count.class);
job.setMapperClass(mapper.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setNumReduceTasks(1);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true)?0:1);
}
}
@DevAbolfazl
Copy link

Hi. It`s good. Bravo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment