About

Edit photo

Sunday, March 6, 2016

Vowel and Consonant map reduce program in Hadoop



The following program is to finds number of vowels and Consonants in the given input file, for example, 

my input file is "vandc.txt" Contains::

Hello there!
i love you.

it contains 1 vowel ("i") and 4 consonants ("Hello, There!, love, you."), So the output would be vowels 1
consonants 4

How to execute the program, please click here.

package tut.ssaik.com.vandc;

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;
import org.apache.hadoop.util.GenericOptionsParser;

public class vowel {
 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 {
   String line = value.toString();

   String[] letters = line.split(" ");

   for (String letter : letters) {
    if (isVowel(letter))
     context.write(new Text("Vowel"), new IntWritable(1));
    else
     context.write(new Text("Consonant"), new IntWritable(1));
   }
  }

  public boolean isVowel(String line) {
   char wr = line.charAt(0);
   if (wr == 'a' || wr == 'e' || wr == 'i' || wr == 'o' || wr == 'u') {
    return true;
   } else {
    return false;
   }
  }
 }

 public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  private IntWritable result = new IntWritable();

  public void reduce(Text key, Iterable<IntWritable> values, Context context)
    throws IOException, InterruptedException {
   int sum = 0;
   for (IntWritable val : values) {
    sum += val.get();
   }
   result.set(sum);
   context.write(key, result);
  }
 }

 public static void main(String[] args) throws Exception {
  Configuration conf = new Configuration();
  String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  if (otherArgs.length != 2) {
   System.err.println("Usage: WordCount <in> <out>");
   System.exit(2);
  }
  Job job = new Job(conf, "word count");
  job.setJarByClass(vowel.class);
  job.setMapperClass(TokenizerMapper.class);
  job.setCombinerClass(IntSumReducer.class);
  // filesystem fs = filesystem.get(conf);
  job.setReducerClass(IntSumReducer.class);
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(IntWritable.class);
  FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  System.exit(job.waitForCompletion(true) ? 0 : 1);
  // if(fs.exists(outputDir))
  // fs.delete(outputDir,true);

 }
}


**Comment below for encourage.**
post by SsaiK

7 comments:

  1. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    selenium training in bangalore|
    python training in bangalore|

    ReplyDelete
  2. I‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.
    hadoop training in bangalore

    ReplyDelete
  3. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you."Devops Training in Chennai"

    ReplyDelete
  4. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. Besant technologies Marathahalli

    ReplyDelete
  5. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. Besant technologies Marathahalli

    ReplyDelete
  6. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
    sas training in bangalore

    ReplyDelete
  7. It’s always so sweet and also full of a lot of fun for me personally and my office colleagues to search your blog a minimum of thrice in a week to see the new guidance you have got.
    big data training in Chennai

    ReplyDelete