Hive的 TRANSFORM 关键字提供了在SQL中调用自写脚本的功能,适合实现Hive中没有的功能又不想写UDF的情况,属于在官方函数和自建UDF之间的一个偷懒状态,举个功能是实现使用空格做字符串拆分的例子:

编写python脚本

#!/usr/bin/python
import sys
for line in sys.stdin:
line = line.strip()
fname , lname = line.split(' ')
l_name = lname.lower()
print '\t'.join([fname, str(l_name)])

上传

将.py文件上传到容器的/usr/lib/inceptor/lib目录,做镜像持久化

创建表

CREATE TABLE test01(id varchar2(255) ) CLUSTERED by(id) INTO 1 BUCKETS STORED AS ORC_TRANSACTION;
INSERT INTO test01 VALUES('liu kaiwen');
INSERT INTO test01 VALUES('zhang sanfeng');

执行

执行transform语句:

SELECT transform(id) USING 'python /usr/lib/inceptor/lib/split.py' AS thing1, thing2 FROM test01;

image

可以看到结果可以拿到。

但是如果开启安全模式,也就是类似Ranger这样的安全认证后,则会出错:

SQL 错误 [12] [08S01]: FAILED: Hive Internal Error: org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException(Query with transform clause is disallowed in current configuration. )

该问题,是因为transform是hive的一个重大安全漏洞,如果一定要使用python udf的话,只能关安全模式,与权限无关。

The TRANSFORM statement in Hive is a big security hole with Hive run without impersonation, so when SQL Standard Authorization is enabled, the feature id completely disabled which is a bit of a sledgehammer approach to securing this statement.

可以参考: https://issues.apache.org/jira/browse/RANGER-738


扫码手机观看或分享: