-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericUDFRank.java
More file actions
104 lines (88 loc) · 3.53 KB
/
Copy pathGenericUDFRank.java
File metadata and controls
104 lines (88 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.apache.hadoop.hive.ql.udf.generic;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.ObjectInspectorOptions;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;
@Description(name = "rank", value = "_FUNC_(col1, col2, .., coln) - Returns the rank of current row, "
+ "when some column differs from one row to the next the rank is reset to 1.")
public class GenericUDFRank extends GenericUDF {
private long counter;
private Object[] previousKey;
private ObjectInspector[] ois;
@Override
public Object evaluate(DeferredObject[] currentKey) throws HiveException {
if (!sameAsPreviousKey(currentKey)) {
this.counter = 0;
copyToPreviousKey(currentKey);
}
return new Long(++this.counter);
}
@Override
public String getDisplayString(String[] currentKey) {
return "Rank-Udf-Display-String";
}
@Override
public ObjectInspector initialize(ObjectInspector[] arg0) throws UDFArgumentException {
ois=arg0;
return PrimitiveObjectInspectorFactory.javaLongObjectInspector;
}
/**
* This will help us copy objects from currrentKey to previousKeyHolder.
*
* @param currentKey
* @throws HiveException
*/
private void copyToPreviousKey(DeferredObject[] currentKey) throws HiveException {
if (currentKey != null) {
previousKey = new Object[currentKey.length];
for (int index = 0; index < currentKey.length; index++) {
previousKey[index]= ObjectInspectorUtils
.copyToStandardObject(currentKey[index].get(),this.ois[index]);
}
}
}
/**
* This will help us compare the currentKey and previousKey objects.
*
* @param currentKey
* @return - true if both are same else false
* @throws HiveException
*/
private boolean sameAsPreviousKey(DeferredObject[] currentKey) throws HiveException {
boolean status = false;
//if both are null then we can classify as same
if (currentKey == null && previousKey == null) {
status = true;
}
//if both are not null and there length as well as
//individual elements are same then we can classify as same
if (currentKey != null && previousKey != null && currentKey.length == previousKey.length) {
for (int index = 0; index < currentKey.length; index++) {
// avoid calling previousKey[index].getClass() when previousKey[index] is null
if (previousKey[index] != null) {
if (ObjectInspectorUtils.compare(currentKey[index].get(), this.ois[index], previousKey[index],
ObjectInspectorFactory.getReflectionObjectInspector(previousKey[index].getClass(), ObjectInspectorOptions.JAVA)) != 0) {
return false;
} else {
continue;
}
// Both objects are null
} else if (currentKey[index].get() == null ) {
continue;
// current is not null but previous is
} else {
return false;
}
}
status = true;
}
return status;
}
}