背景:
最近需要对业务系统数据进行ETL,供机器学习训练,我们需要对数据进行关联聚合,多行或多列数据转换为单行单列这种操作很常见;正好hive提供collect函数可以实现这种格式需求,我们在这整理下用法,顺便扩展一下~


一、collect_set 和 collect_list 函数

collect_set / collect_list(col)函数只接受基本数据类型,它的主要作用是将某字段的值汇总,产生Array类型字段,注意该函数只能接受一列参数!我们一般都会配合group by 函数,直接汇总分组数据!

collect函数也可以实现一个功能:获取group by后面没有的字段!

collect_set 和 collect_list 函数的区别很直观,set会进行去重,而list不会!

测试数据:

select month,day,collect_set(cookieid) from test2 group by month,day

可以看到,结果字段为数组类型!
我们可以通过下标去获取数组中的数据:

select month,day,collect_set(cookieid)[0] c0 from test2 group by month,day

select month,day,collect_set(cookieid)[1] c1 from test2 group by month,day

从上面结果可以看到,取值时如果下标超出范围,会返回NULL

collect_list 函数 除了不去重之外,其他特性和set一模一样:

select month,day,collect_list(cookieid) cl from test2 group by month,day

select month,day,collect_list(cookieid)[0] cl0 from test2 group by month,day


二、扩展:concat / concat_ws 函数

如果想按指定格式合并分组内全部行记录怎么处理?

而hive的collect函数只能接受一个参数,无法像mysqlgroup_concat函数一样能直接多列随意合并,所以我们必须通过hive的concat函数先指定格式合并再collect!

select concat('a','b') c from test2

select concat('a','b',NULL) c from test2


可以看到,直接用concat函数,如果其中某一列为NULL,那么合并结果也为NULL,这个时候,我们需要concat_ws函数:

concat_ws 函数第一个参数为指定合并分隔符:

select concat_ws(',','a','b') cw from test2

select concat_ws(',','a','b',NULL,'c') cw1 from test2

NULL字段合并对于concat_ws来说无影响!

我们用collect函数合并时结果字段为数组形式,很多时候我们并不希望是这种格式,所以可以通过concat_ws去改变:

select month,day,concat_ws(',',collect_list(cookieid)) cw from test2 group by month,day

或者这种格式需求:

select month,day,collect_list(concat(cookieid,'|',day)) cl  from test2 group by month,day


三、Explode 函数

官方解释:
explode()接收一个数组(或一个map)作为输入并将数组(map)的元素作为单独的行输出;


可以看到,explode函数的参数只能是数组或map类型

准备测试数据:

HDFS文件(字段分隔符为/t):

a   b   1,2,3   money:10,age:30
c   d   4,5,6   money:20,age:45

创建外部表:

create external table test3 (name1 string,name2 string,id array<int>,content map<string,int>
)
row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
location '/yj/test'

测试该函数用法:

数组字段进行explode:

select explode(id) e from test3

map字段进行explode:

select explode(content) e from test3


报错!发现取别名失败了!原来content字段为map类型,explode后会变成两列,所以必须取两个别名~

select explode(content) from test3

map类型默认字段名为key,value

select explode(content) as (mykey,myvalue) from test3


取两个别名要加括号哦~

重点来了:

select name1,name2,explode(id) e from test3         (报错)


错误信息如下:
Error while compiling statement: FAILED: SemanticException [Error 10081]:UDTF's are not supported outside the SELECT clause, nor nested in expressions

这就涉及到了explode函数作为UDTF类型函数的基本特性了:

UDTF一般有两种使用方法,一种直接放到select后面一种和lateral view一起使用

1:直接select中使用时:

不可以添加其他字段使用:

select a, explode_map(properties) as (col1,col2) from src(这就是上面报错的原因!)

不可以嵌套调用:

select explode_map(explode_map(properties)) from src

不可以和group by/cluster by/distribute by/sort by一起使用:

select explode_map(properties) as (col1,col2) from src group by col1, col2

2:和lateral view一起使用:

select src.id, mytable.col1, mytable.col2 from src lateral view explode_map(properties) mytable as col1, col2;

那么问题来了,lateral view 到底是个什么东东?


四、lateral view 详解

以下内容为 hive官方 wiki:wiki地址

Lateral View Syntax

lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)*
fromClause: FROM baseTable (lateralView)*

注意语法:比一般函数多了个虚拟表别名

Description

Lateral view is used in conjunction with user-defined table generating functions such as explode(). As mentioned in Built-in Table-Generating Functions, a UDTF generates zero or more output rows for each input row. A lateral view first applies the UDTF to each row of base table and then joins resulting output rows to the input rows to form a virtual table having the supplied table alias.

翻译:
Lateral view与用户定义的表生成函数(如explode())一起使用。正如在内置的表生成函数中提到的,UDTF为每个输入行生成零个或多个输出行。Lateral view首先将UDTF应用于基表的每一行,然后将产生的输出行连接到输入行,形成一个具有提供的表别名的虚拟表。

注:Lateral View通常和UDTF一起出现,为了解决UDTF不允许在select字段的问题

Multiple Lateral Views
A FROM clause can have multiple LATERAL VIEW clauses. Subsequent LATERAL VIEWS can reference columns from any of the tables appearing to the left of the LATERAL VIEW.

Multiple Lateral View 可以实现类似笛卡尔乘积

SELECT * FROM exampleTable
LATERAL VIEW explode(col1) myTable1 AS myCol1
LATERAL VIEW explode(myCol1) myTable2 AS myCol2;

