项目场景:
1、hive2.x的环境,项目中使用hiveSQL使用自定义函数hiveUDF
问题描述
4台hiveserver2节点,其中两台不能执行:自定义函数
通过show functions like '*udf*';查询没有
原因分析:
Reload Function
Version information
As of Hive 1.2.0 (HIVE-2573).
RELOAD (FUNCTIONS|FUNCTION);
As of HIVE-2573, creating permanent functions in one Hive CLI session may not be reflected in HiveServer2 or other Hive CLI sessions, if they were started before the function was created. Issuing RELOAD FUNCTIONS within a HiveServer2 or HiveCLI session will allow it to pick up any changes to the permanent functions that may have been done by a different HiveCLI session. Due to backward compatibility reasons RELOAD FUNCTION; is also accepted.
从 HIVE-2573 开始,在一个 Hive CLI 会话中创建永久函数可能不会反映在 HiveServer2 或其他 Hive CLI 会话中,如果它们是在创建函数之前启动的。在 HiveServer2 或 HiveCLI 会话中发出 RELOAD FUNCTIONS 将允许它获取对不同 HiveCLI 会话可能已完成的永久功能的任何更改。由于向后兼容的原因 RELOAD FUNCTION;也被接受。
官方参考:LanguageManual DDL - Apache Hive - Apache Software Foundation
以下为hive2.x的reloadFuntions源码
public void reloadFunctions() throws HiveException {
HashSet<String> registryFunctions = new HashSet<String>(
FunctionRegistry.getFunctionNames(".+\\..+"));
for (Function function : getAllFunctions()) {
String functionName = function.getFunctionName();
try {
LOG.info("Registering function " + functionName + " " + function.getClassName());
String qualFunc = FunctionUtils.qualifyFunctionName(functionName, function.getDbName());
FunctionRegistry.registerPermanentFunction(qualFunc, function.getClassName(), false,
FunctionTask.toFunctionResource(function.getResourceUris()));
registryFunctions.remove(qualFunc);
} catch (Exception e) {
LOG.warn("Failed to register persistent function " +
functionName + ":" + function.getClassName() + ". Ignore and continue.");
}
}
// unregister functions from local system registry that are not in getAllFunctions()
for (String functionName : registryFunctions) {
try {
FunctionRegistry.unregisterPermanentFunction(functionName);
} catch (Exception e) {
LOG.warn("Failed to unregister persistent function " +
functionName + "on reload. Ignore and continue.");
}
}
}
解决方案:
1、beeline连接到对应hiveserver2节点(注意:不能执行此函数的所有hiveserver2节点)
2、执行 RELOAD (FUNCTIONS|FUNCTION);