【问题标题】:Hive TimeStamp for Weekly and Quarterly data每周和每季度数据的 Hive 时间戳
【发布时间】:2016-06-23 20:29:48
【问题描述】:

下面的 hive 命令,

select * from my_new_table
  where month(time_stamp)= '03'
    and year(time_stamp) = '2016'
    and age = '1'
    and gender = '0'
    and income = '4'
    and ethnicity = '3'
    and marital_status = '1';

为 2016 年 3 月 (03) 月份的所有数据(31 天)生成以下结果:

time_stamp              age gender  income  ethnicity   marital_status
2016-03-14@17:42:47.000 1   0       4       3           1
2016-03-14@16:10:51.000 1   0       4       3           1
2016-03-20@15:16:44.000 1   0       4       3           1
2016-03-14@17:13:51.000 1   0       4       3           1
2016-03-14@17:12:51.000 1   0       4       3           1
2016-03-14@18:24:51.000 1   0       4       3           1
2016-03-03@13:02:06.000 1   0       4       3           1

同样,我想获取 2016 年第二季度(或第 n 季度,数据应为 3 个月,从本季度的 3 月 1 日至 5 月 31 日)和第 12 周(或第 n 周,数据应为2016 年这个特定周的 7 天)。什么是正确的 Hive 命令?

如果我在 Hive 命令中将 month 替换为 quarter 或 week,则会出错。

select * from my_new_table 
where quarter(time_stamp)='03' 
and year(time_stamp) = '2016'; 

返回

FAILED: SemanticException [Error 10011]: Line 1:71 Invalid function 'quarter'

和

select * from my_new_table 
where week(time_stamp)='12' 
and year(time_stamp) = '2016'; 

返回

FAILED: SemanticException [Error 10011]: Line 1:71 Invalid function 'week'

看起来需要在获取第 n 个季度或第周时包含计算,但不确定。请帮忙。谢谢。

【问题讨论】:

  • 如果您显示您尝试过的命令以及生成的特定错误,这可能会有所帮助。 Date Functions in Hive 可能会提供信息。
  • select * from my_new_table where quarter(time_stamp)='03' and year(time_stamp) = '2016'; FAILED: SemanticException [Error 10011]: Line 1:71 Invalid function 'quarter' select * from my_new_table where week(time_stamp)='12' and year(time_stamp) = '2016'; FAILED: SemanticException [Error 10011]: Line 1:71 Invalid function 'week'

标签: hive


【解决方案1】:

文档说month 和week 函数需要“字符串日期”,而不是实际的日期或时间戳类型作为输入。您可能需要使用 date_format 函数将时间戳转换为字符串,然后使用函数,例如

month(date_format(timestamp, 'yyyy-MM-dd'))

这并不能解释为什么您将这些函数作为 where 子句的一部分的第一个示例似乎有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多