HIVE中关于collect_set与explode函数妙用

栏目: 服务器 · 发布时间: 6年前

内容简介:HIVE中关于collect_set与explode函数妙用

HIVE中关于collect_set与explode函数妙用

hive的复合数据类型

Hive中的列支持使用三类复杂的集合数据类型,即:array,map及struct,这些类型的名称是保留字,具体用法可参见该篇博文,里面有关于三类基本集合数据类型的操作实例,注:map中可嵌套array类型。

例如,定义表:

create table example ( 
    device_id string, 
    login_ip array<string>, 
    user_info map<string,array<string>> 
    address struct<street:string,city:string,state:string> 
) 
row format delimited 
fields terminated by '\001' 
collection items terminated by '\002' 
map keys terminated by '\003' 
lines terminated by '\n' 
stored as RCFile; 

假设这样的数据类型以分区表存储,你要统计一段时间类no=1下的去重score,那么该怎么办了?这里可配合使用lateral view首先实现列转行的功能,如下所示:

select no,score from tablaa lateral view explode(score_set) xxx as score;

注:xxx代表虚表名称,不能缺少。

进一步深化上述代码解决统计一段时间的去重值,可写为:

select no,collect_set(score) from tablaa lateral view explode(score_set) xxx as score group by no;

这样,将两个函数结合实现了行转列或列转行的妙用。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Lean Analytics

Lean Analytics

Alistair Croll、Benjamin Yoskovitz / O'Reilly Media / 2013-3-18 / USD 29.99

If you're involved with a startup, analytics help you find your way to the right product and market before the money runs out. But with a flood of information available, where do you start? This book ......一起来看看 《Lean Analytics》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具