首页 > 解决方案 > 缺少 HIVE UDF 类

问题描述

我在 UDF 函数下面创建了:

package co.hive.udf;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.*;
import org.apache.hadoop.hive.ql.exec.UDF;


public class encryptHivecolumn extends UDF {
    private static final String ALGO = "AES";
     private static final byte[] keyValue = 
                new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
        'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
     public static String encrypt(String Data) throws Exception {
            Key key = generateKey();
            Cipher c = Cipher.getInstance(ALGO);
            c.init(Cipher.ENCRYPT_MODE, key);
            byte[] encVal = c.doFinal(Data.getBytes());
            @SuppressWarnings("restriction")
            String encryptedValue = new BASE64Encoder().encode(encVal);
            return encryptedValue;
        }


        private static Key generateKey() throws Exception {
            Key key = new SecretKeySpec(keyValue, ALGO);
            return key;
    }




}

并从 Eclipse 创建 JAR。我也从eclipse添加了JAR文件,它说添加到类路径但是当我创建临时函数时它说找不到类。

标签: classhiveuser-defined-functions

解决方案


我能够创建 JAR 并运行它,但现在我遇到了错误。如果我只是运行 select 语句,则它是正确加密的,但是当我从 select 创建表时,它会在表中插入 NULL 值。请参阅下面的详细信息。如何避免在 create table as 语句中列变为空的问题。

hive> select id,Decrypt(Encrypt(firstname)),lastname,city  from plikhi.stagging_encrypt limit 10;
OK
1       Theodore        KQcNniTorf      Des Moines
2       Franklin        u6792WT1MZ      Jefferson City
3       Dwight  suR6IQZwLJ      Charleston
4       Ronald  yeMmNHuQca      Providence
5       Woodrow XE3bw5Cjib      Juneau
6       Richard PQOXrqHyCP      Baton Rouge
7       Lyndon  IrKwoTqXnR      Carson City
8       William 5TdDMqAXrX      Providence
9       George  OtWW4BdFVv      Raleigh
10      Harry   NiRnATHIWo      Augusta
Time taken: 0.078 seconds, Fetched: 10 row(s)

hive> create table Encrypt_Test as select id,Encrypt(firstname) as firstname,lastname,city  from plikhi.stagging_encrypt limit 10;

Query ID = plikhi.admin_20180907082938_f71da154-8c3c-46d5-88d4-b794712199e8
Total jobs = 1
Launching Job 1 out of 1
Status: Running (Executing on YARN cluster with App id application_1535856819605_0058)

Moving data to directory hdfs://vtorbdapc01.devad.moneris.com:8020/apps/hive/warehouse/encrypt_test
Table default.encrypt_test stats: [numFiles=1, numRows=10, totalSize=507, rawDataSize=497]
OK
Time taken: 10.161 seconds
hive> select * from Encrypt_Test;
OK

1 BiOPF4LrWbPHDWRbiSV4eg== NULL NULL KQcNniTorf Des Moines NULL 2 QkXExqbMWZ5y9NWjO4PDYQ== NULL NULL u6792WT1MZ Jefferson City NULL 3 hLb2Ya9j+ayIpkCqWKqbCA== NULL NULL suR6IQZwLJ Charleston NULL 4 ctljLq7D2+h+nF52LvQDjA== NULL NULL yeMmNHuQca Providence NULL 5 ZGSoPIVwdya70f+qhuvsJg== NULL NULL XE3bw5Cjib Juneau NULL 6 8OgOf6opg38CEqRDCZVBkw== NULL NULL PQOXrqHyCP Baton Rouge NULL 7 kjwaxk7pXtCuOEXqBD56dw== NULL NULL IrKwoTqXnR Carson City NULL 8 IyQAknyGOO3hnvRnMAaWQw== NULL NULL 5TdDMqAXrX Providence NULL 9 cxyNZFnBCs0nQwwI5Hj07A== NULL NULL OtWW4BdFVvRaleigh NULL 10 jBXiI6c24f+s9hiWo52RYg== NULL NULL NiRnATHIWo Augusta NULL 耗时:0.058 秒,获取:20 行

hive> select Decrypt(firstname) from Encrypt_Test;

Theodore
NULL
Franklin
NULL
Dwight
NULL
Ronald
NULL
Woodrow
NULL
Richard
NULL
Lyndon
NULL
William
NULL
George
NULL
Harry
NULL

推荐阅读