Java代码-
package ClusterAlgoCall2;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.api.java.UDF3;
import org.apache.spark.sql.types.DataTypes;
public class ClusterAlgocall_Main3 {
public static void main(String[] args) {
SparkSession spark = SparkSession.builder().appName("someappname").enableHiveSupport().getOrCreate();
UDF3 UDFCosinetoTxt = new UDF3<String, String, String, String>() {
public String call(String col1, String col2 , String col3) throws Exception {
return col3 ;
}
};
spark.udf().register("UDFCosinetoTxt", UDFCosinetoTxt, DataTypes.StringType);
}
}创建了它&为它创建了一个jar。在使用-SQL创建临时函数之后,尝试在spark中使用此jar
add jar s3://xxxx/jar/ClusterAlgoCall2.jar ;
CREATE TEMPORARY FUNCTION ClusterAlgo as 'ClusterAlgoCall2.ClusterAlgocall_Main3' using jar 's3://xxxx/jar/ClusterAlgoCall2.jar';运行查询时错误-
select ClusterAlgo2(col1,col2,col3) from clusteralgo_test ;
Error in query: No handler for UDF/UDAF/UDTF 'ClusterAlgoCall2.ClusterAlgocall_Main3'; line 1 pos 7发布于 2022-04-14 15:38:03
要创建UDF,Java类需要遵循特定的模式。根据文档,实现类应该扩展其中一个基类:
class_name指定为要创建的函数提供实现的类的名称。实现类应该扩展其中一个基类,如下所示:
UDF或UDAF包中扩展org.apache.hadoop.hive.ql.exec。AbstractGenericUDAFResolver包中扩展org.apache.hadoop.hive.ql.udf.generic、GenericUDF或GenericUDTF。UserDefinedAggregateFunction包中扩展org.apache.spark.sql.expressions。因此,例如,类定义应该如下所示:
import org.apache.hadoop.hive.ql.exec.UDF;
public class ClusterAlgocall_Main3 extends UDF { ... }这里就是一个例子。
https://stackoverflow.com/questions/67776394
复制相似问题