//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.sm.hive;
import com.sm.util.Sm4Util;
import java.nio.charset.StandardCharsets;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
import org.apache.hadoop.io.Text;
public class Sm4EncryptUDF extends GenericUDF {
private static final Log log = LogFactory.getLog(Sm4EncryptUDF.class.getName());
private transient StringObjectInspector oi = null;
public Sm4EncryptUDF() {
}
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
this.oi = (StringObjectInspector)arguments[0];
if (arguments.length != 2) {
throw new UDFArgumentLengthException("函数参数格式不正确!");
} else {
return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
}
}
public Object evaluate(GenericUDF.DeferredObject[] arguments) throws HiveException {
log.info(String.format("入参1:%s,入参2:%s", arguments[0].get().toString(), arguments[1].get().toString()));
Object dataArgument = arguments[0].get();
if (dataArgument != null) {
Text t = this.oi.getPrimitiveWritableObject(arguments[0].get());
byte[] bytes = t.getBytes();
String utfStr = new String(bytes, StandardCharsets.UTF_8);
log.info(String.format("原始入参转UTF8后值:%s", utfStr));
String sm4KeyArgument = arguments[1].get().toString();
if (StringUtils.isBlank(sm4KeyArgument)) {
throw new UDFArgumentTypeException(1, "函数参数二格式或长度不正确!");
} else {
String encrypt = Sm4Util.encrypt(utfStr, sm4KeyArgument);
log.info(String.format("加密后密文数据:%s", encrypt));
Text text = new Text(encrypt.getBytes(StandardCharsets.UTF_8));
log.info(String.format("加密后密文数据:%s", new String(text.getBytes(), StandardCharsets.UTF_8)));
return text;
}
} else {
return "";
}
}
public String getDisplayString(String[] strings) {
return strings[0] + strings[1];
}
}
使用上面代码进行udf函数注册,在hive实际使用过程中发现select出来的加密后的中文值,和直接insert select出来的值不一致,