我正在尝试使用 databricks sql 获取嵌套 json 的数据,但无法对多个数组列执行爆炸,并且抛出以下错误
[UNSUPPORTED_GENERATOR.MULTI_GENERATOR] 不支持生成器:仅允许使用一台生成器
尝试执行以下查询时
SELECT
explode(array_join(from_json(food:Fruits.children\[*\].`Fruit Quantity`, 'array\<string\>'), ' - ')) AS `Fruit Quantity`,
explode(array_join(from_json(food:Fruits.children\[*\].`Fruit Weight`, 'array\<string\>'), ' - ')) AS `Fruit Weight`,
explode(array_join(from_json(food:Fruits.children\[\*\].`Fruit Packaging`, 'array\<string\>'), ' - ')) AS `Fruit Packaging`
FROM
input_table
输入
| Day | Food |
| -------- | -------- |
| Day 1 | {
"breakfast": {
"carbs": [
{
"carbs name": "No food"
}
]
},
"Fruits": {
"batch": "batch01",
"children": [
{
"Fruit name": "apple01",
"Fruit Quantity": 3,
"Fruit Weight": "50g",
"Fruit Packaging": "Basket"
},
{
"Fruit name": "apple01",
"Fruit Quantity": 3,
"Fruit Weight": "50g",
"Fruit Packaging": ""
},
{
"Fruit name": "apple02",
"Fruit Quantity": 5,
"Fruit Weight": "100g",
"Fruit Packaging": "Foil"
}
]
}
} |
注意:我需要没有任何 CTE 或子查询的 sql。
检查以下查询。
SELECT
day,
FROM_JSON(food, 'Fruits struct<batch string>').Fruits.batch AS Batch,
children['Fruit name'] AS `Fruit name`,
children['Fruit Quantity'] AS `Fruit Quantity`,
children['Fruit Weight'] AS `Fruit Weight`,
children['Fruit Packaging'] AS `Fruit Packaging`,
carbs['carbs name'] AS `carbs name`
FROM VALUES
('Day 1 ', '{ "breakfast": { "carbs": [ { "carbs name": "No food" } ] }, "Fruits": { "batch": "batch01", "children": [ { "Fruit name": "apple01", "Fruit Quantity": 3, "Fruit Weight": "50g", "Fruit Packaging": "Basket" }, { "Fruit name": "apple01", "Fruit Quantity": 3, "Fruit Weight": "50g", "Fruit Packaging": "" }, { "Fruit name": "apple02", "Fruit Quantity": 5, "Fruit Weight": "100g", "Fruit Packaging": "Foil" } ] } }') AS (day, food)
LATERAL VIEW EXPLODE(FROM_JSON(food, 'Fruits struct<children array<map<string,string>>>').Fruits.children) AS children
LATERAL VIEW EXPLODE(from_json(food, 'breakfast struct<carbs array<map<string,string>>>').breakfast.carbs) AS carbs
+------+-------+----------+--------------+------------+---------------+----------+
|day |Batch |Fruit name|Fruit Quantity|Fruit Weight|Fruit Packaging|carbs name|
+------+-------+----------+--------------+------------+---------------+----------+
|Day 1 |batch01|apple01 |3 |50g |Basket |No food |
|Day 1 |batch01|apple01 |3 |50g | |No food |
|Day 1 |batch01|apple02 |5 |100g |Foil |No food |
+------+-------+----------+--------------+------------+---------------+----------+