首页 > 解决方案 > Hive:如果不存在则创建函数

问题描述

在我的配置单元脚本的开头,我有以下声明:

create function x as y using jar z;

现在,如果该函数已经存在,它会给我一个错误:

Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.FunctionTask. AlreadyExistsException(message:Function x already exists)

如果我使用create function if not exists x as y using jar z;,我会得到这个错误:

Error while compiling statement: FAILED: ParseException line 7:192 cannot recognize input near 'if' 'not' 'exists' in function identifier

该语句create function if not exists将在 Impala 中工作,但似乎 hive 不支持它。这是在 Hive 中做类似事情的方法吗?

标签: hadoophivehiveql

解决方案


我在 hive 中使用以下语句来创建临时函数。

ADD JAR hdfs:///tmp/udfs/jar1.jar;
DROP  FUNCTION IF EXISTS rowid;
CREATE TEMPORARY FUNCTION rowid AS 'com.example.hive.jartest.RowIdUDF';

推荐阅读