hive函数之~hive当中的lateral view 与 explode

摘要:
其中explode还可以用于将hive一列中复杂的array或者map结构拆分成多行。

1、使用explode函数将hive表中的Map和Array字段数据进行拆分

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

其中explode还可以用于将hive一列中复杂的array或者map结构拆分成多行。

需求:现在有数据格式如下

zhangsan     child1,child2,child3,child4      k1:v1,k2:v2
lisi  child5,child6,child7,child8      k3:v3,k4:v4

字段之间使用 分割,需求将所有的child进行拆开成为一列

+----------+--+
| mychild  |
+----------+--+
| child1   |
| child2   |
| child3   |
| child4   |
| child5   |
| child6   |
| child7   |
| child8   |
+----------+--+

将map的key和value也进行拆开,成为如下结果

+-----------+-------------+--+
| mymapkey  | mymapvalue  |
+-----------+-------------+--+
| k1        | v1          |
| k2        | v2          |
| k3        | v3          |
| k4        | v4          |
+-----------+-------------+--+

第一步:创建hive数据库

创建hive数据库

hive (default)> create databasehive_explode;
hive (default)> use hive_explode;

第二步:创建hive表,然后使用explode拆分map和array

hive (hive_explode)> create  table t3(name string,children array<string>,address Map<string,string>)
                    row format delimited fields terminated by ''collection items terminated by ','map keys terminated by ':' stored as textFile;

第三步:加载数据

node03执行以下命令创建表数据文件

mkdir -p /export/servers/hivedatas/cd /export/servers/hivedatas/vim maparray

zhangsan     child1,child2,child3,child4      k1:v1,k2:v2
lisi  child5,child6,child7,child8      k3:v3,k4:v4

hive表当中加载数据

hive (hive_explode)> load data local inpath '/export/servers/hivedatas/maparray' into table t3;

第四步:使用explode将hive当中数据拆开

将array当中的数据拆分开

hive (hive_explode)> SELECT explode(children) AS myChild FROM t3;

将map当中的数据拆分开

hive (hive_explode)> SELECT explode(address) AS (myMapKey, myMapValue) FROM t3;

2、使用explode拆分json字符串

需求:现在有一些数据格式如下:

a:shandong,b:beijing,c:hebei|1,2,3,4,5,6,7,8,9|[{"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"},{"source":"jd","monthSales":2090,"userCount":78981,"score":"9.8"},{"source":"jdmart","monthSales":6987,"userCount":1600,"score":"9.0"}]

其中字段与字段之间的分隔符是 |

我们要解析得到所有的monthSales对应的值为以下这一列(行转列)

4900
2090
6987

第一步:创建hive表

hive (hive_explode)> create tableexplode_lateral_view
                   (`area` string,
                   `goods_id` string,
                   `sale_info` string)
                   ROW FORMAT DELIMITED
                   FIELDS TERMINATED BY '|'STORED AS textfile;

第二步:准备数据并加载数据

准备数据如下

cd /export/servers/hivedatas
vim explode_json
a:shandong,b:beijing,c:hebei|1,2,3,4,5,6,7,8,9|[{"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"},{"source":"jd","monthSales":2090,"userCount":78981,"score":"9.8"},{"source":"jdmart","monthSales":6987,"userCount":1600,"score":"9.0"}]

加载数据到hive表当中去

hive (hive_explode)> load data local inpath '/export/servers/hivedatas/explode_json' overwrite into table explode_lateral_view;

第三步:使用explode拆分Array

hive (hive_explode)> select explode(split(goods_id,',')) as goods_id from explode_lateral_view;

第四步:使用explode拆解Map

hive (hive_explode)> select explode(split(area,',')) as area from explode_lateral_view;

5.创建hive表并导入数据

创建hive表并加载数据

hive (hive_explode)> create tableperson_info(
                    name string,
                    constellation string,
                    blood_type string)
                    row format delimited fields terminated by"	";
加载数据
hive (hive_explode)> load data local inpath '/export/servers/hivedatas/constellation.txt' into table person_info;

第五步:拆解json字段

hive (hive_explode)> select explode(split(regexp_replace(regexp_replace(sale_info,'\[\{',''),'}]',''),'},\{')) as  sale_info from explode_lateral_view;

然后我们想用get_json_object来获取key为monthSales的数据:

