- ANKUSH THAVALI
- 20 Dec, 2021
- 0 Comments
- 1 Min Read
Count Frequency of Character in Mapreduce Programme
Mapper Programe
package wordcount; import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class AlphaMapper extends Mapper<Object, Text, Text, LongWritable> { private final static LongWritable one = new LongWritable(1); private Text character = new Text(); @Override public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String v = value.toString(); for (int i = 0; i < v.length(); i++) { character.set(v.substring(i, i + 1)); context.write(character, one); } } }
Reducer Programe
package wordcount; import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class AlphaReducer extends Reducer<Text, LongWritable, Text, LongWritable> { private LongWritable result = new LongWritable(); public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { long sum = 0; for (LongWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } }
Driver Programe
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; 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.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; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; // The driver program for mapreduce job. public class AlphaCounter extends Configured implements Tool { public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(), new AlphaCounter(), args); System.exit(res); } @Override public int run(String[] args) throws Exception { Configuration conf = this.getConf(); // Create job Job job = Job.getInstance(conf, "Alpha Counter Job"); job.setJarByClass(AlphaCounter.class); job.setMapperClass(AlphaMapper.class); job.setReducerClass(AlphaReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); job.setInputFormatClass(TextInputFormat.class); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setOutputFormatClass(TextOutputFormat.class); return job.waitForCompletion(true) ? 0 : 1; } }
Run Mapreduce Programe
hadoop jar count.jar wordcount.AlphaCounter /user/hive/input/wordcount.txt /user/hive/output/wordcount1 14 I 1 a 10 c 2 d 4 e 9 g 6 h 2 i 5 l 3 m 7 n 4 o 8 p 4 r 9 s 3 u 3 w 2 y 2