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