我创建了以下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文件,并说已添加到类路径中,但是当我创建临时函数时却说未找到类。
答案 0 :(得分:0)
我能够创建并运行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杰斐逊城NULL 3 hLb2Ya9j + ayIpkCqWKqbCA == NULL NULL suR6IQZwLJ查尔斯顿NULL 4 ctljLq7D2 + h + nF52LvQDjA == NULL NULL yeMmNHuQca普罗维登斯NULL 5 ZGSoPIVwdya70f + qhuvsJg == NULL NULL XE3bw5Cjib朱诺NULL 6 8OgOf6opg38CEqRDCZVBkw == NULL NULL PQOXrqHyCP巴吞鲁日NULL 7 kjwaxk7pXtCuOEXqBD56dw == NULL NULL IrKwoTqXnR卡森城NULL 8 IyQAknyGOO3hnvRnMAaWQw == NULL NULL 5TdDMqAXrX天意NULL 9 cxyNZFnBCs0nQwwI5Hj07A == NULL NULL OtWW4BdFVv Raleigh 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