1、列轉行ui
user_basic_info:spa
id | name |
1 | a |
2 | b |
3 | c |
4 | d |
user_address;code
name | address |
a | add1 |
a | add2 |
b | add3 |
c | add4 |
d | add5 |
user_infoci
id | name | address |
1 | a | add1,add2 |
2 | b | add3 |
3 | c | add4 |
4 | d | add5string |
建表語句:it
create table user_basic_info( id string, name string )ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ; --指定每一行中的字段用\t 作分隔符,
create table user_address(name string, address string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ; --指定每一行中的字段用\t 作分隔符,
create table user_info( id string, name string , address array<string> ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ',' ;--集合按照指定分割符號分割
導入數據:table
load data local inpath '/usr/local/hive/user_basic_info_file' into table user_basic_info; load data local inpath '/usr/local/hive/user_address_file' into table user_address;
列轉行,並將數據插入到表 user_info中file
insert overwrite table user_info select max(ubi.id), ubi.name, collect_set(ua.address) as address from user_basic_info ubi join user_address ua on ubi.name=ua.name group by ubi.name;
2、行轉列select
建立新表 user_info2im
create table user_info2( id string, name string, address string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
行轉列語句:
insert into user_info2 select id, name, add from user_info ui lateral view explode(ui.address) adtable as add;