我在哪里可以找到有关 Spark SQL 中 from_json 函数的架构参数的更多详细信息?一位同事给了我一个有效的模式示例,但说实话,我只是不明白,它看起来不像我迄今为止找到的任何示例。找到的文档 here似乎缺乏。
最佳答案
在您分享的链接中 from_json
函数使用这个例子:
SELECT from_json('{"a":1, "b":0.8}', 'a INT, b DOUBLE');
Spark SQL supports the vast majority of Hive features such as the defining TYPES
我面临的示例问题要求我解析以下 JSON 对象:
{'data': [
{
"id":02938,
"price": 2938.0,
"quantity": 1
},
{
"id":123,
"price": 123.5,
"quantity": 2
}
]}
相应的 Spark SQL 查询如下所示:
SELECT
from_json('{"data":[{"id":123, "quantity":2, "price":39.5}]}'),
'data array<struct<id:INT, quantity:INT, price:DOUBLE>>').data) AS product_details;
you can couple this with the
explode
function to extract each element into it's own column.
我推荐这个 post了解有关为查询构建类型的更多信息。
有关更多示例,请参阅此 SO 帖子
https://stackoverflow.com/a/55432107/1500443
关于apache-spark-sql - Spark SQL from_json 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50373104/