[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 EndReason { |
---|
| 16 | |
---|
| 17 | private static int[] domains = {-1, 0, 1, 1000, 2000, 3000, 4000, 5000, 6000, 7000}; |
---|
| 18 | private static String[] reasonName = {"not reported", "undetermined", "infrastructure", "hardware", "IO", "network", "software", "human error", "user"}; |
---|
| 19 | |
---|
| 20 | public static class Map extends Mapper<Object, Text, IntWritable, IntWritable> { |
---|
| 21 | |
---|
| 22 | private static String null_reason = "NULL"; |
---|
| 23 | |
---|
| 24 | private final static IntWritable one = new IntWritable(1); |
---|
| 25 | private final static IntWritable zero = new IntWritable(0); |
---|
| 26 | private final static int fieldNo = 9; |
---|
| 27 | |
---|
| 28 | private int findDomainCode(int reasonCode) { |
---|
| 29 | for (int domainCode = 0; domainCode < domains.length - 1; domainCode++) { |
---|
| 30 | if (domains[domainCode] <= reasonCode && reasonCode < domains[domainCode + 1]) |
---|
| 31 | return domainCode; |
---|
| 32 | } |
---|
| 33 | return 0; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | private int extractReasonDomainCode(String line) { |
---|
| 37 | String[] pieces = line.split("\\s+"); |
---|
| 38 | if (pieces.length < fieldNo + 1) { |
---|
| 39 | return 0; |
---|
| 40 | } |
---|
| 41 | String reason = pieces[fieldNo]; |
---|
| 42 | if (reason.equals(null_reason)) { |
---|
| 43 | return 0; |
---|
| 44 | } else { |
---|
| 45 | int reasonCode = Integer.parseInt(reason); |
---|
| 46 | return findDomainCode(reasonCode); |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | public void map(Object key, Text value, Context context) throws IOException, InterruptedException { |
---|
| 51 | String line = value.toString(); |
---|
| 52 | try { |
---|
| 53 | int reasonDomain = extractReasonDomainCode(line); |
---|
| 54 | context.write(new IntWritable(reasonDomain), one); |
---|
| 55 | } catch (NumberFormatException ex) { |
---|
| 56 | context.write(zero, zero); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public static class Reduce extends Reducer <IntWritable, IntWritable, Text, IntWritable> { |
---|
| 63 | |
---|
| 64 | private Text reasonDomain = new Text(); |
---|
| 65 | |
---|
| 66 | public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { |
---|
| 67 | int sum = 0; |
---|
| 68 | for (IntWritable cnt : values) { |
---|
| 69 | sum += cnt.get(); |
---|
| 70 | } |
---|
| 71 | reasonDomain.set(reasonName[key.get()]); |
---|
| 72 | context.write(reasonDomain, new IntWritable(sum)); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public static void main(String[] args) throws Exception { |
---|
| 77 | Configuration conf = new Configuration(); |
---|
| 78 | Job job = new Job(conf, "EndReason"); |
---|
| 79 | job.setNumReduceTasks(5); |
---|
| 80 | job.setJarByClass(EndReason.class); |
---|
| 81 | |
---|
| 82 | /* set map output key/value classes */ |
---|
| 83 | job.setMapOutputKeyClass(IntWritable.class); |
---|
| 84 | job.setMapOutputValueClass(IntWritable.class); |
---|
| 85 | |
---|
| 86 | /* set application output key/value classes */ |
---|
| 87 | job.setOutputKeyClass(Text.class); |
---|
| 88 | job.setOutputValueClass(IntWritable.class); |
---|
| 89 | |
---|
| 90 | job.setMapperClass(Map.class); |
---|
| 91 | job.setReducerClass(Reduce.class); |
---|
| 92 | /* don't set any combiner class */ |
---|
| 93 | |
---|
| 94 | FileInputFormat.addInputPath(job, new Path(args[0])); |
---|
| 95 | FileOutputFormat.setOutputPath(job, new Path(args[1])); |
---|
| 96 | System.exit(job.waitForCompletion(true) ? 0 : 1); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | } |
---|