hive中的列转行和行转列
1、列转行
1.1 相关函数的说明:
concat(string1,string,...) //连接括号内字符串,数量不限。
concat_ws(separator,string1,string2,...) //连接括号内字符串,数量不限,连接符为separator。
collect_set(col) //此函数只接受基本类型,主要是将字段的值进行去重汇总,产生array类型字段。
1.2 例子:

创建表:create table person_info(
name string,
constellation string,
blood_type string
)row format delimited fields terminated by '\t';
上传数据:
load data local inpath ‘/home/hdc/constellation.txt’ into table person_info;
查询语句:
select t.base,concat_ws('|',collect_set(t.name)) name
from(
select name,concat(costellation,',',blood_type) base
from person_info
)t
group by t.base;
2、行转列
2.1 相关函数:
explode(col_name):将hive中的一列中复杂的array或者map分成多行
lateral view:侧视图配合explode(或者其他的UDTF),一个语句生成把单行数据拆解成多行后的数据结果集。 //LATERAL VIEW explode(split(goods_id,','))goods相当于一个虚拟表
2.2 例子:

创建表:create table movie_info(
name string,
categroy array<string>
)row format delimited fields terminated by '\t';
collection items terminated by ',';
上传数据:
load data local inpath '/home/hdc/movie.txt' into table movie_info;
查询语句:
select name,category_type
from movie_info lateral view explode(categroy) temp_table as category_type;
解析:表movie_info与虚表temp_table进行笛卡尔乘积其中temp_table表中的字段为category_type
explode还有如下用法:
select distinct(t2.videoid), t3.category
from (
select explode(relatedid) as videoid
from (
select *
from video_orc
order by views desc
limit 50) t1
)t2
-->
hive中的列转行和行转列的更多相关文章
- SQL列转行,行转列实现
在工作中,大家可能会遇到一些SQL列转行.行转列的问题,恰好,我也遇到了,就在此记录一下.此处所用的是SQLServer2008R2. 行转列,列转行,都要预先知道要要处理多少数据,在此我就以三种方案 ...
- hive中array嵌套map以及行转列的使用
1. 数据源信息 {"student": {"name":"king","age":11,"sex" ...
- Spark基于自定义聚合函数实现【列转行、行转列】
一.分析 Spark提供了非常丰富的算子,可以实现大部分的逻辑处理,例如,要实现行转列,可以用hiveContext中支持的concat_ws(',', collect_set('字段'))实现.但是 ...
- oracle 逗号分割,列转行,行转列
SQL代码 列转行 select REGEXP_SUBSTR(a.rolecode ,,l) rolecode from ( select 'a,aa,aaa' rolecode from dual ...
- SQL 列转行与行转列
假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/ -------------- ...
- sqlserver 行转列、字符串行转列、自动生产行转列脚本
行转列,老生常谈的问题.这里总结一下网上的方法. 1.生成测试数据: CREATE TABLE human( name ), --姓名 norm ), --指标 score INT , --分数 gr ...
- 转:hive-列转行和行转列
1. 假设我们在hive中有两张表,其中一张表是存用户基本信息,另一张表是存用户的地址信息等,表数据假设如下: user_basic_info: id name 1 a 2 b 3 c 4 d use ...
- pandas中获取数据框的行、列数
获取数据框的行.列数 # 获取行数 df.shape[0] # 获取行数 len(df) # 获取列数 df.shape[1]
- MySQL 行转列 -》动态行转列 -》动态行转列带计算
Pivot Table Using MySQL - A Complete Guide | WebDevZoomhttp://webdevzoom.com/pivot-table-using-mysql ...
随机推荐
- "C++ Primer Plus" is WAY FUCKING BETTER than "C++ Primer" (For Beginners)!!!
再看到给初学C++的人推荐C++ Primer的我就要揍人了,真的! 被他妈这帮装逼犯给误导了,耽误了无数的功夫! 就是听这帮傻逼的谣言,说C++ Primer讲解更深入什么的,初学也应该啃这本书,老 ...
- linux(centos6) 下安装 postgresql-9.3.1.tar.gz
目录 一. 环境 二.准备工作 三.先安装 make, gcc ,gcc-c++,readline-devel ,zlib-devel .如果已安装,可以忽略 四.开始安装 4.1 解压 tar -z ...
- JS各种变量是true或者false列表
如果操作数是一个对象,返回true 如果操作数是一个空字符串,返回false如果操作数是一个非空字符串,返回true如果操作数是数值0,返回false如果操作数是任意非0数值(包括Infinity), ...
- nginx配置-main
nginx配置分为几大块: main (全局设置) events(nginx工作模式) http(http设置): upstream(负载均衡服务器设置),server(主机设置):location( ...
- Eclipse总是自动跳到ThreadPoolExecutor
出现这种状况是因为Eclipse默认开启挂起未捕获的异常(Suspend execution on uncaught exceptions),只要关闭此项就可以了.解决方法:在eclipse中选择Wi ...
- Kestrel web server implementation in ASP.NET Core
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore1x&view ...
- redis为什么内存不宜过大
redis的高性能.稳定性都是不用怀疑的,但如果redis塞入数据过多,内存过大,那如果出问题,那它可能会给我们的就是灾难性的. 1 主库宕机 主库宕机,常见的策略为“切主”.具体为从该集群剩余从库中 ...
- [LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)
传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the m ...
- Linux——管道与重定向
参考资料: 极客学院IBMdeveloperWorks 重定向标准I/O Linux shell(比如Bash)接收或发送序列和字符串流形式的输入或输出.每个字符都独立于与之相邻的字符.字符没有被组织 ...
- jQury+Ajax与C#后台交换数据
-------------------------------------------jQury+Ajax调用后台方法----------------------------------------- ...