1

引数なしでudf関数を登録する必要があります。しかし、Apache SparkはUDF0インタフェースを実現していません。 私はsomethigのようにしよう:Apache Sparkで引数なしのUDFをJavaで登録する方法

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))"));は正しく動作しますが、これは間違った方法であり、悪い匂いです。

UDFRegistrationでorg.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の実装を書いたことができますが、は何ですか?タイプタグの証拠$ 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)); 
関連する問題