DF的现有架构:
|-- col1: string (nullable = true)
|-- col2: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- col2_1: string (nullable = true)
| | |-- col2_2: string (nullable = true)
现有架构的示例数据:
col1 col2
A [[0,2],[1,3]]
B [[1,5]]
C [[5,9],[4,6],[2,6]]
所需架构:
|-- col1: timestamp (nullable = true)
|-- col2_1: string (nullable = true)
|-- col2_2: string (nullable = true)
所需架构的示例数据:
col1 col2_1 col2_2
A 0 2
A 1 3
B 1 5
C 5 9
C 4 6
C 2 6
代码:
var df_flattened = df.select($"*", explode($"col2").as("flat")).select($"*",$"flat.col2_1",$"flat.col2_2").drop("col2")
我的代码没有任何错误。但是它缺少原始DF的值,其中原始DF(col1)在原始DF中约为20000,并且在展平后变为〜6000。
关于错误的任何建议。
答案 0 :(得分:0)
explode()
不会发出任何要爆炸的数组为null
的行。因此,您应该改用explode_outer()
。