38be443160cf661d20d5f18e273440dc.png

写在开头:

本章是Hive教程第五部分归纳,着重于归纳Hive函数

文章内容输出来源:拉勾教育大数据高薪训练营

函数在做HQL开发时经常被用到,也需要不断练习才能掌握,这里将介绍hive的内置函数,时间日期函数,字符串函数,数学函数,函数二部分将介绍条件函数,UDTF函数,窗口函数,排名函数,序列函数等,最后加上sql的一些简单案例来帮助大家熟练使用这些函数。

Hive函数很重要所以下文会将所有常用的函数做一个梳理,配合运行结果,让大家更容易学习这些知识。

在学习这些函数时遇到很多问题,尤其是窗口函数,排名函数,序列函数等,从对知识点一开始的一头雾水到越来越清晰,首先感谢授课老师谆谆不倦的教导,每一次的教学都加深了我对这些函数的理解,其次感谢Bob导师,课后在复盘时因为粗心遇到各种问题都是他帮我解决。最后感谢班主任在学习群中的督促,尤其像我这样有严重拖延症的人来说,能让我能够按时完成相应的学习任务。

1.首先介绍HIVE系统内置函数

— 查看系统自带函数

show functions;

— 显示自带函数的用法

desc function upper;
desc function extended upper;

2.日期函数是hive函数模块中很重要的模块

— 查看当前前日期

select current_date;
select unix_timestamp();

— 建议使用current_timestamp,有没有括号都可以

select current_timestamp();

— 时间戳转日期

select from_unixtime(1505456567);
select from_unixtime(1505456567, 'yyyyMMdd');
select from_unixtime(1505456567, 'yyyy-MM-dd HH:mm:ss');

6eccce79375149599c5c09d1e77b82ed.png

— 日期转时间戳

select unix_timestamp('2019-09-15 14:23:00');

63d7d4fedd1f91df759c34483744cbaf.png

— 计算时间差

//查询20200418到20191112差了几天
select datediff('2020-04-18','2019-11-21'); 

b9c43c2035094ce9efa6a565c9199309.png

//同理

select datediff('2019-11-21', '2020-04-18');

— 查询今天是当月第几天

select dayofmonth(current_date);

6dee7aa74fe904f8ebaf0031369b7501.png

— 计算月末:

select last_day(current_date);

882fc25c217f79264b258d794508ef5b.png

— 查询当月第1天:

select date_sub(current_date, dayofmonth(current_date)-1)

767a5d27782e3bab6e54b738f6be68a2.png

— 下个月第1天:

select add_months(date_sub(current_date,dayofmonth(current_date)-1), 1)

6807f6cc40002553fa858f926729f0ae.png

— 字符串转时间(字符串必须为:yyyy-MM-dd格式)

select to_date('2020-01-01');
select to_date('2020-01-01 12:12:12');

8eef02c1227c4aca274dbaeea63d5829.png

— 日期、时间戳、字符串类型格式化输出标准时间格式

select date_format(current_timestamp(), 'yyyy-MM-dd HH:mm:ss');
select date_format(current_date(), 'yyyyMMdd');
select date_format('2020-06-01', 'yyyy-MM-dd HH:mm:ss');

a7ef77acd9a820f34756acb7a804e9f8.png

— 计算emp表中,每个人的工龄

select *, round(datediff(current_date, hiredate)/365,1) workingyears from emp;

字符串函数

— 转小写。lower

select lower('HELLO WORLD');

2e3cc68b74e49d2a94d14c6ac56a8ef4.png

— 转大写。upper

select upper(ename), ename from emp;

— 求字符串长度。length

select length(ename), ename from emp;

— 字符串拼接。 concat / ||

select empno || ” ” ||ename idname from emp;
select concat(empno, ” ” ,ename) idname from emp;

— 指定分隔符。concat_ws(separator, [string | array(string)]+)

SELECT concat_ws(‘.’, ‘www’, array(‘lagou','com’));
select concat_ws(” “, ename, job) from emp;

— 求子串。substr

SELECT substr(‘www.lagou.com’, 5);
SELECT substr(‘www.lagou.com’, -5);
SELECT substr(‘www.lagou.com’, 5, 5);

— 字符串切分。split,注意 ‘.’ 要转义

select split('www.lagou.com', '.');

d91a34b6163b0d84b462c534bc7c4527.png

数学函数

— 四舍五入。round

select round(314.15926);

ebcdde85ce56cab462e4f84b5adeab6c.png
select round(314.15926, 2);
select round(314.15926, -2);

— 向上取整。ceil

select ceil(3.1415926);

c521bf7ab6407434bd9f1d098af560ec.png

— 向下取整。floor

select floor(3.1415926);

1fa8a0644629d5c948d39d0244d6f2ad.png

写在结尾:

本章主要介绍了内置函数,时间日期函数,字符串函数和数学函数,其中日期函数在自己使用时还是会有很多问题。

下一章将介绍函数比较难的部分。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