hive中array相关函数总结

hive官方函数解释

hive官网函数大全地址: hive官网函数大全地址

Return Type Name Description
array array(value1, value2, …) Creates an array with the given elements.
boolean array_contains(Array, value) Returns TRUE if the array contains value.
array sort_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).
T explode(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,T posexplode(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 对象,由一组值组成。

SELECT array(85, 90, 78, 92, 86) AS scores;
---结果
scores
[85,90,78,92,86]

2、array_contains(Array, value)
判断一个值是否在 Array 对象中存在。

SELECT array_contains(array(85, 90, 78, 92, 86), 60) AS is_passed;
---结果
is_passed
false

SELECT if(array_contains(array(85, 90, 78, 92, 86), 60),1,0) AS is_passed;
---结果
is_passed
0

3、sort_array(Array)
sort_array 函数可以用于对 Array 对象中的元素进行排序。具体来说,sort_array 函数会将 Array 对象中的元素按照升序顺序进行排序,并返回一个新的排序后的 Array 对象。

SELECT sort_array(array(85, 90, 78, 92, 86)) AS sorted_scores;
---结果
sorted_scores
[78,85,86,90,92]

4、explode (array)
explode 函数可以用于将一个 Array 对象拆分成多行。具体来说,explode 函数会将 Array 对象中的每个元素拆分成一行,并与原始数据集中的其他字段一起返回。

select explode(array('A','B','C'));
select explode(array('A','B','C')) as col;
select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf;
select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf as col;

---上述四个结果均为
col
A
B
C

5、posexplode (array)

select posexplode(array('A','B','C'));
select posexplode(array('A','B','C')) as (pos,val);
select tf.* from (select 0) t lateral view posexplode(array('A','B','C')) tf;
select tf.* from (select 0) t lateral view posexplode(array('A','B','C')) tf as pos,val;
---上述四个结果均为
pos          col	
0             A	
1             B
2             C

实战

下面全年级的学生成绩单的部分,包含学生姓名,课程,分数,年级排名,要求将每人的成绩进行汇总整理:按排名,课程和成绩的顺序
数据:

    '小明' ,'语文'  ,80 as score , 100 
        
    '小明' ,'数学'  ,90 as score,  85 
        
    '小明' ,'英语'  ,75 as score,  203 
        
    '小花' ,'语文'  ,85 as score , 90 
        
    '小花' ,'数学'  ,65 as score , 350 
        
    '小花' ,'英语'  ,90 as score , 20 

代码实现:

with students as(
    select '小明' as name,'语文' as course ,80 as score , 100 as rk
        union all 
    select '小明' as name,'数学' as course ,90 as score,  85 as rk
        union all 
    select '小明' as name,'英语' as course ,75 as score,  203 as rk
        union all 
    select '小花' as name,'语文' as course ,85 as score , 90 as rk
        union all 
    select '小花' as name,'数学' as course ,65 as score , 350 as rk
        union all 
    select '小花' as name,'英语' as course ,90 as score , 20 as rk
)
SELECT name, 
       concat_ws(',', 
                 sort_array(
                     collect_list(
                         concat_ws(':', 
                                   lpad(cast(rk as string), 4, '0'), 
                                   course, 
                                   cast(score as string) 
                         )
                     )
                 )
       ) AS sorted_scores
FROM students
GROUP BY name;
---结果
name                          sorted_scores
小明                 0085:数学:90,0100:语文:80,0203:英语:75
小花                 0020:英语:90,0090:语文:85,0350:数学:65

相关推荐

  1. hivearray相关函数总结

    2023-12-22 18:22:03       45 阅读
  2. hivemap相关函数总结

    2023-12-22 18:22:03       48 阅读
  3. hivestruct相关函数总结

    2023-12-22 18:22:03       43 阅读
  4. hivesplit函数相关总结

    2023-12-22 18:22:03       21 阅读
  5. Js Array相关总结

    2023-12-22 18:22:03       47 阅读
  6. Hive/SparkSQLMap、Array的基本使用和转换

    2023-12-22 18:22:03       40 阅读
  7. Hivearray,map,struct三种数据结构说明

    2023-12-22 18:22:03       11 阅读
  8. Hive函数 date_format 使用示例总结

    2023-12-22 18:22:03       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-22 18:22:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-22 18:22:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-22 18:22:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-22 18:22:03       20 阅读

热门阅读

  1. 自定义ORM(mybatis)源码(一)-解析config.xml

    2023-12-22 18:22:03       36 阅读
  2. Semaphore的简单使用

    2023-12-22 18:22:03       35 阅读
  3. flutter学习-day16-自定义组件

    2023-12-22 18:22:03       45 阅读
  4. P8736 [蓝桥杯 2020 国 B] 游园安排

    2023-12-22 18:22:03       31 阅读
  5. GO设计模式——21、观察者模式(行为型)

    2023-12-22 18:22:03       33 阅读
  6. 常用英文缩略词

    2023-12-22 18:22:03       41 阅读
  7. 矩阵秩的公式小结

    2023-12-22 18:22:03       36 阅读
  8. C复习-typedef相关

    2023-12-22 18:22:03       44 阅读
  9. 0.6 V LOW VOLTAGE SWING TERMINATED LOGIC (LVSTL06)

    2023-12-22 18:22:03       46 阅读
  10. golang:Excel写入mysql数据库

    2023-12-22 18:22:03       41 阅读
  11. windows 动态库和静态库 介绍

    2023-12-22 18:22:03       40 阅读