Hive:Java Class类注册成udf函数

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

当hive现有函数无法满足需求的时候可以将代码打成jar包注册成函数之后就可以直接引用

一、需求

        hive上游数据发送到表的字段内容都是加密的需要解密后供使用方使用

二、代码转换

1原始Java Class代码

public class EncrypDES2 {
    private static String strDefaultKey = "joseph@#$%^&";
    //加密工具
    private Cipher encryptCipher = null;
    // 解密工具
    private Cipher decryptCipher = null;

    /**
     * 默认构造方法使用默认密钥
     */
    public EncrypDES2() throws Exception {
        this(strDefaultKey);
    }

    /**
     * 指定密钥构造方法
     * @param strKey 指定的密钥
     * @throws Exception
     */

    public EncrypDES2(String strKey) throws Exception {

        // Security.addProvider(new com.sun.crypto.provider.SunJCE());
        Key key = getKey(strKey.getBytes());
        encryptCipher = Cipher.getInstance("DES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key);
        decryptCipher = Cipher.getInstance("DES");
        decryptCipher.init(Cipher.DECRYPT_MODE, key);
    }

    /**
     * 将byte数组转换为表示16进制值的字符串 如byte[]{8,18}转换为0813和public static byte[]
     *
     * hexStr2ByteArr(String strIn) 互为可逆的转换过程
     *
     * @param arrB 需要转换的byte数组
     * @return 转换后的字符串
     * @throws Exception  本方法不处理任何异常所有异常全部抛出
     */
    public static String byteArr2HexStr(byte[] arrB) throws Exception {
        int iLen = arrB.length;
        // 每个byte用2个字符才能表示所以字符串的长度是数组长度的2倍
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = arrB[i];
            // 把负数转换为正数
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            // 小于0F的数需要在前面补0
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    /**
     * 将表示16进制值的字符串转换为byte数组和public static String byteArr2HexStr(byte[] arrB)
     * 互为可逆的转换过程
     * @param strIn 需要转换的字符串
     * @return 转换后的byte数组
     */
    public static byte[] hexStr2ByteArr(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
        // 两个字符表示一个字节所以字节数组长度是字符串长度除以2
        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }

    /**
     *
     * 加密字节数组
     * @param arrB 需加密的字节数组
     * @return 加密后的字节数组
     */
    public byte[] encrypt(byte[] arrB) throws Exception {
        return encryptCipher.doFinal(arrB);
    }
    /**
     * 将一段错误解码的字符串重新解码
     */
    public static String convertEncodingFormat(String str, String formatFrom, String FormatTo) {
        String result = null;
        if (!(str == null || str.length() == 0)) {
            try {
                result = new String(str.getBytes(formatFrom), FormatTo);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 加密字符串
     * @param strIn 需加密的字符串
     * @return 加密后的字符串
     */
    public String encrypt(String strIn) throws Exception {
//        String str1 = convertEncodingFormat(strIn, "ISO-8859-1", "utf-8");

        return byteArr2HexStr(encrypt(strIn.getBytes("utf-8")));
//          byte[] strs = strIn.getBytes("UTF-8");
//          return encrypt(strIn.getBytes());
    }

    /**
     * 解密字节数组
     * @param arrB 需解密的字节数组
     * @return 解密后的字节数组
     */
    public byte[] decrypt(byte[] arrB) throws Exception {
        return decryptCipher.doFinal(arrB);
    }

    /**
     * 解密字符串
     * @param strIn 需解密的字符串
     * @return 解密后的字符串
    EncrypDES     */
    public String decrypt(String strIn) throws Exception {
        return new String(decrypt(hexStr2ByteArr(strIn)));
//        return new String(decrypt(hexStr2ByteArr(strIn)));
    }

    /**
     * 从指定字符串生成密钥密钥所需的字节数组长度为8位 不足8位时后面补0超出8位只取前8位
     * @param arrBTmp 构成该字符串的字节数组
     * @return 生成的密钥
     */
    private Key getKey(byte[] arrBTmp) throws Exception {
        // 创建一个空的8位字节数组默认值为0
        byte[] arrB = new byte[8];
        // 将原始字节数组转换为8位
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        // 生成密钥
        Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
        return key;


    }


    public static void main(String[] args) {
        try {
            String msg1 = "张三";

            EncrypDES2 des1 = new EncrypDES2();// 使用默认密钥

            System.out.println("加密前的字符" + msg1);

            System.out.println("加密后的字符" + des1.encrypt(msg1));

            System.out.println("解密后的字符" + des1.decrypt(des1.encrypt(msg1)));

            System.out.println("-------------------------------");

            String msg2 = "李四";

            String key =  "joseph";   // 自定义密钥

            EncrypDES2 des2 = new EncrypDES2(key);

            System.out.println("加密前的字符" + msg2);

            System.out.println("加密后的字符" + des2.encrypt(msg2));

            System.out.println("解密后的字符" + des2.decrypt(des2.encrypt(msg2)));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

2转换后

/**
 * 数据解密java class类转成hive udf函数
 * @author joseph25
 * @version 1.0
 */
public class EncrypDES  extends UDF {

    public String evaluate(String input)throws Exception{
        //解密工具
        Cipher decryptCipher;
        //默认密钥
        String strDefaultKey = "joseph@#$%^&";

        decryptCipher = Cipher.getInstance("DES");

        byte[] arrB1 = new byte[8];
        // 将原始字节数组转换为8位
        for (int i = 0; i < strDefaultKey.getBytes().length && i < arrB1.length; i++) {
            arrB1[i] = strDefaultKey.getBytes()[i];
        }
        // 生成密钥
        Key key = new javax.crypto.spec.SecretKeySpec(arrB1, "DES");

        decryptCipher.init(Cipher.DECRYPT_MODE, key);


        byte[] arrB = input.getBytes();
        int iLen = arrB.length;
        // 两个字符表示一个字节所以字节数组长度是字符串长度除以2
        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        byte[] bytes = decryptCipher.doFinal(arrOut);
        return new String(bytes);
    }

//    public static void main(String[] args) throws Exception {
//        EncrypDES s= new EncrypDES();
//        System.out.println(s.evaluate("53f1ce867cd057a9"));
//    }
}

三、注册udf

clean-> package后将jar上传到服务器/user/joseph/下在hive里加载jar包

1注册临时函数

        hive> add jar /user/joseph/xx.jar;

        hive> CREATE TEMPORARY FUNCTION decrypt_udf AS 'com.joseph.udf';

        AS 后的 'com.joseph.udf'是你代码的包路径

        验证

        hive> select decrypt_udf('xxxxx');

        注意临时函数当退出hive后函数失效

2注册永久函数

        将jar包上传到hdfs上

        hdfs  dfs -put /user/joseph/xxx.jar /user/joseph/lib/

        在hive界面建立永久函数

        hive> create function decryptudf as 'com.xx.udf' using jar 'hdfs://ns1/user/joseph/lib/xx.jar'

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java