Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update struct field spark/scala

I have a struct as part of my json.

store: struct (c1, c2, c3, c4)

I would like to update c2 in place so no new field is created. After update it should be same struct with new value for c2.

In spark/scala, I have already tried:

df.withColumn("store.c2", newVal)

But this creates a new field store.c2, Columns not part of struct, I am able to update.

df.withColumn("columnTen", newValue)

does not create new field and updated to newValue.

like image 515
user2570285 Avatar asked Oct 18 '25 15:10

user2570285


1 Answers

Since Spark 3.1+, you can use withField on a struct column:

An expression that adds/replaces field in StructType by name.

df.withColumn("store", $"store".withField("c2", lit(newVal)))
like image 100
blackbishop Avatar answered Oct 20 '25 05:10

blackbishop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!