Outer Lateral Views
The user can specify the optional OUTER keyword to generate rows even when a LATERAL VIEW usually would not generate a row. This happens when the UDTF used does not generate any rows which happens easily with explode when the column to explode is empty. In this case the source row would never appear in the results. OUTER can be used to prevent that and rows will be generated with NULL values in the columns coming from the UDTF.

翻译:
即使横向视图通常不会生成一行,用户也可以指定可选的OUTER关键字来生成行。当使用的UDTF不生成任何行时,就会发生这种情况。在这种情况下,源行永远不会出现在结果中。可以使用OUTER来防止这种情况,并在来自UDTF的列中使用空值生成行。

Outer关键字可以把不输出的UDTF的空结果,输出成NULL,防止丢失数据

测试:

select name1,name2,exid from test3 LATERAL VIEW explode(id) idtable as exid

Hive collect、explode函数详解(包括concat、Lateral View)相关推荐

  1. LATERAL VIEW EXPLODE函数详解及应用

    需求背景:在进行统计分析的时候有时候会有类似这样的需求 比如求某个平台某一天所有的订单总和,或者淘宝所有pc 端的交易总和,这个时候我们可以基于原本基础的数据进行炸裂处理之后得出结结果值,方便后续进行 ...

  2. hive内置函数详解

    为什么80%的码农都做不了架构师?>>>    内置函数 2.1数学函数 返回类型 函数 说明 BIGINT round(double a) 四舍五入 DOUBLE round(do ...

  3. Hive SQL开窗函数详解

    Hive 开窗函数 group by 是分组函数,一组出来一个数据 over() 开窗,针对每一条数据,都有一个独立的组 mk 3 jk 3 mk 3 select orderdate,cost,su ...

  4. 5、Hive函数详解与案例实战

    1.Hive系统内置函数 1.1.数值计算函数 1.取整函数: round 语法: round(double a) 返回值: BIGINT 说明: 返回double类型的整数值部分 (遵循四舍五入) ...

  5. Spark: sortBy和sortByKey函数详解

    在很多应用场景都需要对结果数据进行排序,Spark中有时也不例外.在Spark中存在两种对RDD进行排序的函数,分别是 sortBy和sortByKey函数.sortBy是对标准的RDD进行排序,它是 ...

  6. blankcount函数python,Python pandas常用函数详解

    本文研究的主要是pandas常用函数,具体介绍如下. 1 import语句 2 文件读取 df = pd.read_csv(path='file.csv') 参数:header=None 用默认列名, ...

  7. 【ES6】Generator函数详解

    [ES6]Generator函数详解 一.Generator函数简介 基本概念 函数写法 yield关键字介绍 二.next方法的参数 三.for...of循环 四.关于普通throw()与Gener ...

  8. 内核中的kmalloc函数详解

    一.kmalloc函数详解 #include <linux/slab.h> void *kmalloc(size_t size, int flags); 给 kmalloc 的第一个参数是 ...

  9. Matlab中画图以及plot函数及legend函数详解

    Matlab中plot函数及legend函数详解 Matlab中plot函数全功能解析Matlab中plot函数及legend函数详解 功能 二维曲线绘图 语法 plot(Y) plot(X1,Y1, ...

  10. Matlab中plot函数及legend函数详解

    Matlab中plot函数及legend函数详解 Matlab中plot函数及legend函数详解 Matlab中plot函数全功能解析 功能 二维曲线绘图 语法 plot(Y) plot(X1,Y1 ...

最新文章

  1. 综述|基于深度学习方式的场景分类算法
  2. NA-NP-IE系列实验之前三个实验小结
  3. K-means聚类自定义距离计算
  4. 剖析Vue原理实现双向绑定MVVM
  5. YARN的内存和CPU配置优化
  6. ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动
  7. RobotFramework自动化框架—数据驱动测试
  8. c语言 字符串 正序再倒序_python字符串
  9. 软件工程结对项目:四则运算web
  10. 在JavaScript中将字符转换为ASCII代码
  11. 【重点】剑指offer——面试题25:二叉树中和为某一值的路径
  12. 镜像下载cudnn+tensorflow
  13. 科学计算机带度分秒,科学计算器度分秒
  14. 自有项目Iframe嵌入ThingJS物联网可视化项目代码解析
  15. Centos7 安装奔跑的小火车
  16. 正负筛选(neo正向+HSV-tk负向)原理
  17. poj 2187 凸包or旋转qia壳法
  18. H5C3进阶——播放器
  19. linux优麒麟iso镜像,优麒麟 Linux x64 17.04
  20. win10 设定计划任务时提示所指定的账户名称无效,如何解决?

热门文章

  1. Springboot毕设项目公共机房的值班管理系统wyz7b(java+VUE+Mybatis+Maven+Mysql)
  2. html静态网页设计实训总结,html网页设计总结 html静态网页设计大作业
  3. php中的gd图像处理,PHP图像处理(GD库)
  4. 安卓10源码开发定制(30)screencap命令源码分析
  5. MATLAB 常用语法、函数
  6. JAVA中利用Docx4J组件操作word文档,进行docx格式文档的创建、写入、读取、转换html、图片处理示例、转换pdf
  7. 成功解决生意参谋中transit-id和加密数据date
  8. android动画送礼物,【Android】直播App礼物弹窗及连送礼物动画
  9. 一个简单的爬虫例子(代码)
  10. oracle pmon 多长时间,oracle 11g pmon工作内容系列一