Hive的函数

本文详细介绍了Hive中的窗口函数,包括窗口聚合、窗口分片、窗口排序及上下移动,提供了丰富的函数实例,如sum()over(), avg()over(), LAG() over(), LEAD()over()等。此外,还探讨了Hive处理JSON文件的方法,以及如何自定义UDF进行数据处理。" 114620756,10542470,Java生成带文字和图片的自定义图形,"['Java图形API', '图像合成', '图形绘制', '文件输出', 'JPEG编码库']
摘要由CSDN通过智能技术生成

一 hive内置函数

1 窗口函数

1、类型转换
语法:cast(value AS type)
例:select id from test_3 where cast(id as bigint)<3;
这样id就会显示成bigint类型,如不能转换为bigint,cast就会返回NULL.
2、切割
语法:split(string str, string pat)
例:select split('nihao|hello|nice','\\|')
(注:hive当中直接写‘|’是不识别的,必须写成‘\\|’)
❤️ 也可以使用正则表达式截取字符串
语法:regexp_extract(string subject, string pattern, int index)
例:select regexp_extract('hello<B>nice</B>haha','<B>(.*)</B>',1)
select regexp_extract(字段名,正则表达式,索引)
3、去除字符串出现的空格
去除前后的空格:trim(string A)
去除前面的空格:ltrim(string A)
去除后面的空格:rtrim(string A)
4、拼接字符串
语法:concat(string A, string B...)
例:select concat('nihao','hello')
5、字符串截取
语法:substr(string A,3,2)
例:select substr('abcde',3,2)
6、炸裂函数
语法: explode(split(‘string A|string B|string C’,‘\\|’))
例:select explode(split("nice|good|well","\\|"))
(注:通常和split函数一起使用)
7、case when 的两种基础用法
方法一:

case when   string A  满足条件一( eg: =>likethen 结果1
     when   string B  满足条件二                    then 结果2
     else  结果3  end

方法二:

case 字段
     when  value 1  then    结果1
     when  value 2  then    结果2
     else     结果3 
     end

8、日期处理函数
(1)date_format函数(根据格式整理日期)
(注:日期字符串必须满足yyyy-MM-dd HH:mm:ss格式)

select date_format('2022-10-21','yyyy-MM');
输出:2022-10

(2)date_add函数(返回日期n天后的日期)

select date_add('2022-10-21',-1);
输出:2022-10-20

select date_add('2022-10-21',1);
输出:2022-10-22

(3)date_sub(返回日期n天前的日期)

select date_sub('2022-10-21',1);
输出:2022-10-20

select date_sub('2022-10-21',-1);
输出:2022-10-22

(4)next_day函数(得到字符串日期的下周几的具体日期)
例:得到2022-10-21的下一个周一

select next_day('2022-10-21','Monday');
输出:2022-10-24

(5)last_day(得到当月最后一天日期)

select last_day('2022-10-21');
输出:2022-10-31

1.1 窗口聚合

1.1.1 sum()over()函数

语法:

sum(value) over(partition by string order by string)
说明:
partition by:以什么分区(分组)
order by:区内(组内)以什么排序

案例:
(1)数据准备

cookie1,2015-04-10,1
cookie1,2015-04-13,3
cookie1,2015-04-11,5
cookie1,2015-04-12,2
cookie1,2015-04-15,4
cookie1,2015-04-16,4
cookie1,2015-04-14,4
cookie2,2015-04-14,3
cookie2,2015-04-15,9
cookie2,2015-04-16,</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值