hive中實現行轉列 hive中的列轉行和行轉列

2021-10-16 14:31:19 字數 1615 閱讀 8708

1、列轉行

concat(string1,string,...)  //連線括號內字串,數量不限。

concat_ws(separator,string1,string2,...)  //連線括號內字串,數量不限,連線符為separator。

collect_set(col)  //此函式只接受基本型別,主要是將字段的值進行去重彙總,產生array型別字段。

1.2 例子:

建立表:create table person_info(

name string,

constellation string,

blood_type string

)row format delimited fields terminated by '\t';

上傳資料:

load data local inpath 『/home/hdc/constellation.txt』 into table person_info;

查詢語句:

select t.base,concat_ws('|',collect_set(t.name)) name

from(

select name,concat(costellation,',',blood_type) base

from person_info

)tgroup by t.base;

2、行轉列

explode(col_name):將hive中的一列中複雜的array或者map分成多行

lateral view:側檢視配合explode(或者其他的udtf),乙個語句生成把單行資料拆解成多行後的資料結果集。  //lateral view explode(split(goods_id,','))goods相當於乙個虛擬表

2.2 例子:

建立表:create table movie_info(

name string,

categroy array

)row format delimited fields terminated by '\t';

collection items terminated by ',';

上傳資料:

load data local inpath '/home/hdc/movie.txt' into table movie_info;

查詢語句:

select name,category_type

from movie_info lateral view explode(categroy) temp_table as category_type;

解析:表movie_info與虛表temp_table進行笛卡爾乘積其中temp_table表中的字段為category_type

explode還有如下用法:

select distinct(t2.videoid), t3.category

from (

select explode(relatedid) as videoid

from (

select *

from video_orc

order by views desc

limit 50) t1

)t2

hive中實現行轉列 Hive行轉列詳解

需求 孫悟空 白羊座 a 沙悟淨 射手座 a 宋鬆鬆 白羊座 b 豬八戒 白羊座 a 小鳳姐 射手座 a 轉換成以下格式 白羊座,a 孫悟空 豬八戒 白羊座,b 宋鬆鬆 射手座,a 沙悟淨 小鳳姐 思路 表的行轉列 知識點 concat string1,string2 string1和string2...

SQL實現行轉列

需求 用sql實現行轉列。如下圖所示 行顯示的資料轉換成列顯示 實現行轉列的sql指令碼如下 select date format last day date format now y m d y m d as 業務日期,max case index code when ind20101001 th...

Mysql實現行轉列

create table loc loc varchar 50 xiaoqu varchar 50 addr varchar 50 company varchar 50 插入資料 區域 小區名稱 房屋位址 中介公司寶山 慶安三村 綏化路52弄a寶山 月浦十村 月浦十村67號b寶山 盛橋三村 盛橋三村...