IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 大数据之hive:行列转换系列总结 -> 正文阅读

[大数据]大数据之hive:行列转换系列总结

1、行转列(一)

主要使用:

CONCAT(string A/col, string B/col…):返回输入字符串连接后的结果,支持任意个输入字符串;
CONCAT_WS(separator, str1, str2,…):它是一个特殊形式的 CONCAT()。第一个参数是剩余其他参数间的分隔符。
COLLECT_SET(col):函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生array类型字段。

示例

原始数据:

nameconstellationblood_type
宋江白羊座A
鲁智深射手A
武松白羊座B
潘金莲白羊座A
西门庆射手A

期望输出结果:

constell_bloodname_list
射手座,A鲁智深&西门庆
白羊座,A宋江&潘金莲
白羊座,B武松

实现:

select
    t1.base,
    concat_ws('&', collect_set(t1.name)) name
from
    (select
        name,
        concat(constellation, ",", blood_type) constell_blood
    from
        person_info) t1
group by
    t1.constell_blood;

2、列转行(一)

主要使用
EXPLODE(col):将hive一列中复杂的array或者map结构拆分成多行。
LATERAL VIEW
用法:LATERAL VIEW udtf(expression) tableAlias AS columnAlias
解释:用于和split, explode等UDTF一起使用,它能够将一列数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合。

示例

原始数据:

moviecategory
《疑犯追踪》悬疑&动作
《Lie to me》悬疑&警匪

期望输出结果:

moviecategory_name
《疑犯追踪》悬疑
《疑犯追踪》动作
《Lie to me》悬疑
《Lie to me》警匪

实现:

select
    movie,
    category_name
from 
 movie_info 
lateral view explode(split(category, "\\&")) table_tmp as category_name;

3、行转列(二)

主要使用sum(case when )

示例

原始数据:

stu_idnamecoursescore
01zhangsanmath90
01zhangsanchinese88
01zhangsanenglish88
02lisimath66
02lisichinese77
02lisienglish80

期望输出结果:

stu_idnamemat_scorechi_scoreeng_score
01zhangsan908888
02lisi667780

实现1:

select 
stu_id,
name,
sum(case when course='math' then score else 0 end) mat_score,
sum(case when course='chinese' then score else 0 end) chi_score,
sum(case when course='english' then score else 0 end) eng_score
from stu001 
group by stu_id,name

实现2:

select 
a.stu_id,
a.name,
(select score from stu001 m where course='math'  and a.stu_id=m.stu_id and m.name=a.name ) mat_score,
(select score from stu001 m where course='chinese' and a.stu_id=m.stu_id and m.name=a.name ) chi_score,
(select score from stu001 m where course='english'  and a.stu_id=m.stu_id and m.name=a.name ) eng_score
from stu001 a
group by stu_id,name;

4、列转行(二)

示例

主要使用union all
原始数据:

stu_idnamemat_scorechi_scoreeng_score
01zhangsan908888
02lisi667780

期望输出结果:

stu_idnamecoursescore
01zhangsanmath90
01zhangsanchinese88
01zhangsanenglish88
02lisimath66
02lisichinese77
02lisienglish80

实现:

select
stu_id,
name,
if(mat_score is not null ,'match',null) as course,
mat_score as score
from 
(select 
a.stu_id,
a.name,
(select score from stu001 m where course='math'  and a.stu_id=m.stu_id and m.name=a.name ) mat_score,
(select score from stu001 m where course='chinese' and a.stu_id=m.stu_id and m.name=a.name ) chi_score,
(select score from stu001 m where course='english'  and a.stu_id=m.stu_id and m.name=a.name ) eng_score
from stu001 a
group by stu_id,name) tmp0
union all
select
stu_id,
name,
if(chi_score is not null ,'chinese',null) as course,
chi_score as score
from 
(select 
a.stu_id,
a.name,
(select score from stu001 m where course='math'  and a.stu_id=m.stu_id and m.name=a.name ) mat_score,
(select score from stu001 m where course='chinese' and a.stu_id=m.stu_id and m.name=a.name ) chi_score,
(select score from stu001 m where course='english'  and a.stu_id=m.stu_id and m.name=a.name ) eng_score
from stu001 a
group by stu_id,name) tmp1
union all
select
stu_id,
name,
if(eng_score is not null ,'english',null) as course,
eng_score as score
from 
(select 
a.stu_id,
a.name,
(select score from stu001 m where course='math'  and a.stu_id=m.stu_id and m.name=a.name ) mat_score,
(select score from stu001 m where course='chinese' and a.stu_id=m.stu_id and m.name=a.name ) chi_score,
(select score from stu001 m where course='english'  and a.stu_id=m.stu_id and m.name=a.name ) eng_score
from stu001 a
group by stu_id,name) tmp2
order by stu_id,name;
  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-08-23 16:45:39  更:2021-08-23 16:46:33 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/25 22:28:43-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码