美文网首页
Hive中加密字段的UDF

Hive中加密字段的UDF

作者: 悠扬前奏 | 来源:发表于2020-06-06 23:54 被阅读0次

    工作需要,写了两个用TripleDES算法给敏感信息字段加/解密的UDF,笔记一下:

    加密函数

    
    /**
     * 自定义hive函数,用TripleDES对敏感信息加密
     *
     * @author pengjz
     */
    public class UdfEncode extends GenericUDF {
    
        private static final String DES_KEY = "hello+1d13ef12d11a44b6aa44823e1478b529";
    
        @Override
        public ObjectInspector initialize(ObjectInspector[] objectInspectors) throws UDFArgumentException {
            if (objectInspectors.length != 1) {
                throw new UDFArgumentLengthException("Param must be 1 argument.");
            }
            if (objectInspectors[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
                throw new UDFArgumentTypeException(1, "A string argument was expected.");
            }
            return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
        }
    
        @Override
        public String evaluate(DeferredObject[] deferredObjects) throws HiveException {
            if (deferredObjects[0].get() == null
                    || Strings.isBlank(deferredObjects[0].get().toString())
                    || "\\N".equals(deferredObjects[0].get().toString())) {
                return "";
            }
            return TripleDesUtil.tripleDesEncrypt(deferredObjects[0].get().toString(), DES_KEY);
        }
    
        @Override
        public String getDisplayString(String[] strings) {
            return strings[0];
        }
    }
    

    解密函数

    /**
     * @author pengjz
     * @date 2019/10/24 19:55
     */
    class UdfDecode extends GenericUDF {
    
        private static final String DES_KEY = "hello+1d13ef12d11a44b6aa44823e1478b529";
    
        @Override
        public ObjectInspector initialize(ObjectInspector[] objectInspectors) throws UDFArgumentException {
            if (objectInspectors.length != 1) {
                throw new UDFArgumentLengthException("Param must be 1 argument.");
            }
            if (objectInspectors[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
                throw new UDFArgumentTypeException(1, "A string argument was expected.");
            }
            return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
        }
    
        @Override
        public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
            if (deferredObjects[0].get() == null
                    || Strings.isBlank(deferredObjects[0].get().toString())
                    || "\\N".equals(deferredObjects[0].get().toString())) {
                return "";
            }
            return TripleDesUtil.tripleDesDecrypt(deferredObjects[0].get().toString(), DES_KEY);
        }
    
        @Override
        public String getDisplayString(String[] strings) {
            return strings[0];
        }
    }
    

    相关文章

      网友评论

          本文标题:Hive中加密字段的UDF

          本文链接:https://www.haomeiwen.com/subject/tjlqtktx.html