Vai*_*hav 1 amazon-s3 amazon-web-services pyspark aws-glue
我正在使用 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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
代码:
dfc = Relationalize.apply(frame = datasource0, staging_path = glue_temp_storage, name = "root", transformation_ctx = "dfc")
dfc.select('root').toDF().show()
Run Code Online (Sandbox Code Playgroud)
输出:
+----+----------+--------+
|ID |territory |imgList |
+----+----------+--------+
|1234| US | 1|
+----+----------+--------+
Run Code Online (Sandbox Code Playgroud)
期望的输出:
+----+----------+-------------+---------------+---------------+
|ID |territory |imgList.type |imgList.locale |imgList.url |
+----+----------+-------------+---------------+---------------+
|1234| US | box | en-US |boxart/url.jpg |
+----+----------+-------------+---------------+---------------+
|1234| US | square| en-US |square/url.jpg |
+----+----------+-------------+---------------+---------------+
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2254 次 |
最近记录: |