[154] | 1 | package ro.pub.cs.pp.pdad; |
---|
| 2 | |
---|
| 3 | import java.io.IOException; |
---|
| 4 | |
---|
| 5 | import org.apache.hadoop.fs.Path; |
---|
| 6 | import org.apache.hadoop.conf.Configuration; |
---|
| 7 | import org.apache.hadoop.io.Text; |
---|
| 8 | import org.apache.hadoop.io.IntWritable; |
---|
| 9 | import org.apache.hadoop.mapreduce.Job; |
---|
| 10 | import org.apache.hadoop.mapreduce.Mapper; |
---|
| 11 | import org.apache.hadoop.mapreduce.Reducer; |
---|
| 12 | import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; |
---|
| 13 | import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; |
---|
| 14 | |
---|
| 15 | public class ComponentType { |
---|
| 16 | |
---|
| 17 | public static class Map extends Mapper<Object, Text, IntWritable, IntWritable> { |
---|
| 18 | |
---|
| 19 | private final static IntWritable one = new IntWritable(1); |
---|
| 20 | private final static IntWritable zero = new IntWritable(0); |
---|
| 21 | |
---|
| 22 | private int extractComponentType(String line) { |
---|
| 23 | String[] pieces = line.split("\\s+"); |
---|
| 24 | return Integer.parseInt(pieces[6]); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | public void map(Object key, Text value, Context context) throws IOException, InterruptedException { |
---|
| 28 | String line = value.toString(); |
---|
| 29 | try { |
---|
| 30 | int componentType = extractComponentType(line); |
---|
| 31 | context.write(new IntWritable(componentType), one); |
---|
| 32 | } catch (NumberFormatException ex) { |
---|
| 33 | context.write(zero, zero); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | public static class Reduce extends Reducer <IntWritable, IntWritable, Text, IntWritable> { |
---|
| 40 | |
---|
| 41 | public static String[] compTypeMap = {"host", "CPU", "IO", "network", "memory", "software"}; |
---|
| 42 | private Text compType = new Text(); |
---|
| 43 | |
---|
| 44 | public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { |
---|
| 45 | int sum = 0; |
---|
| 46 | for (IntWritable cnt : values) { |
---|
| 47 | sum += cnt.get(); |
---|
| 48 | } |
---|
| 49 | compType.set(compTypeMap[key.get()]); |
---|
| 50 | context.write(compType, new IntWritable(sum)); |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public static void main(String[] args) throws Exception { |
---|
| 55 | Configuration conf = new Configuration(); |
---|
| 56 | Job job = new Job(conf, "ComponentType"); |
---|
| 57 | job.setNumReduceTasks(5); |
---|
| 58 | job.setJarByClass(ComponentType.class); |
---|
| 59 | job.setOutputKeyClass(IntWritable.class); |
---|
| 60 | job.setOutputValueClass(IntWritable.class); |
---|
| 61 | job.setMapperClass(Map.class); |
---|
| 62 | /* don't set any combiner class */ |
---|
| 63 | job.setReducerClass(Reduce.class); |
---|
| 64 | FileInputFormat.addInputPath(job, new Path(args[0])); |
---|
| 65 | FileOutputFormat.setOutputPath(job, new Path(args[1])); |
---|
| 66 | System.exit(job.waitForCompletion(true) ? 0 : 1); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | } |
---|