如何在Presto中将varchar转换为MAP(VARCHAR,VARCHAR) [英] How to cast varchar to MAP(VARCHAR,VARCHAR) in presto

查看:1956
本文介绍了如何在Presto中将varchar转换为MAP(VARCHAR,VARCHAR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中保存了一个表,名为( 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