Hive 行轉列 列轉行的理解

2021-10-25 03:27:03 字數 3749 閱讀 6082

建立表,匯入資料

create

table person_info(

name string,

age int

,constellation string,

blood_type string)

row format delimited fields

terminated

by" "

;

load

data

local inpath "/root/test.txt"

into

table person_info;

test.txt 據資訊

孫悟空 20 白羊座 a

大海 19 射手座 a

宋宋 36 白羊座 b

豬八戒 40 白羊座 a

鳳姐 18 射手座 a

concat(string a/col, string b/col…):返回輸入字串連線後的結果,支援任意個輸入

字串;

這裡把每一行的列都用「-」拼接成一列資訊

concat_ws(separator, str1, str2,…):它是乙個特殊形式的 concat()。

separator:分隔符。將str1、str2…都用separator連線

str必須是string或array,否則報錯

比如:

我們可以把age轉成string型別

concat_set(col):函式只接受基本資料型別,它的主要作用是將某字段的值進行去

重彙總,產生 array 型別字段。

我先檢視不同星座不同血型有哪些人,這些人用「|」分割

首先把不同星座不同血型拼接到一起

explode(col):將 hive 一列中複雜的 array 或者 map 結構拆分成多行。

用法:later view udtf(expression) tablealias as columnalias

解釋:用於和 split, explode 等 udtf 一起使用,它能夠將一列資料拆成多行資料,在此

基礎上可以對拆分後的資料進行聚合。

create

table movie_info(

movie string,

category array

)row format delimited fields

terminated

by"\t"

collection items terminated

by","

;load

data

local inpath "/opt/module/datas/movie.txt"

into

table

movie_info;

炸裂開movie_info中category列

如果想在前面加電影名,下面寫法報錯,這是需要lateral view

把explode(category) 變成table_tmp ,加上lateral view 把列名叫做category_name

再舉個例子

user    friends

小張 王五,李四

小紅 張三,李四

select

user

,friend

from ods_user_friends

lateral view explode(split(friends,

",")

) s as friend

user 	friend 

小張 王五

小張 李四

小紅 張三

小紅 李四

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...

Hive 行轉列 列轉行

並不是真正意義上的行轉列 列轉行。只是這樣叫的。concat stringa,stringb,stringc 返回輸入字串拼接後的結果,支援輸入任意多個字串 測試結果 可以連線任意多個 concat ws 分隔符 stringa,stringb 是乙個特殊的concat 第乙個引數是引數間的分隔符 ...

hive 列轉行 HQL 行轉列,列轉行

1 相關函式 concat string a col,string b col 返回輸入字串連線後的結果,支援任意個輸入字串 concat ws separator,str1,str2,它是乙個特殊形式的 concat 第乙個引數剩餘引數間的分隔符。分隔符可以是與剩餘引數一樣的字串。如果分隔符是 n...