hive lateral view 与 explode详解

项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

1.explode


hive wiki对于expolde的解释如下:

explode() takes in an array (or a map) as an input and outputs the elements of the array (map) as separate rows. UDTFs can be used in the SELECT expression list and as a part of LATERAL VIEW.
As an example of using explode() in the SELECT expression list, consider a table named myTable that has a single column (myCol) and two rows:

 Then running the query:

SELECT explode(myCol) AS myNewCol FROM myTable;

will produce:

 The usage with Maps is similar:

SELECT explode(myMap) AS (myMapKey, myMapValue) FROM myMapTable;

总结起来一句话:explode就是将hive一行中复杂的array或者map结构拆分成多行。

使用实例:
xxx表中有一个字段mvt为string类型,数据格式如下:

[{“eid”:“38”,“ex”:“affirm_time_Android”,“val”:“1”,“vid”:“31”,“vr”:“var1”},{“eid”:“42”,“ex”:“new_comment_Android”,“val”:“1”,“vid”:“34”,“vr”:“var1”},{“eid”:“40”,“ex”:“new_rpname_Android”,“val”:“1”,“vid”:“1”,“vr”:“var1”},{“eid”:“19”,“ex”:“hotellistlpage_Android”,“val”:“1”,“vid”:“1”,“vr”:“var01”},{“eid”:“29”,“ex”:“bookhotelpage_Android”,“val”:“0”,“vid”:“1”,“vr”:“var01”},{“eid”:“17”,“ex”:“trainMode_Android”,“val”:“1”,“vid”:“1”,“vr”:“mode_Android”},{“eid”:“44”,“ex”:“ihotelList_Android”,“val”:“1”,“vid”:“36”,“vr”:“var1”},{“eid”:“47”,“ex”:“ihotelDetail_Android”,“val”:“0”,“vid”:“38”,“vr”:“var1”}]

用explode小试牛刀一下:

select explode(split(regexp_replace(mvt,'\\[|\\]',''),'\\},\\{')) from ods_mvt_hourly where day=20160710 limit 10;

最后出来的结果如下:

