Hive lateral view 和 explode 详解(转载)

  1. 建表语句结构

create table if not exists employees (
name string,
salary float,
subordinates array,
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 的数据结构

  1. 使用 lateral view 和 explode 查询

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

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;

文章原出处:https://my.oschina.net/zdtdtel/blog/1613715

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值