1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 大数据常见函数及案例实战

大数据常见函数及案例实战

时间:2021-06-28 11:56:27

相关推荐

大数据常见函数及案例实战

空字段赋值

1)函数说明

NVL:给值为 NULL 的数据赋值,它的格式是 NVL( string1, replace_with)。

它的功能是如果string1 为 NULL,则 NVL 函数返回 replace_with 的值,否则返回 string1 的值,如果两个参数都为 NULL ,则返回 NULL。

2)数据准备:采用emp表

emp.empno emp.ename emp.job emp.mgr emp.hiredate emp.sal m emp.deptno7369 SMITH CLERK 7902 1980-12-17 800.0 NULL 207499 ALLEN SALESMAN 7698 1981-2-20 1600.0 300.0 307521 WARD SALESMAN 7698 1981-2-22 1250.0 500.0 307566 JONES MANAGER 7839 1981-4-2 2975.0 NULL 207654 MARTIN SALESMAN 7698 1981-9-28 1250.0 1400.0 307698 BLAKE MANAGER 7839 1981-5-1 2850.0 NULL 307782 CLARK MANAGER 7839 1981-6-9 2450.0 NULL 107788 SCOTT ANALYST 7566 1987-4-19 3000.0 NULL 207839 KING PRESIDENT NULL 1981-11-17 5000.0 NULL 107844 TURNER SALESMAN 7698 1981-9-8 1500.0 0.0 307876 ADAMS CLERK 7788 1987-5-23 1100.0 NULL 207900 JAMES CLERK 7698 1981-12-3 950.0 NULL 307902 FORD ANALYST 7566 1981-12-3 3000.0 NULL 207934 MILLER CLERK 7782 1982-1-23 1300.0 NULL 10

3)查询:如果员工的 comm 为 NULL,则用-1 代替

hive (default)> select comm,nvl(comm,-1) from emp;

4)查询:如果员工的 comm 为 NULL,则用领导 id 代替

hive (default)> select mgr,comm,nvl(comm,mgr) from emp;

时间类

1)date_format:格式化时间

hive(default)>selectdate_format('-06-29','yyyy-MM-dd');

2)date_add:时间跟天数相加

hive (default)> select date_add('-06-29',5);hive (default)> select date_add('-06-29',-5);

3)date_sub:时间跟天数相减

hive (default)> select date_sub('-06-29',5);hive (default)> select date_sub('-06-29 12:12:12',5);hive (default)> select date_sub('-06-29',-5);

4)datediff:两个时间相减

hive (default)> select datediff('-06-29','-06-24');hive (default)> select datediff('-06-24','-06-29');hive (default)> select datediff('-06-24 12:12:12','-06-29');hive (default)> select datediff('-06-24 12:12:12','-06-29 13:13:13');

CASE WHEN

1.数据准备

name dept_id sex

悟空 A 男八戒 A 男沙僧 B 男妖怪 A 女豹女 B 女轮子妈 B 女

2.创建 hive 表并导入数据

create table emp_sex(name string,dept_id string,sex string)row format delimited fields terminated by "\t";#加载数据load data local inpath '/opt/module/data/emp_sex.txt' into table emp_sex;

5.求出不同部门男女各多少人,按需求查询数据

selectdept_id,sum(case sex when '男' then 1 else 0 end) male_count,sum(case sex when '女' then 1 else 0 end) female_countfromemp_sexgroup bydept_id;

行转列

1.相关函数说明

CONCAT(string A/col, string B/col…):返回输入字符串连接后的结果,支持任意个输入字符串;

CONCAT_WS(separator, str1, str2,...):它是一个特殊形式的 CONCAT()。第一个参数剩余参数间的分隔符。

分隔符可以是与剩余参数一样的字符串。如果分隔符是 NULL,返回值也将为 NULL。这个函数会跳过分隔符参数后的任何 NULL 和空字符串。分隔符将被加到被连接的字符串之间;

COLLECT_SET(col):函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生 array 类型字段。

2.数据准备

name constellation blood_type

孙悟空 白羊座 A沙僧 射手座 A德玛 白羊座 B猪八戒 白羊座 A凤姐 射手座 A

3.需求

把星座和血型一样的人归类到一起。结果如下:

4.创建本地 constellation.txt,导入数据

孙悟空 白羊座 A沙僧 射手座 A德玛 白羊座 B猪八戒 白羊座 A凤姐 射手座 A

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/data/person_info.txt" into table person_info;

6.按需求查询数据

selectt1.base,concat_ws('|', collect_set(t1.name)) namefrom(selectname,concat(constellation, ",", blood_type) basefromperson_info) t1group byt1.base;

列转行

1.函数说明

EXPLODE(col):将 hive 一列中复杂的 array 或者 map 结构拆分成多行。

LATERAL VIEW用法:LATERAL VIEW udtf(expression) tableAlias AS columnAlias

