背景:
函数会有迭代的过程,对于处理相同字段,由于业务驱动,不同时期会有不同的处理方法,对于以前的函数规则要保留,后续规则要在此函数上迭代,就像体制内常用的,“旧人旧规则,新人新办法”,哈哈,规则可以同时进行。
举例:
举个与实际业务场景不相关的例子做参考,功能实现上是一样的
对某字段使用base64加解密,代码如下
import sun.misc.BASE64Decoder;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.lazy.LazyString;
import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
import org.apache.hadoop.io.Text;
import org.apache.log4j.Logger;
/**
* @author zhifeiji
* @file_name GenericUDFBase64.java
* @date 2020年11月16日
*/
@Description(name="GenericUDFBase64",
value="_FUNC_(col,constant) -Returns enbase64 or debase64",
extended ="Example:\n+"+"select GenericUDFBase64(id,'V1') from test "
)
public class GenericUDFBase64 extends GenericUDF{
public static final Logger LOG = Logger.getLogger(GenericUDFBase64.class);
private ObjectInspector strOI;
private ObjectInspector strOI2;
//输出变量
private Text text=null;
String retStr=null;
String str=null;
String version=null;
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
//输入变量
strOI=arguments[0];
strOI2=arguments[1];
//获取参数2常量的值,值为版本号
version = ( (ConstantObjectInspector) this.strOI2).getWritableConstantValue().toString();
LOG.info("hello"+version);
//判断参数个数
if (arguments.length != 2) {
throw new UDFArgumentException("The function accepts 2 arguments.");
}
//判断参数1类型
if (!arguments[0].getCategory().equals(Category.PRIMITIVE) || !arguments[1].getCategory().equals(Category.PRIMITIVE)) {
throw new UDFArgumentException("The two functions must be PRIMITIVE");
}
//返回值类型
return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
}
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
LOG.info("hello3"+version);
str = ((StringObjectInspector)strOI).getPrimitiveJavaObject( arguments[0].get() );
LOG.info("hello2"+str);
GenericUDFBase64 bs=new GenericUDFBase64();
if(str==null ||str.trim().equals("")){
return null;
}
if(version.equals("V1")){ //加密
try {
retStr=bs.encryptBASE64(str.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(version.equals("V2")){ //解密
try {
retStr=bs.decryptBASE64(str.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
retStr=retStr;
text=new Text(retStr);
return text;
}
/**
* BASE64加密
* @throws Exception
*/
public String encryptBASE64(String key) throws Exception {
String encoded = new sun.misc.BASE64Encoder().encode(key.getBytes("utf-8"));
return encoded;
}
/**
* BASE64解密
* @throws Exception
*/
public String decryptBASE64(String key) throws Exception {
BASE64Decoder decoder = new BASE64Decoder();
byte[] decoded = decoder.decodeBuffer(key);
String decodeStr = new String(decoded,"utf-8");
return decodeStr;
}
@Override
public String getDisplayString(String[] children) {
assert( children.length>0 );
StringBuilder sb = new StringBuilder();
sb.append("GenericUDFBase64(");
sb.append(children[0]);
sb.append(children[1]);
sb.append(")");
return sb.toString();
}
}
函数打包注册后查询加解密结果如下,加密可以看做版本1,解密可以看做版本二,当然可以有其它版本,比如脱敏看做版本3