amazon-web-services - 如何关系化包含数组的 JSON
问题描述
我正在使用 AWS Glue 读取包含 JSON 的数据文件(在 S3 上)。这是一个 JSON,其数据包含在数组中。我曾尝试使用关系化()函数,但它不适用于数组。它确实适用于嵌套的 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()
推荐阅读
- php - 在 eloquent 中选择加入 laravel
- jquery - Jquery如何找出我是否有一个选中的复选框然后将其放入if语句中
- android - GET_ACCOUNT 权限未在 android N 中授予
- java - Spock Maven - 无法使用现有的 Java 单元测试代码和 *.groovy 文件运行
- laravel - 在添加之前对数据库中的添加值进行哈希处理
- google-api - 通过 YouTube 数据 API 验证 YouTube Premium 会员
- azure - 针对 Azure 表存储的并行 StartsWith 查询
- java - Hibernate Validator 不起作用,但我不知道为什么
- reactjs - 这个编译错误的原因是什么?“类型 IntrinsicAttributes 和 TabProps 上不存在属性 containerElement ...”
- c# - C# WEB API 从 HTTP2 到 SPDY 的协议更改