我们用 Java 编写了一个 Hive UDF,以从添加到分布式缓存中的文件中获取值,该文件可以完美地从如下选择查询中运行:
查询 1。
select country_key, MyFunction(country_key,"/data/MyData.txt") as capital from tablename;
但是在尝试从其输出创建表时不起作用。喜欢 :
查询 2。
create table new_table
as
select country_key, MyFunction(country_key,"/data/MyData.txt") as capital from tablename;
它甚至不能从外部选择工作。喜欢 :
查询 3。
select t.capital from
(
select country_key, MyFunction(country_key,"/data/MyData.txt") as capital from tablename
) t;
下面是我的 UDF 的评估函数:
public class CountryMap extends UDF{
Map<Integer, String> countryMap = null;
public String evaluate(Integer keyCol, String mapFile) {
if (countryMap == null){
//read comma delimited data from mapFile and build a hashmap
countryMap.put(key, value);
}
if (countryMap.containsKey(keyCol)) {
return countryMap.get(keyCol);
}
return "NA";
}
}
在 Hive 中添加 jar、文件和创建 Hive 临时函数,如:
ADD JAR /data/CountryMap-with-dependencies.jar;
ADD FILE /data/MyData.txt;
CREATE TEMPORARY FUNCTION MyFunction as 'CountryMap';
当我运行查询 1 时,我从 Map 获得预期值,但是当我运行查询 2 和 3 时,我得到“NA”。当我为查询 2 和 3 返回 Map.size() 代替“NA”时,它为零。
我很困惑为什么外部选择或创建表无法获取 coutryMap() 值以及为什么 Map 的大小变为零。