Pyspark AWS将列选择解析为数组或结构

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 | | | | |--

关于如何解决以下问题的想法已经没有了。Glue数据目录中的表具有以下架构:

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()
    )后,重新组合它们