Input DF:
+-------------------+---------+
|VALUES |Delimiter|
+-------------------+---------+
|50000.0#0#0# |# |
|0@1000.0@ |@ |
|1$ |$ |
|1000.00^Test_string|^ |
+-------------------+---------+
Expected Output DF:
+-------------------+---------+----------------------+
|VALUES |Delimiter|SPLITED_VALUES |
+-------------------+---------+----------------------+
|50000.0#0#0# |# |[50000.0, 0, 0] |
|0@1000.0@ |@ |[0, 1000.0] |
|1$ |$ |[1] |
|1000.00^Test_string|^ |[1000.00, Test_string]|
+-------------------+---------+----------------------+
Code:
import sparkSession.sqlContext.implicits._
val dept = Seq(("50000.0#0#0#", "#"),("0@1000.0@", "@"),("1$", "$"),("1000.00^Test_string", "^")).toDF("VALUES", "Delimiter")
I am very new to spark, trying to split value of "VALUES" column by using Delimiter which is in another column.
Tried to use spark split function as
val dept2 = dept.withColumn("SPLITED_VALUES", split(col("VALUES"), "#"))
but here split function take delimiter as constant value, i am not able to pass this as
val dept2 = dept.withColumn("SPLITED_VALUES", split(col("VALUES"), col("Delimiter")))
Can anyone suggest better approach for this?