1. 程式人生 > 其它 >大資料之Hive:其他常用查詢函式之行轉列

大資料之Hive:其他常用查詢函式之行轉列

技術標籤:大資料系列二

行轉列
1.相關函式說明
CONCAT(string A/col, string B/col…):返回輸入字串連線後的結果,支援任意個輸入字串;
CONCAT_WS(separator, str1, str2,…):它是一個特殊形式的 CONCAT()。第一個引數剩餘引數間的分隔符。分隔符可以是與剩餘引數一樣的字串。如果分隔符是 NULL,返回值也將為 NULL。這個函式會跳過分隔符引數後的任何 NULL 和空字串。分隔符將被加到被連線的字串之間;
COLLECT_SET(col):函式只接受基本資料型別,它的主要作用是將某欄位的值進行去重彙總,產生array型別欄位。

2. 資料準備
在這裡插入圖片描述
3.需求
把星座和血型一樣的人歸類到一起。結果如下:
射手座,A 大海|鳳姐
白羊座,A 孫悟空|豬八戒
白羊座,B 宋宋
4.建立本地constellation.txt,匯入資料

[[email protected] datas]$ vi constellation.txt

5.建立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;

6.按需求查詢資料

select
    t1.base,
    concat_ws('|', collect_set(t1.name)) name
from
    (select
        name,
        concat(constellation, ",", blood_type) base
    from
        person_info) t1
group
by t1.base;