use*_*285 4 scala apache-spark apache-spark-sql
我有一个结构作为我的json的一部分。
store: struct (c1, c2, c3, c4)
我想c2就地更新,这样就不会创建新字段。更新后,它应该是具有新值的相同结构c2。
在Spark/scala中,我已经尝试过:
df.withColumn("store.c2", newVal)
Run Code Online (Sandbox Code Playgroud)
但这会创建一个新字段store.c2,列不是结构的一部分,我可以更新。
df.withColumn("columnTen", newValue)
Run Code Online (Sandbox Code Playgroud)
不创建新字段并更新为newValue.
做这个
df.withColumn("store",struct($"store.c1", $"store.c2", $"store.c3", lit(newValue) as "c4"))
Run Code Online (Sandbox Code Playgroud)
将替换该字段中的值store.c4。
从 Spark 3.1+ 开始,您可以withField在结构列上使用:
StructType按名称添加/替换字段的表达式。
df.withColumn("store", $"store".withField("c2", lit(newVal)))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7608 次 |
| 最近记录: |