1

I have a Spark dataframe with column that has array<struct<_1:string,_2:string>> datatype and following sample data:

WrappedArray([Book1,Title1], [Book2,Title2], [Book3,Title3])

I want to convert this column from WrappedArray of tuples to a string

Here is the desired output:

Book1/Title1 Book2/Title2 Book3/Title3

I tried the following udf passing that column of the dataframe, but it did not work:

val unwraparr = udf ((x: mutable.WrappedArray[(String, String)]) => x.map { case (val1, val2) => val1 + "/" + val2 })

1 Answer 1

1

Try like this:

import org.apache.spark.sql.Row

(x: Seq[Row]) => x.map { case Row(val1: String, val2: String) => val1 + "/" + val2 })
Sign up to request clarification or add additional context in comments.

2 Comments

Is this the complete UDF: val unwraparr = udf ((x: mutable.WrappedArray[(String, String)]) => x.map { case Row(val1: String, val2: String) => val1 + "/" + val2 })
Is the way you accepted the input parameters to the udf correct? If there's an array of tuples, do you accept it as Seq[Row]?

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.