如何在Presto中将varchar转换为MAP(VARCHAR,VARCHAR) [英] How to cast varchar to MAP(VARCHAR,VARCHAR) in presto
问题描述
我在表中保存了一个表,名为( mappings)的一列具有键值对作为字符串
I have table in presto, one column named ("mappings") have key-value pair as string
从hello中选择映射;
select mappings from hello;
例如:{ foo: baar, foo1: bar1}
Ex: {"foo": "baar", "foo1": "bar1" }
我想投射映射列成MAP
I want to cast "mappings" column into a MAP
像从hello中选择CAST( mappings as MAP);
like select CAST("mappings" as MAP) from hello;
错误的。
推荐答案
MAP没有规范的字符串表示形式> code>在Presto中,因此无法将其直接转换为
MAP(VARCHAR,VARCHAR)
。但是,如果您的字符串包含JSON映射,则可以使用 json_parse
函数将字符串转换为 JSON
类型的值,并将其转换为SQL MAP
通过 cast 。
There is no canonical string representation for a MAP
in Presto, so so there's no way to cast it directly to MAP(VARCHAR, VARCHAR)
. But, if your string contains a JSON map, you can use the json_parse
function to convert the string into a value of JSON
type and convert that to a SQL MAP
via a cast.
示例:
WITH
data(c) AS (
VALUES '{"foo": "baar", "foo1": "bar1"}'
),
parsed AS (
SELECT cast(json_parse(c) as map(varchar, varchar)) AS m
FROM data
)
SELECT m['foo'], m['foo1']
FROM parsed
产生:
_col0 | _col1
-------+-------
baar | bar1
这篇关于如何在Presto中将varchar转换为MAP(VARCHAR,VARCHAR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!