【发布时间】:2020-07-30 18:16:09
【问题描述】:
我正在尝试使用 pyspark 将一些 mongo 集合引入到大查询中。架构如下所示。
root
|-- groups: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- my_field: struct (nullable = true)
| | | |-- **{ mongo id }**: struct (nullable = true)
| | | | |-- A: timestamp (nullable = true)
| | | | |-- B: string (nullable = true)
| | | | |-- C: struct (nullable = true)
| | | | | |-- abc: boolean (nullable = true)
| | | | | |-- def: boolean (nullable = true)
| | | | | |-- ghi: boolean (nullable = true)
| | | | | |-- xyz: boolean (nullable = true)
问题是在 my_field 中我们存储了 id,每个组都有自己的 id,当我将所有内容导入到大查询中时,我最终会为每个 id 生成一个新列。我想将 my_field 转换为字符串并将所有嵌套字段存储为 json 或类似的东西。但是当我尝试转换它时,我收到了这个错误
temp_df = temp_df.withColumn("groups.my_field", col("groups.my_field").cast('string'))
TypeError: Column is not iterable
我错过了什么?
【问题讨论】:
-
你考虑过使用BQ的
TO_JSON_STRING函数吗? -
你能发布一个示例输入行吗
-
to_json不起作用吗? -
它不适用于嵌套字段。我可以用 to_json 创建一个新字段,但只能在根级别,我不能替换 my_field 甚至把它放在同一级别
标签: python pyspark google-bigquery