Implement the MD5 function in hive UDF

Source: Internet
Author: User

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

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.