Home > other >  Spark: convert array of arrays to array of strings?
Spark: convert array of arrays to array of strings?

Time:02-05

I have column with type Array of Arrays I need to get column array of string.

 -------------------------- 
|field                     |
 -------------------------- 
|[[1, 2, 3], [1, 2, 3], []]|
 -------------------------- 

I need to get:

 -------------------------- 
|field                     |
 -------------------------- 
|["123", "123", ""]        |
 -------------------------- 

Can I do this in Spark without using UDF?

CodePudding user response:

You can use transform higher order function,

import spark.implicits._

val df = Seq(Seq(Seq(1,2,3), Seq(1,2,3), Seq())).toDF("field")

df.withColumn("field", expr("transform(field, v->concat_ws('',v))"))
    .show

 ------------ 
|       field|
 ------------ 
|[123, 123, ]|
 ------------ 
  • Related