hive (hive_explode)> select get_json_object(explode(split(regexp_replace(regexp_replace(sale_info,'\[\{',''),'}]',''),'},\{')),'$.monthSales') as  sale_info from explode_lateral_view;

然后挂了FAILED: SemanticException [Error 10081]: UDTF's are not supported outside the SELECT clause, nor nested in expressions

UDTF explode不能写在别的函数内

如果你这么写,想查两个字段,select explode(split(area,',')) as area,good_id from explode_lateral_view;

会报错FAILED: SemanticException 1:40 Only a single expression in the SELECT clause is supported with UDTF's. Error encountered near token 'good_id'

使用UDTF的时候,只支持一个字段,这时候就需要LATERAL VIEW出场了

3、配合LATERAL VIEW使用

配合lateral view查询多个字段

hive (hive_explode)> select goods_id2,sale_info from explode_lateral_view LATERAL VIEW explode(split(goods_id,','))goods as goods_id2;

其中LATERAL VIEW explode(split(goods_id,','))goods相当于一个虚拟表,与原表explode_lateral_view笛卡尔积关联。

也可以多重使用

hive (hive_explode)> selectgoods_id2,sale_info,area2
                    fromexplode_lateral_view
                    LATERAL VIEW explode(split(goods_id,','))goods asgoods_id2
                    LATERAL VIEW explode(split(area,','))area asarea2;
也是三个表笛卡尔积的结果

最终,我们可以通过下面的句子,把这个json格式的一行数据,完全转换成二维表的方式展现

hive (hive_explode)> select get_json_object(concat('{',sale_info_1,'}'),'$.source') assource,
                    get_json_object(concat('{',sale_info_1,'}'),'$.monthSales') asmonthSales,
                    get_json_object(concat('{',sale_info_1,'}'),'$.userCount') asmonthSales,
                    get_json_object(concat('{',sale_info_1,'}'),'$.score') as monthSales fromexplode_lateral_view
                    LATERAL VIEW explode(split(regexp_replace(regexp_replace(sale_info,'\[\{',''),'}]',''),'},\{'))sale_info as sale_info_1;

总结:

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

免责声明:文章转载自《hive函数之~hive当中的lateral view 与 explode》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mui实现切换选项卡JAVA 容器下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

HadoopDB:混合分布式系统

HadoopDB 是一个 Mapreduce 和传统关系型数据库的结合方案,以充分利用 RDBMS 的性能和 Hadoop 的容错、分布特性。2009 年被 Yale 大学教授 Abadi 提出,继而商业化为 Hadapt,据称从 VC 那儿拉到了 10M 刀投资。 本文是对 HadoopDB 论文的总结。其中不免掺杂些自己的不成熟想法,更详细的内容,还...

Hive 时间函数

--Hive中日期函数总结: --1.时间戳函数 --日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数 select unix_timestamp(); --获得当前时区的UNIX时间戳 select unix_timestamp('2017-09-15 14:23:00');  select unix_timestamp('...

Spark在美团的实践

https://tech.meituan.com/2016/03/31/spark-in-meituan.html 本文已发表在《程序员》杂志2016年4月期。 前言 美团是数据驱动的互联网服务,用户每天在美团上的点击、浏览、下单支付行为都会产生海量的日志,这些日志数据将被汇总处理、分析、挖掘与学习,为美团的各种推荐、搜索系统甚至公司战略目标制定提供数据支...

hive函数之~集合统计函数

1、个数统计函数: count *** 语法: count(*), count(expr), count(DISTINCT expr[, expr_.])返回值: int说明: count(*)统计检索出的行的个数,包括NULL值的行;count(expr)返回指定字段的非空值的个数;count(DISTINCT expr[, expr_.])返回指定字段...

Hive 基础

Facebook为了解决海量日志数据的分析而开发了hive,后来开源给了Apache基金会组织。 hive是一种用SQL语句来协助读写、管理存储在HDFS上的大数据集的数据仓库软件。 Hive 特点 1 是基于 Hadoop 的一个数据仓库工具;2 Hive 最大的特点是将 Hive SQL语句转换为 MapReduce、Tez 或者 spark 等任务执...

hive 时间戳函数之unix_timestamp,from_unixtime

一. 日期>>>>时间戳1.unix_timestamp() 获取当前时间戳 例如:select unix_timestamp() --1565858389 2.unix_timestamp(string timestame) 输入的时间戳格式必须为'yyyy-MM-dd HH:mm:ss',如不符合则返回null 例如...