Hive UDF implementation is similar to Oracle's decode Function

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.