How to get the Date of the first day of a week given a time stamp in Hadoop Hive?

≯℡__Kan透↙ 提交于 2019-12-23 03:49:26

问题


Besides writing a custom UDF to support this issue, is there any known methods of achieving this? I'm currently using Hive 0.13.


回答1:


date_sub(m.invitationdate,pmod(datediff(m.invitationdate,'1900-01-07'),7))

This expression gives the exact solution to my question.

Regards,

Boris




回答2:


This is the easiest and the best solution for fetching 1st day of the week's date:

For Current timstamp:

select date_sub(from_unixtime(unix_timestamp()), cast(from_unixtime(unix_timestamp(), 'u') AS int)) ;

For any given date or column:

select date_sub(from_unixtime(unix_timestamp('2017-05-15','yyyy-MM-dd')), cast(from_unixtime(unix_timestamp('2017-05-15','yyyy-MM-dd'), 'u') AS int)) ;

select date_sub(from_unixtime(unix_timestamp(colname,'yyyy-MM-dd')), cast(from_unixtime(unix_timestamp(colname,'yyyy-MM-dd'), 'u') AS int)) ;



回答3:


Yes, you can do this without writing a UDF. If you look at at the Hive documentation under datetime functions, there is a function from_unixtime() that takes a unix timestamp and a string pattern. A couple of functions down on the documentation page, there is a link that explains the different patterns you can use in this function. So, from your timestamp, you can extract the day of the week and proceed accordingly.

Example Data:

1445313193
1445313100
1445313146
1445040000
1445040023
1445040111

The first three are Monday, 2015-10-19 and the last three are Friday, 2015-10-16.

Query:

select day_of_week
  , date_var
  , case when day_of_week = 'Sun' then date_var
    when day_of_week = 'Sat' then date_sub(date_var, 6)
    when day_of_week = 'Fri' then date_sub(date_var, 5)
    when day_of_week = 'Thu' then date_sub(date_var, 4)
    when day_of_week = 'Wed' then date_sub(date_var, 3)
    when day_of_week = 'Tue' then date_sub(date_var, 2)
    when day_of_week = 'Mon' then date_sub(date_var, 1)
    else NULL
    end as first_day_of_week_date
from (
  select from_unixtime(timestamp, 'EEE') day_of_week
    , from_unixtime(timestamp, 'yyyy-MM-dd') date_var
  from db.table ) A

Output:

Mon 2015-10-19  2015-10-18
Mon 2015-10-19  2015-10-18
Mon 2015-10-19  2015-10-18
Fri 2015-10-16  2015-10-11
Fri 2015-10-16  2015-10-11
Fri 2015-10-16  2015-10-11

So, for today it returns yesterday, which was Sunday, and for last Friday, it returns the previous Sunday, the 11th. I am making the assumption that by "first day of a week", you mean Sunday; if not, you can adjust the code to mean Monday. Hope this helps.




回答4:


Starting with Hive 1.2, you can also do it like this:

select next_day(date_sub('2019-01-01', 7), 'MON')

Output:

2018-12-31


来源:https://stackoverflow.com/questions/33196651/how-to-get-the-date-of-the-first-day-of-a-week-given-a-time-stamp-in-hadoop-hive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!