UDF函数:UDF(User-Defined Functions)即是用户定义的hive函数。hive自带的函数并不能完全满足业务需求,这时就需要我们自定义函数了
开发自定义UDF函数需要继承’org.apache.hadoop.hive.ql.exec.UDF’类,并实现evaluate函数
实现sha256加密的UDF函数
package com.encryption.udf;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public class EncryptSha256 extends UDF {
public static String sha256(String name, String prefix) {
return DigestUtils.sha256Hex(name + prefix);
}
public Text evaluate(Text text) throws Exception {
if ((text == null) || (text.toString().trim().length() == 0)) {
return text;
} else {
String pre = "CvBOLjZtj#W"; //盐值
String res = sha256(text.toString(), pre);
return new Text(res);
}
}
public static void main(String[] args) throws Exception {
EncryptSha256 encry = new EncryptSha256();
Text textString = encry.evaluate(new Text("