【发布时间】:2020-07-27 02:14:11
【问题描述】:
我正在将 .json 数据提取到 Google BigQuery 中,并且在提取时,来自 .json 的 arrays 和 objects 的数据类型都被转换为 string 列。 BigQuery 中的数据如下所示:
select 1 as id, '[]' as stringCol1, '[]' as stringCol2 union all
select 2 as id, null as stringCol1, null as stringCol2 union all
select 3 as id, "{'game': '22', 'year': 'sophomore'}" as stringCol1, "[{'teamName': 'teamA', 'teamAge': 37}, {'teamName': 'teamB', 'teamAge': 32]" as stringCol2 union all
select 4 as id, "{'game': '17', 'year': 'freshman'}" as stringCol1, "[{'teamName': 'teamA', 'teamAge': 32}, {'teamName': 'teamB', 'teamAge': 33]" as stringCol2 union all
select 5 as id, "{'game': '9', 'year': 'senior'}" as stringCol1, "[{'teamName': 'teamC', 'teamAge': 31}, {'teamName': 'teamD', 'teamAge': 17]" as stringCol2 union all
select 6 as id, "{'game': '234', 'year': 'junior'}" as stringCol1, "[{'teamName': 'teamC', 'teamAge': 42}, {'teamName': 'teamD', 'teamAge': 25]" as stringCol2
数据有点乱。
- 在
stringCol1中,有null和'[]'缺失数据的值。我想从这个字符串化对象创建两列game和year。 - 对于
stringCol2,这始终是一个包含两个对象的数组,具有相同的键(teamName和teamAge,在这种情况下)。然后需要将其转换为 4 列teamName1、teamAge1、teamName2、teamAge2。
This similar post 解决了将基本字符串化数组转换为非字符串化数组的问题,但这里的示例稍微复杂一些。特别是,其他帖子中的解决方案在这种情况下不起作用。
【问题讨论】:
标签: google-bigquery