Creating UDF in HIVE Hadoop

In this blog “Creating UDF in HIVE Hadoop” I am going to show you how to create a UDF ( User Defined Functions) in HIVE.

1) Create a Java class Sha1encryption.Java

package mrd.training.sample;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import java.security.*;

public final class Sha1encryption extends UDF {

public Text evaluate(final Text s) {
if (s == null) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
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;
}
}

2) Exporting the JAR to the machine where Hive is running

3) Register the JAR in HIVE and then use it in your SELECT statement

add jar /home/notroot/lab/programs/HadoopTraining.jar;
create temporary function encryption as 'mrd.training.sample.Sha1encryption';

use trainingsun;
select encryption(name),age,mark,gender from students;

Thanks have a great day ! enjoy

Leave a Reply