缺少配置单元udf类

4sup72z8  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(343)

我创建了以下自定义函数:

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文件,它显示added to classpath,但当我创建临时函数时,它显示class not found。

csbfibhn

csbfibhn1#

我能够创建jar并运行它,但现在我得到了下面的错误。如果我只是运行select语句,它是正确加密的,但是当我从select创建表时,它在表中插入空值。详情请参见下文。如何避免CREATETABLEAS语句中列变为null的问题。

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 kqcnitorf des moines null 2 qkxexqbmwz5y9nwjo4pdyq==null u6792wt1mz jefferson city null 3 hlb2ya9j+ayipkcqwkqbca==null sur6iqzwlj charleston null 4 ctljlq7d2+h+nf52lvqdja==null yemmnhuqca providence null 5 zgsopivwdya70f+qhuvsjg==null xe3bw5cjib juneau null 6 8ogof6opg38ceqrdczvbkwkwk==null null pqoxrqhycp巴吞鲁日null 7 kjwaxk7pxtcuoexqbd56dw==null IRKWOTQNR卡森市null 8 iyqaknygoo3hnvrnmaawqw==null 5tddmqaxrx普罗维登斯null 9 CXYNZFBCS0NQWWI5HJ07A==null otww4bdfvv raleigh null 10 jbxii6c24f+s9hiwo52ryg==null NIRNATIWO 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

相关问题