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 日期函数 - 文章教程 - 文江博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 时间函数Spark SQLHive常用的函数之一,用于处理时间类型的数据。常见的时间函数包括: 1. year/month/day/hour/minute/second:获取时间的年/月/日/小时/分钟/秒。 2. date_format:将时间格式化为指定的字符串格式。 3. from_unixtime:将Unix时间戳转换为时间格式。 4. unix_timestamp:将时间格式转换为Unix时间戳。 5. datediff:计算两个日期之间的天数差。 6. add_months:给定日期加上指定的月数。 7. last_day:获取给定日期所在月份的最后一天。 8. trunc:截取时间,只保留指定的时间部分。 9. to_date:将字符串转换为日期类型。 10. to_utc_timestamp:将时间转换为UTC时间。 这些函数可以帮助我们方便地处理时间类型的数据,提高数据处理的效率和准确性。 ### 回答2: 时间函数Spark SQLHive的重要函数之一,通常用于处理日期时间相关的数据。本文将从以下几个方面来介绍Spark SQLHive常见的时间函数日期函数时间函数时间函数和间隔函数日期函数 日期函数主要用于处理日期数据,包括YEAR、MONTH、DAY、WEEK、DAYOFWEEK等。YEAR函数用于提取指定日期的年份;MONTH函数用于提取指定日期的月份;DAY函数用于提取指定日期的日份;WEEK函数用于提取指定日期所在年份的周数;DAYOFWEEK函数用于提取指定日期所在星期的第几天,其1代表星期天,2代表星期一,以此类推。 时间函数 时间函数主要用于处理时间数据,包括HOUR、MINUTE、SECOND、FROM_UNIXTIME等。HOUR函数用于提取指定时间的小时数;MINUTE函数用于提取指定时间的分钟数;SECOND函数用于提取指定时间的秒数;FROM_UNIXTIME函数用于将整数时间戳格式化为可读时间时间函数 时间函数主要用于处理时间戳数据,包括TO_UNIX_TIMESTAMP、UNIX_TIMESTAMP、FROM_TIMESTAMP等。TO_UNIX_TIMESTAMP函数用于将时间字符串转换成时间戳;UNIX_TIMESTAMP函数用于获取当前时间时间戳;FROM_TIMESTAMP函数用于将时间字符串转换成时间戳(默认格式)。 间隔函数 间隔函数主要用于计算时间间隔,包括DATEDIFF、TIMESTAMPDIFF等。DATEDIFF函数用于计算两个日期之间的间隔天数;TIMESTAMPDIFF函数用于计算两个时间戳之间的间隔时间,可以是天、小时、分钟、秒等。 综上所述,Spark SQLHive时间函数在数据处理具有重要的作用,能够方便地提取和计算日期时间相关的信息。了解常见的时间函数,能够在实际的数据处理提高工作效率和精度。 ### 回答3: 时间函数Spark SQLHive常用的函数之一,它们可以用来操作日期时间信息。本文将介绍Spark SQLHive的常用时间函数。 1. current_date()和current_timestamp() current_date()函数返回当前日期,格式为“yyyy-MM-dd”。current_timestamp()函数返回当前时间戳,格式为“yyyy-MM-dd HH:mm:ss”。 2. date_add()和date_sub() date_add(date, days)函数返回一个日期加上指定天数后的日期。date_sub(date, days)函数返回一个日期减去指定天数后的日期。其date指定要操作的日期,days指定要加或减的天数。 3. datediff() datediff(endDate, startDate)函数返回两个日期之间的天数差。其endDate和startDate可以是日期型或字符串型,要求日期格式一致。 4. year()、quarter()、month()、day()和weekofyear() year(date)函数返回日期的年份,quarter(date)函数返回日期的季度数,month(date)函数返回日期的月份,day(date)函数返回日期的天数,weekofyear(date)函数返回日期所在的年份第几周。 5. from_unixtime()和unix_timestamp() from_unixtime(unix_time, format)函数将UNIX时间戳转换为指定格式的日期时间。unix_timestamp(string date, format)函数将指定格式的日期时间转换为UNIX时间戳。其format可以为“yyyy-MM-dd HH:mm:ss”。 6. date_format() date_format(date, format)函数日期从一种格式转换为另一种格式。其date为要转换的日期,format为目标格式。 7. hour()、minute()和second() hour(time)函数返回时间的小时数,minute(time)函数返回时间的分钟数,second(time)函数返回时间的秒数。其time可以为时间型或字符串型,要求时间格式一致。 总之,时间函数的使用可以帮助我们处理与日期时间相关的问题,例如计算日期差、格式转换等操作。 以上就是有关Spark SQLHive时间函数的介绍,希望对大家有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值