【发布时间】:2019-07-07 22:18:36
【问题描述】:
我需要用null 替换数据框(带有嵌套模式)中的一些值,我见过这个solution 但它似乎只适用于一个
级别嵌套架构。
我的架构是这样的
root
......
......
......
|-- user: struct (nullable = true)
| |-- country: string (nullable = true)
| |-- id: string (nullable = true)
| |-- ip_address: string (nullable = true)
| |-- state: struct (nullable = true)
| | |-- level: long (nullable = true)
| | |-- session_id: string (nullable = true)
| | |-- xp: long (nullable = true)
我想要做的是将user.state.level 和user.state.xp 替换为null,并保持我的数据框的其余部分保持不变。
有什么方法可以实现吗?
如果我关注this solution
val myUDF = udf((s:String) => {
null
})
val structCols: Array[org.apache.spark.sql.Column] = badVersion.select($"user.*")
.columns
.map(name => col("user."+name))
val newDF = badVersion.withColumn(
"user",
struct((structCols:+myUDF($"user.country").as("country")):_*)
)
它适用于国家并替换价值,但如果我这样做
val newDF = badVersion.withColumn(
"user",
struct((structCols:+myUDF($"user.country").as("country"):+myUDF($"user.state.level").as("state.level")):_*)
)
只是将state.level 添加为新字段
【问题讨论】:
标签: scala apache-spark dataframe schema