hive genericudf error-runtimeexception typeinfo不能为null

6ovsh4lw  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(557)

在amazonemr、hive0.11上运行,我试图用genericudf类创建一个简单的udf。我试图用udf做的是,从一列中获取一个值,然后打印回屏幕。关键是看我能不能在建造更复杂的东西之前把它修好。
我编译jar,加载到hive中,并创建一个临时函数。

add jar ..../GenericTest.jar;
create temporary function gen_test as 'GenericTest';

当我用错误的参数数运行函数时,会得到预期的错误:

SemanticException [Error 10015]: Line 1:13 Arguments length mismatch 'gen_test': Wrong # of Args

但是,当我将正确数量的参数传递给它时,它会立即失败,并显示以下消息:

FAILED: RuntimeException typeInfo cannot be null!

到目前为止,我还没有找到这个问题的根源。此自定义项的代码如下。

import org.apache.hadoop.hive.ql.exec.Description; 
import org.apache.hadoop.hive.ql.exec.UDFArgumentException; 
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; 
import org.apache.hadoop.hive.ql.metadata.HiveException; 
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; 
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils; 
import org.apache.hadoop.hive.serde2. objectinspector.ObjectInspector;

public class GenericTest extends GenericUDF {

  private GenericUDFUtils.ReturnObjectInspectorResolver returnOIResolver;
  private ObjectInspector[] argumentOIs;

  public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
     argumentOIs = arguments;
     if (arguments.length != 1) {
       throw new UDFArgumentLengthException("Wrong # of Args");
     }

        if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE)
        throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted");

     returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true);

     return returnOIResolver.get();
  }

  public Object evaluate(DeferredObject[] arguments) throws HiveException {
    Object retVal = returnOIResolver.convertIfNecessary(arguments[0].get(), argumentOIs[0]);
    return retVal;
  }

  public String getDisplayString(String[] children){
    String rt = "get Display String test";
    return rt;
  }

}
xqk2d5yq

xqk2d5yq1#

如果你想试试基本的:你可以用这个

package yarn;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;

public class GenericUDFNvl extends GenericUDF {
private GenericUDFUtils.ReturnObjectInspectorResolver returnOIResolver;
private ObjectInspector[] argumentOIs;
@Override
public ObjectInspector initialize(ObjectInspector[] arguments)
throws UDFArgumentException {
 argumentOIs = arguments;
 if (arguments.length != 2) {
 throw new UDFArgumentLengthException(
 "The operator 'NVL' accepts 2 arguments.");
 }
 returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true);
 if (!(returnOIResolver.update(arguments[0]) && returnOIResolver
 .update(arguments[1]))) {
 throw new UDFArgumentTypeException(2,
 "The 1st and 2nd args of function NLV should have the same type, "
 + "but they are different: \"" + arguments[0].getTypeName()
 + "\" and \"" + arguments[1].getTypeName() + "\"");
 }
 return returnOIResolver.get();
 }
 @Override
 public Object evaluate(DeferredObject[] arguments) throws HiveException {
    // TODO Auto-generated method stub
    Object retVal = returnOIResolver.convertIfNecessary(arguments[0].get(),
            argumentOIs[0]);
            if (retVal == null ){
            retVal = returnOIResolver.convertIfNecessary(arguments[1].get(),
            argumentOIs[1]);
            }
            return retVal;

}
@Override

    public String getDisplayString(String[] children) {
        StringBuilder sb = new StringBuilder();
        sb.append("if ");
        sb.append(children[0]);
        sb.append(" is null ");
        sb.append("returns");
        sb.append(children[1]);
        return sb.toString() ;
        }

public static void main(String[] args) {

}
}

必须传递2个参数如果第一个参数不为null,那么它将打印第一个参数,如果第一个参数为null,那么它将打印第二个参数

select nvl(movie_title,"test") from u_item_test1;

如果有电影标题,那么电影标题,如果没有,那么测试将被打印出来

w80xi6nr

w80xi6nr2#

我已经把它弄走了。
在initialize()中,我需要类似于 returnOIResolver.update(arguments[0]); (如第一个答案所示)所以 returnOIResolver.get(); 将返回一些内容(返回值的objectinspector)。

相关问题