df在Spark中拥有一个数据框:
|-- array_field: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- a: string (nullable = true)
| | |-- b: long (nullable = true)
| | |-- c: long (nullable = true)
Run Code Online (Sandbox Code Playgroud)
如何将字段重命名array_field.a为array_field.a_renamed?
[更新]:
.withColumnRenamed() 不适用于嵌套字段,所以我尝试了这个hacky和不安全的方法:
# First alter the schema:
schema = df.schema
schema['array_field'].dataType.elementType['a'].name = 'a_renamed'
ind = schema['array_field'].dataType.elementType.names.index('a')
schema['array_field'].dataType.elementType.names[ind] = 'a_renamed'
# Then set dataframe's schema with altered schema
df._schema = schema
Run Code Online (Sandbox Code Playgroud)
我知道设置私有属性不是一个好习惯,但我不知道为df设置架构的其他方法
我觉得我是在一个正确的轨道,但df.printSchema()仍显示为旧名array_field.a …