1

我需要注册没有参数的udf函数。但Apache Spark没有UDF0接口的实现。 我想somethig这样的:如何在Java Spark的Apache Spark中无参数地注册UDF

UDF1<Object, String> my_func = o -> return "some_generated_string"; 
sqlContext.udf().register("my_func", my_func, DataTypes.StringType); 

df.withColumns("newCol", functions.expr("concat(col1, my_funct())"));回报例外org.apache.spark.sql.UDFRegistration$$anonfun$register$25$$anonfun$apply$1 cannot be cast to scala.Function0

因此df.withColumns("newCol", functions.expr("concat(col1, my_funct(1))"));工作正常,但这是错误的方式和气味不好。

UDFRegistrationorg.apache.spark.sql具有方法register[RT: TypeTag](name: String, func: Function0[RT]): UserDefinedFunction。 Java将此方法看作register(String name, Function0<RT> func, TypeTag<RT> evidence$1)。我可以写scala.Function0实现,但是什么是TypeTag证据$ 1

回答

0

我决心在明年招这个问题:

UDF1<Object, String> my_func = o -> "some_generated_string"; 
sqlContext.udf().register("my_func", my_func, DataTypes.StringType); 

String expression = "concat(`col1`, my_func())"; 
expression = expression.replace("my_func()", "my_func(null)"); 

df.withColumns("newCol", functions.expr(expression));