我有一个包含多个嵌套列的DataFrame。架构不是静态的,可能会改变我的Spark应用程序的上游。模式演化保证始终向后兼容。架构的匿名缩短版本粘贴在
下面root
|-- isXPresent: boolean (nullable = true)
|-- isYPresent: boolean (nullable = true)
|-- isZPresent: boolean (nullable = true)
|-- createTime: long (nullable = true)
<snip>
|-- structX: struct (nullable = true)
| |-- hostIPAddress: integer (nullable = true)
| |-- uriArguments: string (nullable = true)
<snip>
|-- structY: struct (nullable = true)
| |-- lang: string (nullable = true)
| |-- cookies: map (nullable = true)
| | |-- key: string
| | |-- value: array (valueContainsNull = true)
| | | |-- element: string (containsNull = true)
<snip>
火花作业应该改变&#34; structX.uriArguments&#34;从字符串到映射(字符串,字符串)。在this post中提出了类似的情况。但是,答案假设架构是静态的并且不会更改。所以case class
在我的情况下不起作用。
如果不对代码中的整个架构进行硬编码,那么转换structX.uriArguments
的最佳方法是什么?结果应如下所示:
root
|-- isXPresent: boolean (nullable = true)
|-- isYPresent: boolean (nullable = true)
|-- isZPresent: boolean (nullable = true)
|-- createTime: long (nullable = true)
<snip>
|-- structX: struct (nullable = true)
| |-- hostIPAddress: integer (nullable = true)
| |-- uriArguments: map (nullable = true)
| | |-- key: string
| | |-- value: string (valueContainsNull = true)
<snip>
|-- structY: struct (nullable = true)
| |-- lang: string (nullable = true)
| |-- cookies: map (nullable = true)
| | |-- key: string
| | |-- value: array (valueContainsNull = true)
| | | |-- element: string (containsNull = true)
<snip>
由于
答案 0 :(得分:2)
您可以尝试使用DataFrame.withColumn()
。它允许您引用嵌套字段。您可以添加新的map
列并删除平面列。 This question显示了如何使用withColumn
来处理结构。