hive入門之常用查詢函式 6

2021-10-02 13:27:12 字數 3043 閱讀 7567

select comm,nvl(comm, -1) from emp;
查詢comm欄位,值為null就展示-1.

資料準備

name	dept_id	***

悟空 a 男

大海 a 男

宋宋 b 男

鳳姐 a 女

婷姐 b 女

婷婷 b 女

2.需求

求出不同部門男女各多少人。結果如下:

a     2       1

b 1 2

3.建立本地emp_***.txt,匯入資料

[atguigu@hadoop102 datas]$ vi emp_***.txt

悟空	a	男

大海 a 男

宋宋 b 男

鳳姐 a 女

婷姐 b 女

婷婷 b 女

4.建立hive表並匯入資料

create table emp_***(

name string,

dept_id string,

*** string)

row format delimited fields terminated by "\t";

load data local inpath '/opt/module/datas/emp_***.txt' into table emp_***;

5.按需求查詢資料

select 

dept_id,

sum(case *** when '男' then 1 else 0 end) male_count,

sum(case *** when '女' then 1 else 0 end) female_count

from

emp_***

group by

dept_id;

相關函式說明

建立本地constellation.txt,匯入資料

vi constellation.txt
孫悟空	白羊座	a

大海 射手座 a

宋宋 白羊座 b

豬八戒 白羊座 a

鳳姐 射手座 a

建立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.按需求查詢資料

函式說明

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

lateral view

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

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

建立本地movie.txt,匯入資料

vi movie.txt
《疑犯追蹤》	懸疑,動作,科幻,劇情

《lie to me》 懸疑,警匪,動作,心理,劇情

《戰狼2》 戰爭,動作,災難

建立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;
按需求查詢資料
select

movie,

category_name

from

movie_info lateral view explode(category) table_tmp as category_name;

其實和下面一樣:

select

m.movie,

table_tmp.category_name

from

movie_info m

lateral view explode(category) table_tmp as category_name;

大資料之Hive 其他常用查詢函式

1.空欄位賦值 nvl 給值為null的資料賦值,它的格式是nvl value,default value 它的功能是如果value為null,則nvl函式返回default value的值,否則返回value的值,如果兩個引數都為null 則返回null 示例1 查詢如果員工的comm為null,...

Hive常用函式

if判斷 select if 1 1,yes no 返回yes 語法 case expression when condition1 then result1 when condition2 then result2 else result end 例子 case a when 1 then one...

hive常用函式

hive常用函式 1 檢視函式用法 desc function 函式名 desc function extended 函式名 2 獲取array陣列長度 size函式 select size collect list field from table select size split hello ...