前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hive lateral view 和 explode 详解

Hive lateral view 和 explode 详解

作者头像
北漂的我
发布2019-05-29 10:53:03
1.4K0
发布2019-05-29 10:53:03
举报
文章被收录于专栏:北漂的我北漂的我

1. 建表语句结构

代码语言:javascript
复制
create table if not exists employees ( 
name         string, 
salary       float, 
subordinates array<string>, 
deductions   map<string, float>, 
address      struct<street:string, city:string, state:string, zip:int> 
) 
row format delimited 
fields terminated by '\001'  
collection items terminated by '\002'  
map keys terminated by '\003' 
lines terminated by '\n' 
stored as textfile;

2. 表里 name 和 subordinates 的数据结构

3. 使用 lateral view 和 explode 查询

代码语言:javascript
复制
select name,subordinate from employees lateral view explode(subordinates) subordinates_table as subordinate; 

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

下面就做个小例子, 创建 hive 表 doc, 表里只有一列 text 类型为 string, 将 hadoop 目录下的 README.txt 导入该表, 并写出 sql 求出 wordcount

代码语言:javascript
复制
create table if not exists doc(text string) row format delimited lines terminated by '\n';

load data local inpath '/opt/hadoop-2.7.4/README.txt' overwrite into table doc;

select word, count(*) from doc lateral view explode(split(text,' ')) ITable as word group by word;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档