Hive UDF不会返回预期结果

时间:2013-12-13 11:22:57

标签: java select hadoop hive user-defined-functions

我编写了一个可由Hive(查询语言)调用的UDF,它接受2个参数并具有以下逻辑:

如果两个参数都为null,则

返回null 如果一个参数为null,则返回非null值 如果两个参数都不为空,则返回两个值中的较大值

我编写了代码,编译了类,并使用Hive成功注册了JAR。我验证了在创建临时函数后我可以在HIVE中看到该函数。我遇到的问题是,当我从select中调用它时,它只返回'_c0'而不是预期的值:

这是java类定义。

package com.ispace.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.exec.Description;
import java.util.*;
/*
*
* Compilation on Local box is very environment specific but for the iMac in 2013, this command will compile the class:
* javac -target 1.6 -cp $(ls /usr/local/Cellar/hive/0.12.0/libexec/lib/hive-exec*.jar):/usr/local/Cellar/hadoop/1.2.1/libexec/lib/hadoop-core.jar com/ispace/hive/udf/GreaterOf.java
* 
* The above step creates a single .class file that needs to be bundled into a JAR (java archive file)
* To bundle a file or multiple files into a jar, you can run this:
*       jar cvf udfcomparer.jar ./com/ispace/hive/udf/GreaterOf.class ./com/ispace/hive/udf/LesserOf.class
*
* To call a UDF, you must add the JAR to your hive session and then create a 'temporary'function as follows:
*
* hive (default)> ADD JAR /Users/calvinimac/Documents/Safezone/Projects/prospect-visual/etl/scripts/ec2-emr/jars/udfcomparer.jar;            
* hive (default)> create temporary function inlinemax as 'com.ispace.hive.udf.GreaterOf';
*/

@Description(name = "GreaterOf",
             value = "_FUNC_(Integer s, Integer t) - returns the greater value of the two.\n"+
                        "If both values are null, it will return NULL.\n"+
                        "If one value is non null, it will return that value if the other is NULL.",
             extended = "Example:\n"
                    + " > SELECT _FUNC_(column1, column2) FROM src;")

public final class GreaterOf extends UDF {
  public Integer evaluate(final Integer s, final Integer t) {
    Integer result = null;

    if (s == null && t == null) { 
        result = null; 
    } else if (s == null) {
        result = t;
    } else if (t == null) {
        result = s;
    } else if (s >= t) {
        result = s;
    } else {
        result = null;   
    }

    return result;
  }
}

在Hive中,我创建了一个占位符表(未使用) create table unused(id bigint) 然后我运行这个选择: 从未使用的

中选择inlinemax(2,4)

我期待得到4的结果,但我得到'c0'。

我的UDF是否错误并且它会将Hive null值作为参数处理并正确地将它们映射到我的Integer方法参数中吗?

2 个答案:

答案 0 :(得分:1)

未使用的是否有任何行?看起来“_c0”是Hive生成的派生列名。要获取任何行,您的查询表中至少需要一行。

答案 1 :(得分:0)

正如Jerome所说,java UDF确实会返回预期结果,只要该表(尽管是任意的)至少有一行数据。