scalaapache-spark-sqluser-defined-functionsudf

udf spark column names


I need to specify a sequence of columns. If I pass two strings, it works fine

val cols = array("predicted1", "predicted2")

but if I pass a sequence or an array, I get an error:

 val cols = array(Seq("predicted1", "predicted2"))

Could you please help me? Many thanks!


Solution

  • Function signature is def array(colName: String, colNames: String*): Column which means that it takes one string and then one or more strings. If you want to use a sequence, do it like this:

    array("predicted1", Seq("predicted2"):_*)
    

    From what I can see in the code, there are a couple of overloaded versions of this function, but neither one takes a Seq directly. So converting it into varargs as described should be the way to go.