当前位置: 首页 > news >正文

hive和spark-sql中 日期和时间相关函数 测试对比

测试版本:

hive 2.3.4      spark 3.1.1  hadoop 2.7.7

1、增加月份

  • add_months(timestamp date, int months)
  • add_months(timestamp date, bigint months)
  • Return type: timestamp
  • usage:add_months(now(),1)

2、增加日期

  • adddate(timestamp startdate, int days),
  • adddate(timestamp startdate, bigint days)
  • Return type: timestamp
  • usage:adddate(now(),1)

3、当前时间戳

  • current_timestamp()和now()等价

4、日期相减

  • datediff(string enddate, string startdate)
  • Return type: int
  • usage:datediff("2018-08-05", "2018-08-03")

5、得到天,得到月份

  • day(string date)
  • Return type: int
  • usage: day("2018-08-05")

6、得到星期英文

  • dayname(string date)
  • Return type: string
  • usage:dayname("2018-08-05") Sunday

7、得到这一天是这周的第几天

  • dayofweek(string date)   1 (Sunday) to 7 (Saturday).
  • Return type: int
  • usage:dayofweek("2018-08-06")

8、加天数和减天数

1)days_add(timestamp startdate, int days)

  • Return type: timestamp
  • usage:days_add(now(),2)

2)、days_sub(timestamp startdate, int days)

  • Return type: timestamp
  • usage:days_sub(now(), 2)

9、格式化日期

  • from_unixtime(bigint unixtime[, string format])
  • Return type: string

注意参数

  • usage:from_unixtime(1392394861,"yyyy-MM-dd");

10、得到小时

  • hour(string date)
  • Return type: int
  • usage:hour("2018-08-06 12:32:54")

11、增加小时 和 减少小时

1)、hours_add(timestamp date, int hours)

  • Return type: timestamp
  • usage:hours_add(now(),2)

2)、hours_sub(timestamp date, int hours)

  • Return type: timestamp
  • usage:hours_sub(now(),2)

12、得到分钟

  • minute(string date)
  • Return type: int
  • usage:minute(now())

13、增加分钟和减少分钟

1)、minutes_add(timestamp date, int minutes)

  • Return type: timestamp
  • usage:minutes_add(now(),2)

2)、minutes_sub(timestamp date, int minutes)

  • Return type: timestamp
  • usage:minutes_sub(now(),2)

14、得到月份

  • month(string date)
  • Return type: int
  • usage:month("2018-08-06 12:32:54")

15、月份相加和减月份

1)、months_add(timestamp date, int months)

  • Return type: timestamp
  • usage:months_add(now(),3)

2)、months_sub(timestamp date, int months)

  • Return type: timestamp
  • months_sub(now(),3)

16、得到秒

  • second(string date)
  • Return type: int

17、秒加 和 秒减

1)、seconds_add(timestamp date, int seconds)

  • Return type: timestamp

2)、seconds_sub(timestamp date, int seconds)

  • Return type: timestamp

18、得到日期

  • to_date(now())

19、得到1970到今秒

  • unix_timestamp(),
  • unix_timestamp(string datetime),
  • unix_timestamp(string datetime, string format),
  • unix_timestamp(timestamp datetime)
  • Return type: bigint

20、得到这周是这年的多少周

  • weekofyear(string date)
  • Return type: int
  • usage:weekofyear("2018-08-06 12:32:54")

21、周加  和 周减

1)、weeks_add(timestamp date, int weeks)

  • Return type: timestamp
  • usage:weeks_add("2018-08-06 12:32:54", 1)

2)、weeks_sub(timestamp date, int weeks)

  • Return type: timestamp
  • usage:weeks_sub("2018-08-06 12:32:54", 1)

22、得到年

  • year(string date)
  • Return type: int

23、年加 和 年减

1)、years_add(timestamp date, int years)

  • Return type: timestamp

2)、years_sub(timestamp date, int years)

  • Return type: timestamp

参考:hive 日期函数 - 文章教程 - 文江博客

http://www.lryc.cn/news/227694.html

相关文章:

  • Cell和RefCell
  • DaoWiki(基于Django)开发笔记 20231113
  • 照片放大软件 Topaz Gigapixel AI mac中文版简介
  • 某手游完整性校验分析
  • 【ElasticSearch】学习使用DSL和RestClient编写查询语句
  • asp.net外卖网站系统VS开发mysql数据库web结构c#编程Microsoft Visual Studio
  • 2.4.0 Milky Way 强势登场!新功能大爆炸,让你High翻全场!
  • C语言----静态链接库和动态链接库
  • PCA(主成分分析)数据降维技术代码详解
  • Git版本控制系统之分支与标签(版本)
  • JSP运行环境搭建
  • React通过属性 (props) 和状态 (state) 来传递和管理组件的数据
  • Web相机和浏览器的二维码扫描方案
  • 云端部署ChatGLM-6B
  • 设计模式(3)-结构型模式
  • C/C++调试工具 - gdb详解
  • 传奇GOM引擎微端连接不上如何解决
  • Easymesh介绍
  • 图像相似度对比方法
  • C++ 配合图形库实现画线效果
  • zookeeper应用之分布式屏障
  • PDBADMIN 的作用,命名,重建 以及能否DROP
  • 华为L410上制作内网镜像模板02
  • 美国材料与试验协会ASTM发布新版玩具安全标准 ASTM F963-23
  • Postman模拟上传文件
  • 【系统架构设计】架构核心知识: 1 系统工程与信息系统基础
  • 加班把数据库重构完毕
  • Centos(Linux)安装mysql数据库
  • 【数据结构】深度剖析ArrayList
  • 离线环境通过脚本实现服务器时钟同步(假同步)