gpt4 book ai didi

amazon-web-services - 如何关系化包含数组的 JSON

转载 作者:行者123 更新时间:2023-12-04 14:47:41 24 4
gpt4 key购买 nike

我正在使用 AWS Glue 读取包含 JSON 的数据文件(在 S3 上)。这是一个包含在数组中的数据的 JSON。我试过使用relationalize() 函数,但它不适用于数组。它确实适用于嵌套的 JSON,但这不是输入的数据格式。

有没有办法将 JSON 与其中的数组关联起来?

输入数据:

{
"ID":"1234",
"territory":"US",
"imgList":[
{
"type":"box"
"locale":"en-US"
"url":"boxart/url.jpg"
},
{
"type":"square"
"locale":"en-US"
"url":"square/url.jpg"
}
]
}

代码:
dfc = Relationalize.apply(frame = datasource0, staging_path = glue_temp_storage, name = "root", transformation_ctx = "dfc")
dfc.select('root').toDF().show()

输出:
+----+----------+--------+
|ID |territory |imgList |
+----+----------+--------+
|1234| US | 1|
+----+----------+--------+

期望的输出:
+----+----------+-------------+---------------+---------------+
|ID |territory |imgList.type |imgList.locale |imgList.url |
+----+----------+-------------+---------------+---------------+
|1234| US | box | en-US |boxart/url.jpg |
+----+----------+-------------+---------------+---------------+
|1234| US | square| en-US |square/url.jpg |
+----+----------+-------------+---------------+---------------+

最佳答案

Relationalize 为 JSON 文档中的每个数组创建 DynamicFrames。所以你只需要获取它并加入根表:

dfc = Relationalize.apply(frame = datasource0, staging_path = glue_temp_storage, name = "root", transformation_ctx = "dfc")
root_df = dfc.select('root')
imgList_df = dfc.select('root_imgList')

df = Join.apply(root_df, imgList_df, 'imgList', 'id')
df.toDF().show()

关于amazon-web-services - 如何关系化包含数组的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55251576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com