简体   繁体   English

蜂巢表上基于时间的查询

[英]time based query on hive table

My table structure is like this: 我的表结构是这样的:

hive> describe user_data2;
OK
received_at             string                                      
message_id              string                                      
type                    string                                      
version                 string                                      
timestamp_user          string                                      
user_id                 string                                      
sent_at                 string                                      
channel                 string                                      
time_log                string 

And I am targetting this fields, 我的目标是这个领域,

hive> select received_at, time_log, user_id from user_data2 limit 5;
OK
2016-01-08T12:27:05.565Z    1452256025  836871
2016-01-08T12:27:12.634Z    1452256033  800798
2016-01-08T12:27:12.632Z    1452256033  795799
2016-01-08T12:27:13.694Z    1452256033  820359
2016-01-08T12:27:15.821Z    1452256036  294141

On this I want to make time based query. 在此我想进行基于时间的查询。 like 喜欢

  1. Avg Hours Active; 有效平均小时数; per month; 每月; period: last 12 months 期间:最近12个月
  2. % of users active between 0-1h / day 每天0-1小时之间处于活动状态的用户百分比
  3. % of users active between 1-2h / day 每天1-2小时之间处于活动状态的用户百分比
  4. % of users active between 2-4h / day 每天2-4小时之间处于活动状态的用户百分比
  5. % of users active between 4-8h / day 每天4-8小时之间处于活动状态的用户百分比
  6. % of users active between 8-12h / day 每天8-12小时之间处于活动状态的用户百分比
  7. % of users active between 12-16h / day 每天12-16小时之间活跃的用户百分比
  8. % of users active between 16-24h / day 每天16-24小时之间处于活动状态的用户百分比

I got some clue of using Datetime UDF - https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions 我得到了使用日期时间UDF的一些线索- https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions

But I am not aware how to use this function. 但是我不知道如何使用此功能。

I tried: 我试过了:

select unix_timestamp(received_at) from user_data2 limit 5;
OK
NULL
NULL
NULL
NULL
NULL

Which gives none. 哪也没有。

I appreciate if someone give example of using time UDF and getting records between two hours or some other time frame. 如果有人举一个使用时间UDF并获取两个小时或其他时间范围内的记录的示例,我将不胜感激。

Assuming your local TZ is Rome... 假设您当地的TZ是罗马...

select
  from_utc_timestamp(regexp_replace(regexp_replace(RECEIVED_AT, 'T',' '), '\\..*$',''), 'Europe/Rome') as TS_RECEIVED,
  cast(from_unixtime(cast(TIME_LOG as int)) as timestamp) as TS_LOGGED
from WTF ;

+------------------------+------------------------+--+
|      ts_received       |      ts_logged         |
+------------------------+------------------------+--+
| 2016-01-08 13:27:05.0  | 2016-01-08 13:27:05.0  |
+------------------------+------------------------+--+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM