ITPub博客

首页 > IT职业 > IT职场 > hive计算日期差函数datediff,hive修改日期连接符

hive计算日期差函数datediff,hive修改日期连接符

IT职场 作者:d0e1f 时间:2021-09-09 09:31:50 0 删除 编辑

本文主要讲述怎样用hive计算日期差问题。

首先,hive本身有一个UDF,名字是datediff。我们来看一下这个日期差计算的官方描述,(下面这个是怎么出来的):

hive> desc function extended datediff;     //*查函数功能*//
OK
datediff(date1, date2) - Returns the number of days between date1 and date2
date1 and date2 are strings in the format 'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. The time parts are ignored.If date1 is earlier than date2, the result is negative.
Example:
   > SELECT datediff('2009-30-07', '2009-31-07') FROM src LIMIT 1;

从上面的描述可以看出datediff用法很简单,就是datediff('日期1','日期2'),其中日期是有格式的,目前支持以下两种格式:

yyyy-MM-dd HH:mm:ss
yyyy-MM-dd

但是,看上面的显示 hive function里对datediff的example有问题,2009-30-07应该为2009-07-30,实际上这个udf 还支持2009-7-30这种格式。这里我的一个列子:

SELECT datediff('2013-10-15', '2013-9-15') FROM words LIMIT 1;

如果我的日期是2013/10/15这样的,该这么办?这时候可以用hive的regexp_replace 这个UDF。示例如下:

SELECT datediff(regexp_replace('2013/10/15', "/", "-"),regexp_replace('2013/9/15', "/", "-")) FROM words LIMIT 1;

hive的UDF还是很强大的,可以多show functions,学习了解各种hive function。希望这篇文章对你有用


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4019/viewspace-2804324/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-10-17

  • 博文量
    75
  • 访问量
    555