

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


import java.io.IOException; 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 LogProcessor { public static class LogLevelMapper extends Mapper { private final static IntWritable one = new IntWritable(1); private Text logLevel = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); // Split line by whitespace String[] parts = line.split("\s+"); if (parts.length >= 3) { String level = parts[2]; // INFO, WARN, ERROR logLevel.set(level); context.write(logLevel, one); }
public static class CountReducer extends Reducer { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable 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(); Job job = Job.getInstance(conf, "Log Level Counter"); job.setJarByClass(LogProcessor.class); job.setMapperClass(LogLevelMapper.class); job.setCombinerClass(CountReducer.class); job.setReducerClass(CountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class);