{“eid”:“38”,“ex”:“affirm_time_Android”,“val”:“1”,“vid”:“31”,“vr”:“var1”
“eid”:“42”,“ex”:“new_comment_Android”,“val”:“1”,“vid”:“34”,“vr”:“var1”
“eid”:“40”,“ex”:“new_rpname_Android”,“val”:“1”,“vid”:“1”,“vr”:“var1”
“eid”:“19”,“ex”:“hotellistlpage_Android”,“val”:“1”,“vid”:“1”,“vr”:“var01”
“eid”:“29”,“ex”:“bookhotelpage_Android”,“val”:“0”,“vid”:“1”,“vr”:“var01”
“eid”:“17”,“ex”:“trainMode_Android”,“val”:“1”,“vid”:“1”,“vr”:“mode_Android”
“eid”:“44”,“ex”:“ihotelList_Android”,“val”:“1”,“vid”:“36”,“vr”:“var1”
“eid”:“47”,“ex”:“ihotelDetail_Android”,“val”:“0”,“vid”:“38”,“vr”:“var1”}
{“eid”:“38”,“ex”:“affirm_time_Android”,“val”:“1”,“vid”:“31”,“vr”:“var1”
“eid”:“42”,“ex”:“new_comment_Android”,“val”:“1”,“vid”:“34”,“vr”:“var1”

2.lateral view


hive 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.

Example


Consider the following base table named pageAds. It has two columns: pageid (name of the page) and adid_list (an array of ads appearing on the page)

An example table with two rows: 

and the user would like to count the total number of times an ad appears across all pages.
A lateral view with explode() can be used to convert adid_list into separate rows using the query:
 

SELECT pageid, adid
FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid;

The resulting output will be

 Then in order to count the number of times a particular ad appears, count/group by can be used:

SELECT adid, count(1)
FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid
GROUP BY adid;

The resulting output will be

 

lateral view用于和split, explode等UDTF一起使用,它能够将一行数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合。lateral view首先为原始表的每行调用UDTF,UDTF会把一行拆分成一或者多行,lateral view再把结果组合,产生一个支持别名表的虚拟表。

由此可见,lateral view与explode等udtf就是天生好搭档,explode将复杂结构一行拆成多行,然后再用lateral view做各种聚合。

3.实例

还是第一部分的例子,上面我们explode出来以后的数据,不是标准的json格式,我们通过lateral view与explode组合解析出标准的json格式数据:

SELECT ecrd, CASE WHEN instr(mvtstr,'{')=0
    AND instr(mvtstr,'}')=0 THEN concat('{',mvtstr,'}') WHEN instr(mvtstr,'{')=0
    AND instr(mvtstr,'}')>0 THEN concat('{',mvtstr) WHEN instr(mvtstr,'}')=0
    AND instr(mvtstr,'{')>0 THEN concat(mvtstr,'}') ELSE mvtstr END AS mvt
      FROM ods.ods_mvt_hourly LATERAL VIEW explode(split(regexp_replace(mvt,'\\[|\\]',''),'\\},\\{')) addTable AS mvtstr
        WHERE DAY='20160710' and ecrd is not null limit 10

查询出来的结果:
xxx
{“eid”:“38”,“ex”:“affirm_time_Android”,“val”:“1”,“vid”:“31”,“vr”:“var1”}
xxx
{“eid”:“42”,“ex”:“new_comment_Android”,“val”:“1”,“vid”:“34”,“vr”:“var1”}
xxx
{“eid”:“40”,“ex”:“new_rpname_Android”,“val”:“1”,“vid”:“1”,“vr”:“var1”}
xxx
{“eid”:“19”,“ex”:“hotellistlpage_Android”,“val”:“1”,“vid”:“1”,“vr”:“var01”}
xxx
{“eid”:“29”,“ex”:“bookhotelpage_Android”,“val”:“0”,“vid”:“1”,“vr”:“var01”
xxx
{“eid”:“17”,“ex”:“trainMode_Android”,“val”:“1”,“vid”:“1”,“vr”:“mode_Android”}
xxx
{“eid”:“44”,“ex”:“ihotelList_Android”,“val”:“1”,“vid”:“36”,“vr”:“var1”}
xxx
{“eid”:“47”,“ex”:“ihotelDetail_Android”,“val”:“1”,“vid”:“38”,“vr”:“var1”}
xxx
{“eid”:“38”,“ex”:“affirm_time_Android”,“val”:“1”,“vid”:“31”,“vr”:“var1”}
xxx
{“eid”:“42”,“ex”:“new_comment_Android”,“val”:“1”,“vid”:“34”,“vr”:“var1”}

4.Ending


Lateral View通常和UDTF一起出现,为了解决UDTF不允许在select字段的问题。
Multiple Lateral View可以实现类似笛卡尔乘积。
Outer关键字可以把不输出的UDTF的空结果,输出成NULL,防止丢失数据。

参考内容:


1.http://blog.csdn.net/oopsoom/article/details/26001307 lateral view的用法实例
2.https://my.oschina.net/leejun2005/blog/120463 复合函数的用法,比较详细
3.http://blog.csdn.net/zhaoli081223/article/details/46637517 udtf的介绍
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Hive 中的 lateral view explode 是用来将一个表中的一列数组类型的数据拆分成多行,每一行对应数组中的一个元素。这样可以方便地对数组中的元素进行计算和分析。使用方法如下: ``` SELECT ... FROM table_name LATERAL VIEW explode(array_column_name) exploded_table_alias AS column_alias ``` 其中 array_column_name 是数组类型的列名,exploded_table_alias 是拆分后的表的别名,column_alias 是拆分后新增的列的别名。 ### 回答2: Hive Lateral View ExplodeHive 的一种语法,可以将数组或者 Map 类型的列,展开成多行数据。Lateral View 关键字是用来处理嵌套类型的数据,而 Explode 函数可以将数组或 Map 类型的列展开成多行数据,每一行包含一组键值对或值。这个语法语句会将每个数组或 Map 对象分解成独立的行,其中包含了键和值。这样对于数组或 Map 中的数据就可以进行分组、过滤、排序等操作,这种方式可以方便的处理多类型数据。 它的基本语法如下: select a.id, b.device_name from user_info a lateral view explode(a.devices) b as device_name; 这个语句的主要作用是将 user_info 表中的 devices 列展开成多行数据,每行数据包含一个用户设备名称和设备id。此时,可以通过 device_name 字段来分组、过滤、聚合等操作,从而方便地对数据进行处理。Lateral ViewExplode 结合使用,可以方便的处理复杂数据类型,挖掘数据更深层次的信息,使数据分析更加高效、方便、精准。 总的来说,Hive Lateral View Explode 能够方便地将数组或 Map 类型的列进行展开,使得数据处理更加方便和高效。使用它可以应对各种统计需求,分析更加深入有效。同时也要注意到 Lateral View Explode 这种语法可能会影响计算效率,因此在实际的使用中需要仔细设计和优化查询语句,以提高计算效率。 ### 回答3: Hive Lateral View Explode 是在 Hive 中用于将一个数组或 Map 字段转换为行的扩展函数。它通过创建新行来展开数组或 Map 字段中的每个元素,以便更容易地进行查询和分析。 在 Hive 中,数组和 Map 字段是常见的数据类型,用于存储多个值或键值对。但是,这些类型常常难以查询和分析,因为它们不能直接展开成一些行。Lateral View Explode 解决了这个问题。它允许将数组和 Map 字段展开成多行,这样就可以更轻松地进行分析和查询。 使用 Lateral View Explode 函数,可以将数组字段展开成多行,每行包含数组中的一个元素。例如,假设有一个包含成绩的数组字段 grades,它包含多个数字。使用 Lateral View Explode 函数,可以将 grades 字段展开成新的一组行,每一行包含一个成绩。 另外,Lateral View Explode 也可用于 Map 字段,使每个键值对都展开成一行。Lateral View Explode 还可以与其他 Hive 函数结合使用,以进一步扩展查询。 总之,Lateral View ExplodeHive 中一个重要的扩展函数,它可将数组和 Map 字段转换成行,方便查询和分析。使用 Lateral View Explode 函数可以大大简化数据分析的过程,提高查询效率,并使数据更加有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值