Hive provides the user-defined functions development interface for user extension. It is easy to get started. The following is an example of using MD5 for development.
Java Code :
Package Org. nalang. hive. UDF; import Java. io. unsupportedencodingexception; import Java. security. messagedigest; import Java. security. nosuchalgorithmexception; import org.apache.hadoop.hive.ql.exe C. UDF; import Org. apache. hadoop. io. text; public class MD5 extends UDF {public text evaluate (object... ARGs) {If (ARGs. length = 1) {return new text (rawmd5 (ARGs [0], 16);} else if (ARGs. length = 2 & ARGs [1]. equals ("16") {return new text (rawmd5 (ARGs [0], 16);} else {return new text (rawmd5 (ARGs [0], 32) ;}} private string rawmd5 (string Str) {messagedigest = NULL; try {messagedigest = messagedigest. getinstance ("MD5"); messagedigest. reset (); messagedigest. update (Str. getbytes ("UTF-8");} catch (nosuchalgorithmexception e) {system. out. println ("nosuchalgorithmexception caught! "); System. exit (-1);} catch (unsupportedencodingexception e) {e. printstacktrace ();} byte [] bytearray = messagedigest. digest (); stringbuffer md5strbuff = new stringbuffer (); For (INT I = 0; I <bytearray. length; I ++) {If (integer. tohexstring (0xff & bytearray [I]). length () = 1) md5strbuff. append ("0 "). append (integer. tohexstring (0xff & bytearray [I]); else md5strbuff. append (integer. tohexstring (0xff & bytearray [I]);} return md5strbuff. tostring (). touppercase ();} private string rawmd5 (Object STR, int length) {If (length = 16) {return rawmd5 (stringutils. stringfilter (STR )). substring (8, 24);} else {return rawmd5 (stringutils. stringfilter (STR ));}}}
Hive application:
Hive>Add JAR/home/work/local/hive/UDF. jar; Create temporary function MD5 as 'com. baifendian. hive. UDF. md5'; select MD5 ('test') from Taba limit 1;
Refer:
Https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF