hive中array干系函数总结

[复制链接]
发表于 2026-2-6 04:13:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
hive官方函数表明

hive官网函数大全所在: hive官网函数大全所在
Return TypeNameDescriptionarrayarray(value1, value2, …)Creates an array with the given elements.booleanarray_contains(Array, value)Returns TRUE if the array contains value.arraysort_array(Array)Sorts the input array in ascending order according to the natural ordering of the array elements and returns it (as of version 0.9.0).Texplode(ARRAY a)Explodes an array to multiple rows. Returns a row-set with a single column (col), one row for each element from the array.int,Tposexplode(ARRAY a)Explodes an array to multiple rows with additional positional column of int type (position of items in the original array, starting with 0). Returns a row-set with two columns (pos,val), one row for each element from the array.示例

1、array(value1, value2, …)
创建一个 Array 对象,由一组值构成。
  1. SELECT array(85, 90, 78, 92, 86) AS scores;
  2. ---结果
  3. scores
  4. [85,90,78,92,86]
复制代码
2、array_contains(Array, value)
判断一个值是否在 Array 对象中存在。
  1. SELECT array_contains(array(85, 90, 78, 92, 86), 60) AS is_passed;
  2. ---结果
  3. is_passed
  4. false
  5. SELECT if(array_contains(array(85, 90, 78, 92, 86), 60),1,0) AS is_passed;
  6. ---结果
  7. is_passed
  8. 0
复制代码
3、sort_array(Array)
sort_array 函数可以用于对 Array 对象中的元素举行排序。具体来说,sort_array 函数会将 Array 对象中的元素按照升序序次举行排序,并返回一个新的排序后的 Array 对象。
  1. SELECT sort_array(array(85, 90, 78, 92, 86)) AS sorted_scores;
  2. ---结果
  3. sorted_scores
  4. [78,85,86,90,92]
复制代码
4、explode (array)
explode 函数可以用于将一个 Array 对象拆分成多行。具体来说,explode 函数会将 Array 对象中的每个元素拆分成一行,并与原始数据会合的其他字段一起返回。
  1. select explode(array('A','B','C'));
  2. select explode(array('A','B','C')) as col;
  3. select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf;
  4. select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf as col;
  5. ---上述四个结果均为
  6. col
  7. A
  8. B
  9. C
复制代码
5、posexplode (array)
  1. select posexplode(array('A','B','C'));
  2. select posexplode(array('A','B','C')) as (pos,val);
  3. select tf.* from (select 0) t lateral view posexplode(array('A','B','C')) tf;
  4. select tf.* from (select 0) t lateral view posexplode(array('A','B','C')) tf as pos,val;
  5. ---上述四个结果均为
  6. pos          col       
  7. 0             A       
  8. 1             B
  9. 2             C
复制代码
实战

下面整年级的学天生绩单的部门,包罗门生姓名,课程,分数,年级排名,要求将每人的结果举行汇总整理:按排名,课程和结果的序次
数据:
  1.     '小明' ,'语文'  ,80 as score , 100
  2.         
  3.     '小明' ,'数学'  ,90 as score,  85
  4.         
  5.     '小明' ,'英语'  ,75 as score,  203
  6.         
  7.     '小花' ,'语文'  ,85 as score , 90
  8.         
  9.     '小花' ,'数学'  ,65 as score , 350
  10.         
  11.     '小花' ,'英语'  ,90 as score , 20
复制代码
代码实现:
  1. with students as(
  2.     select '小明' as name,'语文' as course ,80 as score , 100 as rk
  3.         union all
  4.     select '小明' as name,'数学' as course ,90 as score,  85 as rk
  5.         union all
  6.     select '小明' as name,'英语' as course ,75 as score,  203 as rk
  7.         union all
  8.     select '小花' as name,'语文' as course ,85 as score , 90 as rk
  9.         union all
  10.     select '小花' as name,'数学' as course ,65 as score , 350 as rk
  11.         union all
  12.     select '小花' as name,'英语' as course ,90 as score , 20 as rk
  13. )
  14. SELECT name,
  15.        concat_ws(',',
  16.                  sort_array(
  17.                      collect_list(
  18.                          concat_ws(':',
  19.                                    lpad(cast(rk as string), 4, '0'),
  20.                                    course,
  21.                                    cast(score as string)
  22.                          )
  23.                      )
  24.                  )
  25.        ) AS sorted_scores
  26. FROM students
  27. GROUP BY name;
  28. ---结果
  29. name                          sorted_scores
  30. 小明                 0085:数学:90,0100:语文:80,0203:英语:75
  31. 小花                 0020:英语:90,0090:语文:85,0350:数学:65
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表