I need the updatedDF as per new columns. but it is not updated with new columns , it still gives me old columns and its names
val schema = "sku_cd#sku_code,ean_nbr#ean,vnr_cd#nan_key,dsupp_pcmdty_desc#pack_descr"
val schemaArr = schema.split(",")
var df = spark.sql("""select sku_code, ean , nan_key, pack_descr from db.products""")
val updatedDF = populateAttributes(df,schemaArr)
def populateAttributes(df:DataFrame,schemaArr:Array[String]) : DataFrame = {
for(i <- schemaArr)
{
val targetCol = i.split("#")(0)
val sourceCol = i.split("#")(1)
df.withColumn(targetCol, col(sourceCol))
}
df
}
I get below output which is incorrect
scala> updatedDF.printSchema
root
|-- sku_code: string (nullable = true)
|-- ean: string (nullable = true)
|-- nan_key: string (nullable = true)
|-- pack_descr: string (nullable = true)
Expected output
|-- sku_cd: string (nullable = true)
|-- ean_nbr: string (nullable = true)
|-- vnr_cd: string (nullable = true)
|-- dsupp_pcmdty_desc: string (nullable = true)