Oracle decode function Syntax: DECODE (value, if1, then1, if2, thne2, if3, then3,... else ). The first problem involved is the dynamic input parameters.
Oracle decode function Syntax: DECODE (value, if1, then1, if2, thne2, if3, then3,... else ). The first problem involved is the dynamic input parameters.
The customer requested that hive be used to implement the decode function similar to orale. Okay, start work.
Oracle decode function Syntax: DECODE (value, if1, then1, if2, thne2, if3, then3,... else ).
The first problem involved is the dynamic input parameters. The decode function requires an even number of input functions, and then implements logic judgment functions similar to if and else. In this case, the java Object... args is used to pass in multiple parameters, and then the method checks whether the number meets the even number. The reason why the Object type is selected is to dynamically identify the parameter type.
To determine the types of decode (1, 1.0, 'A', 2.0, 'B', 'C'), convert the Integer type to the double type, otherwise, such content cannot be implemented. Use instanceof to check the input parameter type to determine.
Related reading:
Hadoop cluster-based Hive Installation
Differences between Hive internal tables and external tables
Hadoop + Hive + Map + reduce cluster installation and deployment
Install in Hive local standalone Mode
WordCount word statistics for Hive Learning
------------------------------- Split line -------------------------------
Import org.apache.hadoop.hive.ql.exe c. UDF;
Public class Decode extends UDF {
Public String evaluate (Object... args ){
If (args. length % 2! = 0 ){
System. out. println ("the number of input parameters is incorrect. It should be an even number ");
}
Int number = args. length;
Object result = null;
Int flag = number-1;
Int I = 1;
If (args [0] instanceof Integer ){
Args [0] = Double. valueOf (Integer. valueOf (args [0]. toString ()));
}
While (I <flag ){
If (args [I] instanceof Integer ){
Args [I] = Double. valueOf (Integer. valueOf (args [I]. toString ()));
}
If (String. valueOf (args [I]). equals (String. valueOf (args [0]) {
Result = args [I + 1];
Break;
} Else {
I + = 2;
}
}
If (result = null)
Result = args [flag];
Return String. valueOf (result );
}
}
Hive details: click here
Hive: click here