0

关于如何解决以下问题的想法用完了。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

如果我尝试ref使用

resolved = (
     df.
        resolveChoice(
            specs = [('_field.ref','cast:array')]
        )
)

我丢失了记录。

关于我如何做的任何想法:

  1. 过滤 DataFrame 是否_field.refarraystruct
  2. struct记录转换为array或反之亦然
4

1 回答 1

1

我能够通过使用解决我自己的问题

resolved_df = ResolveChoice.apply(df, choice = "make_cols")

这会将array值保存在新ref_array列中,并将struct值保存在ref_struct列中。

这使我可以将 DataFrame 拆分为

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

于 2020-12-29T22:48:36.750 回答