Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 配置单元GenericUDF错误-RuntimeException typeInfo不能为null_Java_Hadoop_Hive_Runtimeexception - Fatal编程技术网

Java 配置单元GenericUDF错误-RuntimeException typeInfo不能为null

Java 配置单元GenericUDF错误-RuntimeException typeInfo不能为null,java,hadoop,hive,runtimeexception,Java,Hadoop,Hive,Runtimeexception,在AmazonEMR、Hive0.11上运行,我试图用GenericUDF类创建一个简单的UDF。我试图用UDF做的是从列中获取一个值,然后将其打印回屏幕。关键是看我是否能在构建更复杂的东西之前让它工作起来 我编译jar,加载到配置单元中,并创建一个临时函数 add jar ..../GenericTest.jar; create temporary function gen_test as 'GenericTest'; 当我使用错误数量的参数运行函数时,我得到了预期的错误: Semantic

在AmazonEMR、Hive0.11上运行,我试图用GenericUDF类创建一个简单的UDF。我试图用UDF做的是从列中获取一个值,然后将其打印回屏幕。关键是看我是否能在构建更复杂的东西之前让它工作起来

我编译jar,加载到配置单元中,并创建一个临时函数

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;
  }

}

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

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; 

若有电影标题,那个么电影标题就在那个里,若并没有,那个么测试就会被打印出来。

我已经让它运行了


在initialize()中,我需要类似于
returnOIResolver.update(参数[0])的东西(如第一个答案所示),以便return
returnOIResolver.get()将返回某些内容(返回值的ObjectInspector)

我可以让这个工作。如果我运行
returnOIResolver.update(参数[0]),也可以让我的工作在初始化语句中。我已经让它运行了。在initialize()中,我需要类似于
returnOIResolver.update(参数[0])的东西以便
返回returnOIResolver.get()将返回某些内容(返回值的ObjectInspector)。