Skip to content

Instantly share code, notes, and snippets.

@dataminelab
Created June 27, 2011 22:24
Show Gist options
  • Save dataminelab/1050002 to your computer and use it in GitHub Desktop.
Save dataminelab/1050002 to your computer and use it in GitHub Desktop.
Hive MD5 UDF
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;
}
}
@rajb2r
Copy link

rajb2r commented May 12, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment