问题
I am using AWS Glue to read data file containing JSON (on S3). This one is a JSON with data contained in array. I have tried using relationalize() function but it doesn't work on array. It does work on nested JSON but this is not the data format of input.
Is there a way to relationalize JSON with arrays in it?
Input data:
{
"ID":"1234",
"territory":"US",
"imgList":[
{
"type":"box"
"locale":"en-US"
"url":"boxart/url.jpg"
},
{
"type":"square"
"locale":"en-US"
"url":"square/url.jpg"
}
]
}
Code:
dfc = Relationalize.apply(frame = datasource0, staging_path = glue_temp_storage, name = "root", transformation_ctx = "dfc")
dfc.select('root').toDF().show()
Output:
+----+----------+--------+
|ID |territory |imgList |
+----+----------+--------+
|1234| US | 1|
+----+----------+--------+
Desired output:
+----+----------+-------------+---------------+---------------+
|ID |territory |imgList.type |imgList.locale |imgList.url |
+----+----------+-------------+---------------+---------------+
|1234| US | box | en-US |boxart/url.jpg |
+----+----------+-------------+---------------+---------------+
|1234| US | square| en-US |square/url.jpg |
+----+----------+-------------+---------------+---------------+
回答1:
Relationalize creates DynamicFrames for each arrays in the JSON document. So you just need to get it and join with the root table:
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()
来源:https://stackoverflow.com/questions/55251576/how-to-relationalize-json-containing-arrays