在配置单元中注册python自定义udf

pcrecxhr  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(391)

我的问题是在配置单元中注册python udf。
我创建了一个用于配置单元查询的加密和解密python代码。这些都按预期工作。但是,我不想每次使用它时都添加文件,而是希望在udf中创建一个永久注册表。
我看到可以使用“创建临时函数”将JavaJAR文件添加到udf列表中。对于python,我看不到这种情况。
对任何快速的帮助表示感谢。
下面是为我工作的脚本。

vi decry.py 

    import base64
    import sys

    try:
        for line in sys.stdin:
            line=line.strip()
            (a,b)=line.split('\t')
            print('\t'.join([a,b,base64.b64decode(bytes(a.replace('\n',''),encoding='utf-8')).decode()]))
    except:
        print(sys.exc_info())

vi encry.py

    import base64
    import sys

    try:
        for line in sys.stdin:
            line=line.strip()
            (a,b)=line.split('\t')
            print('\t'.join([a,b,base64.b64encode(bytes(a.replace('\n',''),encoding='utf-8')).decode()]))
    except:
        print(sys.exc_info())

数据库测试

create database test; 
    use test; 
    create table abc (id integer,name string); 
    insert into abc values(1,'vijay'); 
    insert into abc values(2,'vaasista');
    add FILE /hadoop/encry.py; 
    add FILE /hadoop/decry.py;
    select transform(id) using 'python3 encry.py' as id from abc;
    select transform(id,name) using 'python3 encry.py' as id,name,ct from
    abc;
col17t5w

col17t5w1#

首先 base64 它不是加密,而是用不同的字符集(基数64-base)编码,每个人都可以反转它,因为没有使用密钥。
第二:不幸的是,使用python udf的唯一方法是在配置单元中进行转换,create[temporary]函数不支持python。
考虑使用本地函数来获得更好的性能和灵活性,像这样:

--encode
base64(encode(regexp_replace(text_col,'\\n',''), 'UTF-8'))    
--decode
decode(unbase64(str),'UTF-8')

演示:

select base64(encode(regexp_replace('My test \n String','\\n',''), 'UTF-8'))

结果:

TXkgdGVzdCAgU3RyaW5n

解码:

select decode(unbase64('TXkgdGVzdCAgU3RyaW5n'), 'UTF-8')

结果:

My test  String

如果您需要加密,hive中还提供了aes_加密和aes_解密函数。

相关问题