gpt4 book ai didi

json - Spark SQL JSON 数据集查询嵌套数据结构

转载 作者:行者123 更新时间:2023-12-04 21:41:29 26 4
gpt4 key购买 nike

我有一个简单的 JSON 数据集,如下所示。如何查询所有parts.lockid = 1 .

JSON:

{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"],
"parts" : [
{
"lock" : "One lock",
"key" : "single key"
},
{
"lock" : "2 lock",
"key" : "2 key"
}
]
}

询问:
select id,name,price,parts.lockfrom product where id=1

关键是如果我使用 parts[0].lock它将返回如下一行:
{u'price': 12.5, u'id': 1, u'.lock': {u'lock': u'One lock', u'key': u'single key'}, u'name': u'A green door'}

但我想退回所有 locksparts结构体。它将返回多行,但那是我正在寻找的行。我想要完成的这种关系连接。

请在这件事上给予我帮助

最佳答案

df.select($"id", $"name", $"price", explode($"parts").alias("elem"))
.where("id = 1")
.select("id", "name", "price", "elem.lock", "elem.key").show

+---+------------+-----+--------+----------+
| id| name|price| lock| key|
+---+------------+-----+--------+----------+
| 1|A green door| 12.5|One lock|single key|
| 1|A green door| 12.5| 2 lock| 2 key|
+---+------------+-----+--------+----------+

关于json - Spark SQL JSON 数据集查询嵌套数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25224740/

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