簡體   English   中英

Hive UDF不返回預期結果

[英]Hive UDF does not return expected result

我編寫了一個UDF,它可以由Hive(查詢語言)調用,它帶有2個參數,並且具有以下邏輯:

如果兩個參數都為null,則返回null;如果一個參數為null,則返回非null值;如果兩個傳入的參數都不為null,則返回兩個值中的較大者

我已經編寫了代碼,編譯了類,並在Hive中成功注冊了JAR。 我確認可以在創建臨時功能后在HIVE中看到該功能。 我遇到的問題是,當我從選擇中調用它時,它僅返回“ _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中,我創建一個占位符表(未使用),創建表未使用(id bigint),然后運行此選擇:從未使用中選擇inlinemax(2,4)

我原本希望得到4的結果,但我卻得到'c0'。

我的UDF是否出錯,它將Hive空值作為參數處理並將其正確映射到我的Integer方法參數中嗎?

未使用的物品中是否有任何行? 看起來“ _c0”是Hive生成的派生列名稱。 要獲取任何行,您的查詢表中至少需要一行。

正如Jerome所指出的,只要表(盡管是任意的)具有至少一行數據,Java UDF的確會返回預期結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM