1 | import java.io.IOException; |
---|
2 | import java.util.*; |
---|
3 | |
---|
4 | import org.apache.pig.EvalFunc; |
---|
5 | import org.apache.pig.FuncSpec; |
---|
6 | import org.apache.pig.data.DataBag; |
---|
7 | import org.apache.pig.data.DefaultBagFactory; |
---|
8 | import org.apache.pig.data.DefaultTupleFactory; |
---|
9 | import org.apache.pig.data.Tuple; |
---|
10 | import org.apache.pig.impl.logicalLayer.schema.Schema; |
---|
11 | import org.apache.pig.data.DataType; |
---|
12 | import org.apache.pig.impl.logicalLayer.FrontendException; |
---|
13 | |
---|
14 | public class RealLabels extends EvalFunc<DataBag> { |
---|
15 | |
---|
16 | public static HashMap<Double, String> failureCause = new HashMap<Double, String>(); |
---|
17 | public static HashMap<Double, String> durationGroup = new HashMap<Double, String>(); |
---|
18 | |
---|
19 | public static void init() { |
---|
20 | // failure cause |
---|
21 | failureCause.put(new Double(-1), "not reported"); |
---|
22 | failureCause.put(new Double(0), "reported as undetermined"); |
---|
23 | failureCause.put(new Double(999), "infrastructure"); |
---|
24 | failureCause.put(new Double(1999), "hardware"); |
---|
25 | failureCause.put(new Double(2999), "IO"); |
---|
26 | failureCause.put(new Double(3999), "network"); |
---|
27 | failureCause.put(new Double(4999), "software"); |
---|
28 | failureCause.put(new Double(5999), "human error"); |
---|
29 | failureCause.put(new Double(6999), "user"); |
---|
30 | failureCause.put(new Double(7000), "end of measurement"); |
---|
31 | |
---|
32 | // group by duration |
---|
33 | durationGroup.put(new Double(1000), "short"); |
---|
34 | durationGroup.put(new Double(100000), "medium"); |
---|
35 | durationGroup.put(Double.MAX_VALUE, "long"); |
---|
36 | } |
---|
37 | |
---|
38 | @Override |
---|
39 | public DataBag exec(Tuple input) throws IOException { |
---|
40 | RealLabels.init(); |
---|
41 | |
---|
42 | if (input == null || input.size() == 0) |
---|
43 | return null; |
---|
44 | try{ |
---|
45 | |
---|
46 | Double duration = (Double)input.get(0); |
---|
47 | //System.out.println("duration="+duration); |
---|
48 | Double failure = new Double(0); |
---|
49 | if(duration != null && failure != null) { |
---|
50 | |
---|
51 | String f = new String(input.get(1).toString()); |
---|
52 | StringTokenizer st = new StringTokenizer(f); |
---|
53 | String token = st.nextToken(); |
---|
54 | if(token.compareTo("NULL") == 0) |
---|
55 | failure = -1.0; |
---|
56 | else |
---|
57 | failure = Double.parseDouble(token); |
---|
58 | |
---|
59 | //System.out.println("failure="+failure); |
---|
60 | String durationClass = "", faultClass = ""; |
---|
61 | |
---|
62 | // get duration class |
---|
63 | LinkedList<Double> ll1 = new LinkedList<Double>( |
---|
64 | RealLabels.durationGroup.keySet()); |
---|
65 | Collections.sort(ll1); |
---|
66 | Iterator iterator1 = ll1.iterator(); |
---|
67 | while (iterator1.hasNext()) { |
---|
68 | double mkey = (Double) iterator1.next(); |
---|
69 | if (duration <= mkey) { |
---|
70 | String mvalue = (String) RealLabels.durationGroup.get(mkey); |
---|
71 | durationClass = mvalue; |
---|
72 | break; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | // get fault class |
---|
77 | LinkedList<Double> ll2 = new LinkedList<Double>( |
---|
78 | RealLabels.failureCause.keySet()); |
---|
79 | Collections.sort(ll2); |
---|
80 | Iterator iterator2 = ll2.iterator(); |
---|
81 | while (iterator2.hasNext()) { |
---|
82 | double mkey = (Double) iterator2.next(); |
---|
83 | if (failure <= mkey) { |
---|
84 | String mvalue = (String) RealLabels.failureCause.get(mkey); |
---|
85 | faultClass = mvalue; |
---|
86 | break; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | if(faultClass.compareTo("") == 0) |
---|
91 | faultClass = "TYPING"; |
---|
92 | |
---|
93 | DataBag output = DefaultBagFactory.getInstance().newDefaultBag(); |
---|
94 | Tuple t = DefaultTupleFactory.getInstance().newTuple(1); |
---|
95 | String label = durationClass + " event - " + faultClass+ " fault"; |
---|
96 | //System.out.println("label="+label); |
---|
97 | t.set(0, label); |
---|
98 | |
---|
99 | output.add(t); |
---|
100 | return output; |
---|
101 | } |
---|
102 | return null; |
---|
103 | |
---|
104 | |
---|
105 | }catch(Exception e){ |
---|
106 | System.err.println("RealLabelsr: failed to process input; error - " + e.getMessage()); |
---|
107 | e.printStackTrace(); |
---|
108 | return null; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | } |
---|
113 | } |
---|