Saturday, 25 November 2017

Male - Female ANALYSIS in Election Example Mapreduce


import java.io.IOException;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
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.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Counters;



  
public class Election
{
 
  public static class EleMapper extends Mapper 
  {
   //static enum EleCounters { MALE,FEMALE };
   public void map(LongWritable key,Text value,Context context) throws     IOException,InterruptedException
   {

   String fields[] = value.toString().split(",");
   String gender = fields[3];
   // String party = fields[4];

     context.write(new Text(fields[2]),new Text(fields[3]));

    /*if(gender.length()!=0)
    {
     context.getCounter(EleCounters.MALE).increment(1);
    }
    else if(!(Character.isDigit(sales.charAt(0))))
    {
     context.getCounter(EleCounters.FEMALE).increment(1);
    }
    else
    {
     context.write(new Text(country),new Text(sales));
    }*/
   }
  }
   public static class EleReduce extends Reducer 
  {
       int mc=0;
 
     int fc = 0;
       public void reduce(Text key, Iterable values, Context context) throws     IOException, InterruptedException
        {
      
      
      for (Text value : values)
      {
       
       String gender = value.toString();
      
       if(gender.equals("female"))
       {
        mc++;
       }
       else
       {
        fc++;
       }
       
      }
      
      String str = "Male Count = "+mc+"   Female Count = "+fc;
      
                                  Text str1 = new Text(str);
      context.write(key,str1);
       }
  }
  
  
  public static void main(String args[]) throws Exception
  {
     Path input = new Path(args[0]);
        Path output = new Path(args[1]);

     Configuration conf=new Configuration();
  Job job =new Job(conf);
  job.setJobName("Election");
  job.setJar("Election.jar");
  job.setJarByClass(Election.class);
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(Text.class);
  job.setMapperClass(EleMapper.class);
  job.setReducerClass(EleReduce.class);
  job.setInputFormatClass(TextInputFormat.class);
  job.setOutputFormatClass(TextOutputFormat.class);
  job.setNumReduceTasks(1);
  FileInputFormat.setInputPaths(job,input);
  FileOutputFormat.setOutputPath(job,output);
  
  System.exit(job.waitForCompletion(true)?0:1);

  }
}

Output :

administrator@dev:/usr/local/hadoop/bin$ hadoop fs -cat /dev/part-r-00000
17/09/08 12:04:20 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
krishna          Male Count = 1   Female Count = 0
raghavi           Male Count = 2   Female Count = 1
nitu     Male Count = 3   Female Count = 1
jay       Male Count = 4   Female Count = 1
ajay     Male Count = 5   Female Count = 1
shah    Male Count = 5   Female Count = 2
shah    Male Count = 5   Female Count = 3
sharma           Male Count = 5   Female Count = 4


0 comments:

Post a Comment