Created
June 27, 2011 22:24
-
-
Save dataminelab/1050002 to your computer and use it in GitHub Desktop.
Hive MD5 UDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dataminelab.hive.udf; | |
import org.apache.hadoop.hive.ql.exec.UDF; | |
import org.apache.hadoop.io.Text; | |
import java.security.*; | |
/** | |
* Calculate md5 of the string | |
*/ | |
public final class Md5 extends UDF { | |
public Text evaluate(final Text s) { | |
if (s == null) { | |
return null; | |
} | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
md.update(s.toString().getBytes()); | |
byte[] md5hash = md.digest(); | |
StringBuilder builder = new StringBuilder(); | |
for (byte b : md5hash) { | |
builder.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); | |
} | |
return new Text(builder.toString()); | |
} catch (NoSuchAlgorithmException nsae) { | |
System.out.println("Cannot find digest algorithm"); | |
System.exit(1); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I use this inside a hive script which I am trying to unit test inside a Java class using hive runner? I am using Intellij Maven project