i我想在Presto中解析的表中有一个varchar类型的JSON列points
。例如:
points = {"0": 0.2, "1": 1.2, "2": 0.5, "15": 1.2, "20": 0.7}
我只想选择键"0", "2" and "20"
的值。我如何使用Presto的非主要功能来获取它们。到现在为止,我所做的是:
select t.value from myTable CROSS JOIN UNNEST(points) AS t(key, value) limit 1
但这给出了此错误:
Cannot unnest type: varchar
Update:
我运行了以下查询并获得了结果,但是它是从JSON返回一个随机键值对,而我需要特定的键。
select key, value from myTable CROSS JOIN UNNEST(SPLIT_TO_MAP(points, ',', ':')) AS t(key, value) limit 1
您可以解开数组或地图。因此,您首先需要将JSON字符串转换为地图:
CAST(json_parse(str) AS MAP<BIGINT, DOUBLE>)
这是一个示例:
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)
您可能需要根据以下文档首先施放JSON数据类型:在此处输入链接描述
UNNEST(CAST(points AS JSON))
完整查询:
select t.value from myTable
CROSS JOIN UNNEST(CAST(points AS JSON)) AS t(key, value) limit 1