实例一:来源:

https://www.cnblogs.com/kimbo/p/6208973.html

行转列 (对某列拆分,一列拆多行)
使用函数:lateral view explode(split(column, ',')) num
eg: 如表:t_row_to_column_tmp 数据如下,对tag列进行拆分
hive行转列,列转行
结果:
hive行转列,列转行
转行 (根据主键,进行多行合并一列)
使用函数:concat_ws(',',collect_set(column))  
说明:collect_list 不去重,collect_set 去重。 column 的数据类型要求是 string
eg:如表:t_column_to_row ,根据id,对tag_new 进行合并
hive行转列,列转行

hive行转列,列转行

hive行转列,列转行

 

 实例二:

表合并,行转列

hive行转列,列转行

hive行转列,列转行

collect_set这就用到了hive中的行转列的知识,需要用到两个内置UDF: collect_set, concat_ws,
建表:
create table user_basic_info(id string, name string);
create table user_address(name string, address string);
 
加载数据:
load data local inpath '/home/jthink/work/workspace/hive/row_col_tran/data1' into table user_basic_info;
load data local inpath '/home/jthink/work/workspace/hive/row_col_tran/data2' into table user_address;
 
执行合并:
select max(ubi.id), ubi.name, concat_ws(',', collect_set(ua.address)) as address from user_basic_info ubi join user_address ua on ubi.name=ua.name group by ubi.name;
 
hive行转列,列转行

表拆分,列转行

 hive行转列,列转行

hive行转列,列转行

 

 

 

 

相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
  • 2022-02-06
猜你喜欢
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2021-09-24
  • 2022-12-23
  • 2021-10-30
相关资源
相似解决方案