导航

  • ​​hive outline​​
  • ​​hive 集合函数​​
  • ​​判断集合大小函数-size​​
  • ​​取map的keys函数-map_keys​​
  • ​​取map的values函数-map_values​​
  • ​​判断数组是否包含指定元素函数-array_contains​​
  • ​​数组排序函数-sort_array​​
  • ​​列转行&去重函数-collect_set​​
  • ​​列转行&不去重函数collect_list​​

hive outline

​​链接​​

hive 集合函数

判断集合大小函数-size

select size(array(11,22,33));
-- 数出 3


select size(map("name","zhangsan","age",18));
-- 数出 2

取map的keys函数-map_keys

select map_keys(map("id",10086,"name","zhangsan","age",18));
-- 输出 ["id","name","age"]

取map的values函数-map_values

select map_values(map("id",10086,"name","zhangsan","age",18));
-- 输出 ["10086","zhangsan","18"]

判断数组是否包含指定元素函数-array_contains

select array_contains(array(11,22,33),11);
-- 输出 true

数组排序函数-sort_array

select sort_array(array(1,3,2));
-- 输出 [1,2,3]

列转行&去重函数-collect_set

表test

hive 集合函数_hive

select name,
collect_set(product_name)
from test
group by name;

name

_c1

张三

[“a”,“b”]

李四

[“a”,“b”]

​这个函数的缺点​​:去重以后,集合中的字段不能按照我们想要的顺序存放在数组中,而mysql中的group_concat()就能做到

​​栗子:查询没有学全所有课程的同学的信息​​

列转行&不去重函数collect_list

表test

hive 集合函数_hive

select name,
collect_list(product_name)
from test
group by name;

name

_c1

张三

[“a”,“a”,“b”]

李四

[“a”,“b”]