ICP_2 - maturivinay/BigData_Programming-hadoop-Spark- GitHub Wiki

Welcome to the BigData_Programming-hadoop-Spark- wiki!

Word Count for counting the number of even and odd numbers..

Logic In Reducer

############################################################################################################## package com.wordcount;

import java.io.IOException; import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; 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.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {

public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(Object key, Text value, Context context
                ) throws IdOException, InterruptedException {
  StringTokenizer itr = new StringTokenizer(value.toString());
  while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    context.write(word, one);
   
    
  }
}

}

public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public int sum_odd=0; public int sum_even=0; public void reduce(Text key, Iterable values, Context context ) throws IOException, InterruptedException {

  int value=Integer.parseInt(key);
  if(value%2==0)
      sum_even=sum_even+1;
  
  else
      sum_odd=sum_odd+1;
  IntWritable value1 = new IntWritable(sum_even);
  IntWritable value2 = new IntWritable(sum_odd);
  key.set("odd")
  context.write(key,value2);
  key.set("even")
  context.write(key,value1);
}

}

public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }

I have made an logic in the reducer where the even or odd is found out and the respective count will be icreased and finally the odd and even output will be displayed according to the count.

Logic in Mapper....

public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {

  StringTokenizer itr = new StringTokenizer(value.toString());
  while (itr.hasMoreTokens()) {
    int num = Integer.parseInt(itr.nextToken());
    //int ec=0,oc=0;
    if (num % 2 == 0) {
      //ec=ec+1;
      word.set("even");
      context.write(word, one);
    } else {
      //oc=oc+1;
      word.set("odd");
      context.write(word, one);
    }
  }

}

But this is the one where i dint get an valid output. But the logic is correct for mapper logic.