首页 > 其他分享 >Hive 行转列 列转行

Hive 行转列 列转行

时间:2022-11-06 21:14:02浏览次数:32  
标签:info category string movie 转行 Hive 转列 txt name

行转列

CONCAT(string A/col, string B/col…):返回输入字符串连接后的结果,支持任意个输入字符串;

CONCAT_WS(separator, str1, str2,...):它是一个特殊形式的 CONCAT()。第一个参数剩余参
数间的分隔符。分隔符可以是与剩余参数一样的字符串。如果分隔符是 NULL,返回值也将为 NULL。这个函数会跳过分隔符参数后的任何 NULL 和空字符串。分隔符将被加到被连接的字符串之间;
注意: CONCAT_WS must be "string or array<string>

COLLECT_SET(col):函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生 Array 类型字段。

image
image

image
image

创建本地 constellation.txt,导入数据

[atguigu@hadoop102 datas]$ vim person_info.txt
孙悟空 白羊座 A
大海 射手座 A
宋宋 白羊座 B
猪八戒 白羊座 A
凤姐 射手座 A
苍老师 白羊座 B

创建 hive 表并导入数据

create table person_info(
name string, 
constellation string, 
blood_type string) 
row format delimited fields terminated by "\t";
load data local inpath "/opt/module/hive/data/person_info.txt" into table person_info;

按需求查询数据

select
   t1.c_b,
   concat_ws('|',collect_set(t1.name))
from(
    select
	name,
	concat_ws(',',constellation,blood_type) c_b
    from
	person_info
)t1
group by t1.c_b

列转行

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

image
创建本地movies文件

[atguigu@hadoop102 datas]$ vi movie_info.txt
《疑犯追踪》 悬疑,动作,科幻,剧情
《Lie to me》悬疑,警匪,动作,心理,剧情
《战狼 2》 战争,动作,灾难

创建 hive 表并导入数据

create table movie_info(
 movie string,
 category string)
row format delimited fields terminated by "\t";
load data local inpath "/opt/module/data/movie.txt" into table 
movie_info;

按需求查询数据

select
    movie,
    category_name
from
   movie_info
lateral view
   explode(split(category,',')) movie_info_tmp as category_name;

PS:如果不是于原表关联的话

SELECT explode(split(category,",")) from movie_info;

标签:info,category,string,movie,转行,Hive,转列,txt,name
From: https://www.cnblogs.com/catch-autumn/p/16863930.html

相关文章

  • Hive-- CASE WHEN THEN ELSE END 和 IF 函数
    创建本地emp_sex.txt,导入数据[atguigu@hadoop102datas]$viemp_sex.txt悟空A男大海A男宋宋B男凤姐A女婷姐B女婷婷B女创建hive表并导入数据cr......
  • Hive 动态分区 3.0新特征
    下面两者同理,也就是说在动态分区中可以不用继续写parititon,因为最后是按照select最后一个字段(deptno)去进行分区的insertintotabledeptpartition(deptno='10')selec......
  • Hive Order By,Sort by,Distribute By,Cluster By 排序区别
    OrderByOrderBy:全局排序,只有一个Reducer,就算提前设置好n个reducerorderby也是只执行一个reducer,因为全局排序,排序的仅仅是一个表罢了。orderby对于大规模数据集......
  • MySQL函数-Group_Concat分组并行转列
    group_concat函数解析:1、concat()函数:  功能:将多个字符串连接成一个字符串  语法:concat(str1,str2)  结果:连接参数str1,str2为一个字符串,如果有任何一个参数为n......
  • HIVE- set
    SEThive.execution.engine=tez;SEThive.tez.auto.reducer.parallelism=true;SEThive.auto.convert.join=true;sethive.exec.compress.output=true;sethive.in......
  • hive的trunc函数详解
    一、日期TRUNC函数为指定元素而截去的日期值。其具体的语法格式:TRUNC(date[,fmt])其中:date一个日期值fmt日期格式,该日期将由指定的元素格式所截去。忽略它则由最......
  • (转)hive中序列化和反序列化简介
    原文:https://blog.csdn.net/xixihaha_coder/article/details/121229591hive中序列化和反序列化简介serde简介hive的常用serdeLazySimpleSerDeCSVjsonserde......
  • Hive系列之解析JSON数据
    概述在数据处理中,经常遇到的一个数据类型就是JSON,MySQL数据库解析JSON,参考​​MySQL5.7JSON函数学习​​,​MySQLjson_mergewithgroupby​​。在大数据执行引擎Hive中,......
  • Flink SQL UNNEST/UDTF 如何实现列转行?
    在SQL任务里面经常会遇到一列转多行的需求,今天就来总结一下在FlinkSQL里面如何实现列转行的,先来看下面的一个具体案例. 需求:原始数据格式如下namedata......
  • WSL 中搭建 hadoop/hive 环境后,在 windows 宿主机下连接 hive 被拒绝
    报错只有:java.net.ConnectException:Connectionrefused这样简短的几行。其真实原因是(在windows11下),WSL有自己的(内部)IP地址。在WSL下执行ipaddr,然后在cmd下......