解释:用于和 split, explode 等 UDTF 一起使用,它能够将一列数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合。

2.创建本地 movie.txt,导入数据

[root@bigdata161 datas]$ vi movie.txt

《疑犯追踪》悬疑,动作,科幻,剧情《Lie to me》悬疑,警匪,动作,心理,剧情《战狼 2》战争,动作,灾难

3.创建 hive 表并导入数据

create table movie_info(movie string,category array<string>)row format delimited fields terminated by "\t"collection items terminated by ",";load data local inpath "/opt/module/data/movie.txt" into table movie_info;

6.将电影分类中的数组数据展开,按需求查询数据

selectmovie,category_namefrommovie_infolateral view explode(category) table_tmp as category_name;

窗口函数

1.相关函数说明

OVER():指定分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化;

CURRENT ROW:当前行;

n PRECEDING:往前 n 行数据;

n FOLLOWING:往后 n 行数据;

UNBOUNDED:起点,

UNBOUNDED PRECEDING 表示从前面的起点, UNBOUNDEDFOLLOWING 表示到后面的终点;

LAG(col,n):往前第 n 行数据;

LEAD(col,n):往后第 n 行数据;

NTILE(n):把有序分区中的行分发到指定数据的组中,各个组有编号,编号从 1 开始,对于每一行,NTILE 返回此行所属的组的编号。注意:n 必须为 int 类型。

2.数据准备:name,orderdate,cost

jack,-01-01,10tony,-01-02,15jack,-02-03,23tony,-01-04,29jack,-01-05,46jack,-04-06,42tony,-01-07,50jack,-01-08,55mart,-04-08,62mart,-04-09,68neil,-05-10,12mart,-04-11,75neil,-06-12,80mart,-04-13,94

3.需求

(1)查询在 年 4 月份购买过的顾客及总人数

(2)查询顾客的购买明细及购买总额

(3)上述的场景,要将 cost 按照日期进行累加

(4)查询顾客上次的购买时间

(5)查询前 20%时间的订单信息

4.创建本地 business.txt,导入数据

[root@bigdata161 datas]$ vi business.txt

5.创建 hive 表并导入数据

create table business(name string,orderdate string,cost int) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';load data local inpath "/opt/module/data/business.txt" into table business;

6.按需求查询数据

(1)查询在 年 4 月份购买过的顾客及总人数

selectname,count(*) over()frombusinesswheresubstring(orderdate,1,7) ='-04'groupbyname;

(2)查询顾客的购买明细及月购买总额

selectname,orderdate,cost,sum(cost) over(partition by month(orderdate))frombusiness;

(3)上述的场景,要将 cost 按照日期进行累加select

美化后

SELECT`NAME`,orderdate,cost,#--所有行相加sum( cost ) over ( ) AS sample1,#--按 name 分组,组内数据相加sum( cost ) over ( PARTITION BY NAME ) AS sample2,#-- 按 name 分组,组内数据累加sum( cost ) over ( PARTITION BY NAME ORDER BY orderdate ) AS sample3,#--和 sample3 一样,由起点到当前行的聚合sum( cost ) over ( PARTITION BY NAME ORDER BY orderdate rows BETWEEN UNBOUNDED PRECEDING AND current ROW ) AS sample4,#--当前行和前面一行做聚合sum( cost ) over ( PARTITION BY NAME ORDER BY orderdate rows BETWEEN 1 PRECEDING AND current ROW ) AS sample5,#--当前行和前边一行及后面一行sum( cost ) over ( PARTITION BY NAME ORDER BY orderdate rows BETWEEN 1 PRECEDING AND 1 FOLLOWING ) AS sample6,# --当前行及后面所有行sum( cost ) over ( PARTITION BY NAME ORDER BY orderdate rows BETWEEN current ROW AND UNBOUNDED FOLLOWING ) AS sample7FROMbusiness;

执行的sql

select name,orderdate,cost,sum(cost) over() as sample1,sum(cost) over(partition by name) as sample2,sum(cost) over(partition by name order by orderdate) as sample3,sum(cost) over(partition by name order by orderdate rows betweenUNBOUNDED PRECEDING and current row ) as sample4 ,sum(cost) over(partition by name order by orderdate rows between1 PRECEDING and current row) as sample5,sum(cost) over(partition by name order by orderdate rows between1 PRECEDING AND 1 FOLLOWING ) as sample6,sum(cost) over(partition by name order by orderdate rows betweencurrent row and UNBOUNDED FOLLOWING ) as sample7from business;

(4)查看顾客上次的购买时间

select name,orderdate,cost,lag(orderdate,1,'1900-01-01') over(partition by name order byorderdate ) as time1, lag(orderdate,2) over (partition by nameorder by orderdate) as time2from business;

(5)查询前 20%时间的订单信息

select * from (select name,orderdate,cost, ntile(5) over(order by orderdate)sortedfrom business) twhere sorted = 1;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。