目录
- hive官方函数解释
- 示例
- 实战
hive官方函数解释
hive官网函数大全地址: hive官网函数大全地址
Return Type Name Description map map(key1, value1, key2, value2, …) Creates a map with the given key/value pairs. array map_values(Map ) Returns an unordered array containing the values of the input map. array map_keys(Map ) Returns an unordered array containing the keys of the input map. map str_to_map(text[, delimiter1, delimiter2]) Splits text into key-value pairs using two delimiters. Delimiter1 separates text into K-V pairs, and Delimiter2 splits each K-V pair. Default delimiters are ‘,’ for delimiter1 and ‘:’ for delimiter2. Tkey,Tvalue explode(MAP m) Explodes a map to multiple rows. Returns a row-set with a two columns (key,value) , one row for each key-value pair from the input map. (As of Hive 0.8.0.). 示例
1、map(key1, value1, key2, value2, …)
SELECT map('name', '张三', 'age', 20, 'gender', '男') AS student; ---结果: student {"age":"20","gender":"男","name":"张三"}
2、map_values(Map
) SELECT map_keys(map('name', '张三', 'age', 20, 'gender', '男')) AS keys; ---结果: keys ["name","age","gender"]
3、map_values(Map
) SELECT map_values(map('name', '张三', 'age', 20, 'gender', '男')) AS values; ---结果: values ["张三","20","男"]
4、str_to_map(str, delimiter1, delimiter2)
str_to_map 函数用于将一个字符串转换为 Map 对象。具体来说,str_to_map 函数会将一个由键值对组成的字符串解析成一个 Map 对象,其中键和值之间使用指定的分隔符进行分隔。其中,str 是要转换的字符串,delimiter1 是键值对之间的分隔符,delimiter2 是键和值之间的分隔符。默认情况下,delimiter1 的值是 ‘,’,delimiter2 的值是 ‘:’。
SELECT str_to_map('name:张三,age:20,gender:男', ',', ':') AS student; ---结果: student {"age":"20","gender":"男","name":"张三"} SELECT str_to_map('name=张三,age=20,gender=男', ',', '=') AS student; ---结果: student {"age":"20","gender":"男","name":"张三"}
5、explode (map)
select explode(map('A',10,'B',20,'C',30)); select explode(map('A',10,'B',20,'C',30)) as (key,value); select tf.* from (select 0) t lateral view explode(map('A',10,'B',20,'C',30)) tf; select tf.* from (select 0) t lateral view explode(map('A',10,'B',20,'C',30)) tf as key,value; ---上述四个结果均为: key value A 10 B 20 C 30
实战
给出一组学生数据,有名字,课程,等级,分数等字段,现在求每门课的情况,包含平均成绩,及这门课包含哪些学生及学生的等级
with stud as ( select 'zhang3' as name ,'优' as grade ,'math' as course ,'88' as score union all select 'li4' as name ,'良' as grade ,'math' as course ,'72' as score union all select 'zhao6' as name ,'差' as grade ,'math' as course ,'44' as score union all select 'wang5' as name ,'优' as grade ,'chinese' as course ,'80' as score union all select 'zhao6' as name ,'优' as grade ,'chinese' as course ,'55' as score union all select 'tian7' as name ,'优' as grade ,'chinese' as course ,'75' as score ) --sql1 select course, collect_set(concat(name,':',grade)) as collect , avg(score) from stud group by course; ---结果: course collect avg(score) math ["li4:良","zhao6:差","zhang3:优"] 68.0 chinese ["wang5:优","tian7:优","zhao6:优"] 70.0 ----sql2 select course, concat_ws(',',collect_set(concat(name,':',grade))) as strings , avg(score) from stud group by course; ---结果: course strings avg(score) math li4:良,zhao6:差,zhang3:优 68.0 chinese wang5:优,tian7:优,zhao6:优 70.0 ----sql3 select course, str_to_map(concat_ws(',',collect_set(concat(name,':',grade))),',',':') as maps , avg(score) from stud group by course; ---结果: course maps avg(score) math {"li4":"良","zhang3":"优","zhao6":"差"} 68.0 chinese {"tian7":"优","wang5":"优","zhao6":"优"} 70.0
注意:
第一种sql,collect 字段的类型是array;第二种sql,strings字段的类型是string;第三种sql,maps字段的类型是map;
问题来了,能否在第二种的基础上,实现第一种和第三种的结果,且字段类型是string;
下面实现第二种转化为第三种,实际上就是map格式转换成json字符串;
with stud as ( select 'zhang3' as name ,'优' as grade ,'math' as course ,'88' as score union all select 'li4' as name ,'良' as grade ,'math' as course ,'72' as score union all select 'zhao6' as name ,'差' as grade ,'math' as course ,'44' as score union all select 'wang5' as name ,'优' as grade ,'chinese' as course ,'80' as score union all select 'zhao6' as name ,'优' as grade ,'chinese' as course ,'55' as score union all select 'tian7' as name ,'优' as grade ,'chinese' as course ,'75' as score ) select course ,concat('{"',string2,'"}') as string3 from (select course ,regexp_replace(string1,'\\,','\\"\\,\\"') as string2 from ( select course, concat_ws(',', collect_list(concat_ws('":"', k,v) ) ) as string1 from ( select course, str_to_map(concat_ws(',',collect_set(concat(name,':',grade))),',',':') as maps , avg(score) from stud group by course )test_map_1 lateral view outer explode(maps) kv as k,v group by course ) tt ) tm ---结果: course string3 math {"li4":"良","zhang3":"优","zhao6":"差"} chinese {"tian7":"优","wang5":"优","zhao6":"优"}
猜你喜欢
- 4天前(大理悦云雅阁酒店电话)雅阁酒店集团|端午佳节礼遇,大理悦云雅阁度假酒店
- 4天前(三亚海棠湾君悦度假酒店)三亚海棠湾君悦酒店暑期夏令营悦趣海岛游招募中
- 4天前(江西启动“唱游江西”计划)江西启动“唱游江西”计划
- 4天前(瑞虎7plus2021款)重塑10万级SUV价值标杆,全新一代瑞虎7PLUS冠军版给你惊喜
- 4天前(东北地区全域旅游)东北三省一区宣传贯彻研学旅游行业标准
- 4天前(新西兰“空降”上海:新西兰旅游局邀请你来“玩真的”!)新西兰“空降”上海:新西兰旅游局邀请你来“玩真的”!
- 4天前(殷建祥简历)全国十大牛商解码:殷建祥如何用178天技术突围打造星空梦星空房
- 4天前(冬日生活还没安排?上抖音一键打包北方花式过冬精彩)冬日生活还没安排?上抖音一键打包北方花式过冬精彩
- 4天前(曹妃甸美仑华府哪个楼层好)曹妃甸新城教育经济新引擎启动—美仑国际酒店盛大开业
- 4天前(海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章