0

Can any please help to know how to pass the non dataframe value as an argument to udf.

val df2 = df1.withColumn("newcol", udffunc(df1("col1"), x)).

The udf function i defined as below.

udffunc = udf(method _)

Method i defined as below.

def method(inputvar1: String, inputvar2: String): Option[Long] = {
  ...
  ...
  return Longvariable
}
7
  • Compile error. Its not allowing to pass the argument x as a argument. Can any one please help. Commented Sep 28, 2016 at 18:00
  • In the sbt console, could you run :type udffunc? Commented Sep 28, 2016 at 18:45
  • @Reactormonk: I did not get your question Commented Sep 28, 2016 at 20:22
  • I am working on IntelliJ IDE. Commented Sep 28, 2016 at 20:50
  • As per online looks like using withColumn there is no direct way to pass the external argument like hardcoding some string like I said. Commented Sep 29, 2016 at 3:02

1 Answer 1

1

I am able to fix the issue by just writing the below code for UDF.

def udffunc = udf((y:String) => {
    val format:String = "x"
    method(y, "x")
  })

So with this, I am able to pass one argument(dataframe column) from .withColumn to the UDF function udffunc and then am able to pass external argument which is "x" in the above example to the function named as "method".

Sign up to request clarification or add additional context in comments.

2 Comments

I'd recommend to always use val instead of var whenever possible. Immutable code is robust code.
Hi, using lit function i can pass the non dataframe argument from withColumn. Here is the code. val df2 = df1.withColumn(df1("col1"), lit(x))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.