hive 列轉行和行轉列

2021-06-23 09:50:42 字數 1970 閱讀 9257

1. 假設我們在hive中有兩張表,其中一張表是存使用者基本資訊,另一張表是存使用者的位址資訊等,表資料假設如下:

user_basic_info: id

name1a

2b3c

4duser_address;

name

address

aadd1

aadd2

badd3

cadd4

dadd5 id

name

address1a

add1,add22b

add33c

add44d

add5

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;

執行結果:

1       a       add1,add2

2       b       add3

3       c       add4

4       d       add5

2. 假設我們有一張表:

user_info:

idname

address1a

add1,add22b

add33c

add44d

add5

我們需要拆分address,變為:

idname

address1a

add11a

add22b

add33c

add44d

add5

我們很容易想到用udtf,explode():

select explode(address) as address from user_info;

這樣執行的結果只有address, 但是我們需要完整的資訊:

select id, name, explode(address) as address from user_info;

這樣做是不對的, udtf's are not supported outside the select clause, nor nested in expressions

所以我們需要這樣做:

select id, name, add from user_info ui lateral view explode(ui.address) adtable as add;

結果為:

1       a       add1

1       a       add2

2       b       add3

3       c       add4

4       d       add5

hive 列轉行和行轉列

1.假設我們在hive中有兩張表,其中一張表是存使用者基本資訊,另一張表是存使用者的位址資訊等,表資料假設如下 user basic info idname1a 2b3c 4duser address name address aadd1 aadd2 badd3 cadd4 dadd5 我們可以看到...

Hive行轉列和列轉行

優點 好理解 缺點 多次select同一張表,造成計算量成倍增加 冗餘,單個select條件複雜後會變得較難維護。concat height height,weight weight,age age as value select id,height as label,height as value...

Hive行轉列,列轉行

下面舉兩個例子 例一 行轉列 資料 a b 1 a c 2 a b 3 c d 4 c d 5 c d 6 轉化為 a b 1,2,3 c d 4,5,6 創表hive create table test1 col1 string,col2 string,col3 string row format...