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

2021-10-11 09:00:40 字數 1928 閱讀 4181

1.相關函式

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

concat_ws(separator, str1, str2,...):它是乙個特殊形式的 concat()。第乙個引數剩餘引數間的分隔符。分隔符可以是與剩餘引數一樣的字串。如果分隔符是 null,返回值也將為 null。這個函式會跳過分隔符引數後的任何 null 和空字串。分隔符將被加到被連線的字串之間;

collect_set(col):函式只接受基本資料型別,它的主要作用是將某字段的值進行去重彙總,產生array型別字段。

2.資料

孫悟空 白羊座 a

大海 射手座 a

宋宋 白羊座 b

豬八戒 白羊座 a

鳳姐 射手座 a

3.建立hive表並匯入資料

create table person_info(

name string,

constellation string,

blood_type string)

row format delimited fields terminated by "t";

load data local inpath "/opt/module/datas/constellation.txt" into table person_info;

4.需求

把星座和血型一樣的人歸類到一起。結果如下:

射手座,a 大海|鳳姐

白羊座,a 孫悟空|豬八戒

白羊座,b 宋宋

select

name,

concat(constellation, ",",blood_type) as base

from

person_info;

t1select

t1.base

concat_ws('|',collect_set(t1.name)) name

from(

select

name,

concat(constellation, ",",blood_type) as base

from

person_info;) t1

group by

t1.base;

列轉行 utdf炸開函式

1.函式說明

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

lateral view

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

解釋:用於和split, explode等udtf一起使用,它能夠將一列資料拆成多行資料,在此基礎上可以對拆分後的資料進行聚合。

2.資料準備

3.建立hive表並匯入資料

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;

4.需求

將電影分類中的陣列資料展開

select

movie,category_name

from

movie_info

lateral view explode(category) table_category as category_name;

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 列轉行和行轉列

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