Hive中行列转换

Hive中行列转换


行转列

  • 多行转多列

    数据表 row2col
    
    col1   col2    col3
    a      c       1
    a      d       2
    a      e       3  
    b      c       4
    b      d       5
    b      e       6
    
    现在要将其转化为:
    col1   c      d      e
    a      1      2      3
    b      4      5      6
    
    此时需要使用到max(case … when … then … else 0 end),仅限于转化的字段为数值类型且为正值的情况
    
    创建表:
    create table row2col(col1 string,col2 string,col3 int)
    row format delimited
    fields terminated by ',';
    
    加载数据:
    load data local inpath '/root/hivedata/row2col.txt' into table row2col;
    a,c,1
    a,d,2
    a,e,3
    b,c,4
    b,d,5
    b,e,6
    
    select col1,
    max(case col2 when 'c' then col3 else 0 end) as c,
    max(case col2 when 'd' then col3 else 0 end) as d,
    max(case col2 when 'e' then col3 else 0 end) as e
    from row2col
    group by col1;
    
  • 多行转单列(重要)

    数据表 row2col_1:
    col1    col2    col3
    a       b       1
    a       b       2
    a       b       3
    c       d       4
    c       d       5
    c       d       6
    
    将其转化为:
    col1    col2    col3
    a       b       1,2,3
    c       d       4,5,6
    
    此时需要两个内置的函数:
    a)concat_ws(参数1,参数2),用于进行字符的拼接 
    	参数1—指定分隔符 
    	参数2—拼接的内容 
    b)collect_set(col3),它的主要作用是将某字段的值进行去重汇总,产生array类型字段
       如果不想去重可用collect_list()
    
    创建表:
    create table row2col_1(col1 string,col2 string,col3 int)
    row format delimited
    fields terminated by ',';
    
    加载数据:
    load data local inpath '/root/hivedata/row2col_1.txt' into table row2col_1;
    a,b,1
    a,b,2
    a,b,3
    c,d,4
    c,d,5
    c,d,6
    
    select col1, col2, concat_ws('|', collect_set(cast(col3 as string))) as col3
    from row2col_1
    group by col1, col2;
    

列转行

  • 多列转多行

    数据表 col2row:
    col1   c      d      e
    a      1      2      3
    b      4      5      6
    
    现要将其转化为:
    col1   col2    col3
    a      c       1
    a      d       2
    a      e       3
    b      c       4
    b      d       5
    b      e       6
    
    这里需要使用union进行拼接。
    union 可以结合多个select语句 返回共同的结果集
    保证每个select语句返回的数据类型个数是一致的。
    
    创建表:
    create table col2row(col1 string,c int,d int,e int)
    row format delimited
    fields terminated by ',';
    
    加载数据:
    load data local inpath '/root/hivedata/col2row.txt' into table col2row;
    a,1,2,3
    b,4,5,6
    
    select col1, 'c' as col2, c as col3 from col2row
    UNION
    select col1, 'd' as col2, d as col3 from col2row
    UNION
    select col1, 'e' as col2, e as col3 from col2row
    order by col1, col2;
    
  • 单列转多行(重要)

    数据表 col2row_2:
    col1    col2    col3
    a       b       1,2,3
    c       d       4,5,6
    
    现要将其转化为:
    col1    col2    col3
    a       b       1
    a       b       2
    a       b       3
    c       d       4
    c       d       5
    c       d       6
    
    这里需要使用UDTF(表生成函数)explode(),该函数接受array类型的参数,其作用恰好与collect_set相反,实现将array类型数据行转列。explode配合lateral view实现将某列数据拆分成多行。
    
    创建表:
    create table col2row_2(col1 string,col2 string,col3 Array<string>)
    row format delimited
    fields terminated by '\t'
    collection items terminated by ',';
    
    create table col2row_2(col1 string,col2 string,col3 string)
    row format delimited
    fields terminated by '\t';
    
    

加载数据:
load data local inpath ‘/root/hivedata/col2row_2.txt’ into table col2row_2;
a b 1,2,3
c d 4,5,6

  

select col1, col2, lv.col3 as col3
from col2row_2
lateral view explode(split(col3, ‘,’)) lv as col3;


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值