С UDF, назначенным для каждого столбца:
val df = List(
("09", "Tesla", "PC", "modify", "brand", "Jeep"),
("10", "Tesla", "SmallTablet", "modify", "brand", "Jeep"),
("09", "Tesla", "PC", "modify", "brand", "Jeep"),
("10", "Tesla", "SmallTablet", "modify", "mot1", "20"),
("10", "Tesla", "SmallTablet", "modify", "mot1", "20")
).toDF("mot1", "brand", "device", "action", "Column_to_modify", "New_value")
val modifyColumn = (colName: String, colValue: String, modifyColumnName: String, modifyColumnValue: String) =>
if (colName.equals(modifyColumnName)) modifyColumnValue else colValue
val modifyColumnUDF = udf(modifyColumn)
val result = df
.withColumn("mot1", modifyColumnUDF(lit("mot1"), $"mot1", $"Column_to_modify", $"New_value"))
.withColumn("brand", modifyColumnUDF(lit("brand"), $"brand", $"Column_to_modify", $"New_value"))
result.show(false)
Выход:
+----+-----+-----------+------+----------------+---------+
|mot1|brand|device |action|Column_to_modify|New_value|
+----+-----+-----------+------+----------------+---------+
|09 |Jeep |PC |modify|brand |Jeep |
|10 |Jeep |SmallTablet|modify|brand |Jeep |
|09 |Jeep |PC |modify|brand |Jeep |
|20 |Tesla|SmallTablet|modify|mot1 |20 |
|20 |Tesla|SmallTablet|modify|mot1 |20 |
+----+-----+-----------+------+----------------+---------+