MapReduce Character Count
Mapper Logic :
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class CharCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
public void map(LongWritable key, Text value, Context con)
throws IOException, InterruptedException {
String line = value.toString();
char[] chars = line.toCharArray();
for(char c:chars) {
con.write(new Text("Total Characters"), new IntWritable(1));
}
}
}
Reducer Logic :
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class CharCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
/*
* bigdata <1>
*/
@Override
public void reduce(Text key, Iterable<IntWritable> values,Context con)
throws IOException, InterruptedException {
int sum = 0;
for(IntWritable i:values) {
sum = sum + i.get();
}
con.write(key, new IntWritable(sum));
}
}
Driver Logic :
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class CharCountDriver {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: WordCount <input path> <output path>");
System.exit(-1);
}
@SuppressWarnings("deprecation")
Job job = new Job();
job.setJobName("Char Count");
job.setJarByClass(CharCountDriver.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(CharCountMapper.class);
job.setReducerClass(CharCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
Comments
Post a Comment