Pyspark AWS将列选择解析为数组或结构
关于如何解决以下问题的想法已经没有了。Glue数据目录中的表具有以下架构:Pyspark AWS将列选择解析为数组或结构,pyspark,aws-glue,aws-glue-data-catalog,aws-glue-spark,Pyspark,Aws Glue,Aws Glue Data Catalog,Aws Glue Spark,关于如何解决以下问题的想法已经没有了。Glue数据目录中的表具有以下架构: root |-- _id: string |-- _field: struct | |-- ref: choice | | |-- array | | | |-- element: struct | | | | |-- value: null | | | | |-- key: string | | | | |--
root
|-- _id: string
|-- _field: struct
| |-- ref: choice
| | |-- array
| | | |-- element: struct
| | | | |-- value: null
| | | | |-- key: string
| | | | |-- name: string
| | |-- struct
| | | |-- value: null
| | | |-- key: choice
| | | | |-- int
| | | | |-- string
| | | |-- name: string
如果我尝试使用
resolved = (
df.
resolveChoice(
specs = [('_field.ref','cast:array')]
)
)
我丢失了记录
关于我如何能够:
\u field.ref
是否为数组
或结构
struct
记录转换为数组
或反之亦然我能用电脑解决我自己的问题
resolved_df = ResolveChoice.apply(df, choice = "make_cols")
这将在新的ref\u array
列中保存array
值,并在ref\u struct
列中保存struct
值
这允许我将数据帧按
resolved_df1 = resolved_df.filter(col("ref_array").isNotNull()).select(col("ref_array").alias("ref"))
resolved_df2 = resolved_df.filter(col("ref_struct").isNotNull()).select(col("ref_struct").alias("ref"))
将数组仅转换为结构(使用explode()
)或将结构转换为数组(使用array()
)后,重新组合它们