赞
踩
实际工作中,有时会出现map复杂数据类型,字段field1形式如:{‘k0’:‘abc’,‘k1’:‘01,02,03’,‘k2’:‘456’},如果是string形式,我们可以适用get_json_object 函数,取出对应的value值,具体如:get_json_object(field,‘$.k0’),即可取出值abc,如果是map数据类型,这个值该怎么取呢?
可以适用field[‘k0’]这种形式取出abc,顺便把map数据类型的常用方法做个总结;
params为字段名,数据类型为MAP类型;
select size(t.params);
>> 3
select map_keys(t.params);
>> ["k0","k1","k2"]
3.map_values(Map)函数:可得map中所有的value; 返回值类型: array
select map_value(t.params);
>> ["abc","01,02,03","456"]
4.判断map中是否包含某个key值:这种情况较为常见,多用于一些复杂逻辑判断中;
select array_contains(map_keys(t.params),'k0');
>> true
在k-v对中,若value有多个值的情况,如 {‘k1’:‘01,02,03’} ,如果要用 ‘k1’ 中 '02’作为过滤条件,则语句如下:
(这里用到split来处理)
select *
from t
where split(t.params['k1'],',')[1]
>> 02
6.如果过滤条件为:k2的值必须为’45’开头,则语句如下:
(这里用到substr方法来处理,这里注明一下,1和2分别表示起始位置和长度)
select *
from t
where substr(t.params['k2'],1,2) = '45'
with test_map_1 as ( select 1 as uid, map("key1", "value1","key2", "value2") as map1 union all select 2 as uid, map("key3", "value3","key4", "value4") as map1 ) --hive> select * from test_map_1; --OK --test_map_1.uid test_map_1.map1 --1 {"key1":"value1","key2":"value2"} --2 {"key3":"value3","key4":"value4"} select uid, concat('{"', regexp_replace(concat_ws(',', collect_list(concat_ws('":"', k,v) ) ),'\\,','\\"\\,\\"'), '"}') as string1 from test_map_1 lateral view outer explode(map1) kv as k,v group by uid ; select * from test_map_1_to_string; OK test_map_1_to_string.uid test_map_1_to_string.string1 1 {"key1":"value1","key2":"value2"} 2 {"key3":"value3","key4":"value4"} --如果大括号不需要,可以去掉最外层concat()函数; --如果不需要双引号"",则可以将concat_ws('":"', k,v)改为concat_ws(':', k,v)
有时一段复杂的sql,不仅会用到field[‘k0’]形式,还会用到get_json_object(field,‘ . k 0 ′ ),甚至会用到 g e t j s o n o b j e c t ( f i e l d [ ′ k 0 ′ ] , ′ .k0'),甚至会用到 get_json_object(field['k0'],' .k0′),甚至会用到getjsonobject(field[′k0′],′.kk’),待补充;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。