如果您有 hive 1.2,这应该可以工作。它在语言手册中。我正在运行 hive 0.13,所以我无法测试 date_format 函数,但它应该给你一个 1 - 7 的整数;
select cast(date_format('2015-04-06'),'w' as int) as DOW from table;
我在 hive 0.13 上对此进行了测试:
select from_unixtime(unix_timestamp('2015-07-06','yy-MM-dd'),'u') as
DOW from table;
这导致 1,所以它似乎工作。
如果上述失败,可以使用的其他选项是:
select from_unixtime(unix_timestamp('2015-07-06','yy-MM-dd'),'EEEE') as
DOW from table;
这将返回“星期一”
另外
select from_unixtime(unix_timestamp('2015-07-06','yy-MM-dd'),'EEE') as
DOW from table;
将返回“星期一”
然后您可以简单地做一个案例何时更改为整数。
case when DOW = 'Mon' then 1
when DOW = 'Tue' then 2
-- ...
when DOW = 'Sat' then 6
else 7
end as day_of_week
不要使用这个:
select pmod(datediff('2015-07-06','1970-01-04'),7) + 1 as DOW from table;
这个返回2,不正确,有人建议过。