[英]Select particular values from JSON column in Presto
I have a JSON column points
of type VARCHAR in a table which I want to parse in Presto. 我要在Presto中解析的表中有一个VARCHAR类型的JSON列
points
。 For example: 例如:
points = {"0": 0.2, "1": 1.2, "2": 0.5, "15": 1.2, "20": 0.7}
I want to select only the values for keys "0", "2" and "20"
. 我只想选择键
"0", "2" and "20"
。 How do I use the UNNEST functionality of Presto to get them. 如何使用Presto的UNNEST功能获取它们。 What I've done till now is:
到目前为止,我所做的是:
select t.value from myTable CROSS JOIN UNNEST(points) AS t(key, value) limit 1
But this gives this error: 但这给出了这个错误:
Cannot unnest type: varchar
Update:
I ran the following query and got the result but it is returning one random key-value pair from the JSON whereas I need specific keys. 我运行以下查询并获得了结果,但是它从JSON返回一个随机的键值对,而我需要特定的键。
select key, value from myTable CROSS JOIN UNNEST(SPLIT_TO_MAP(points, ',', ':')) AS t(key, value) limit 1
You may need to cast to json datatype first according to these docs: enter link description here 您可能需要根据以下文档首先转换为json数据类型: 在此处输入链接描述
UNNEST(CAST(points AS JSON))
Full query: 完整查询:
select t.value from myTable
CROSS JOIN UNNEST(CAST(points AS JSON)) AS t(key, value) limit 1
You can unnest an Array or Map. 您可以取消嵌套数组或映射。 So you first need to convert the JSON string into a MAP:
因此,您首先需要将JSON字符串转换为MAP:
CAST(json_parse(str) AS MAP<BIGINT, DOUBLE>)
Here is an example: 这是一个例子:
presto> select tt.value
-> from (VALUES '{"0": 0.2, "1": 1.2, "2": 0.5, "15": 1.2, "20": 0.7}') as t(json)
-> CROSS JOIN UNNEST(CAST(json_parse(json) AS MAP<BIGINT, DOUBLE>)) AS tt(key, value)
-> ;
value
-------
0.2
1.2
1.2
0.5
0.7
(5 rows)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.